In this article we’ll be looking into my personal favorite design framework: Tailwind CSS. A completely style-agnostic, utility-based library for creating quick and responsive designs. Tailwind is so simple that once you understand the naming conventions and patterns you can almost guess most of the functionality without needing the documentation.
All of the options introduced here can be explored in more detail in the official docs.
While there are a few different methods for setting up Tailwind, like Gulp, postCSS, or even their own CLI, the simplest route for getting started for learning’s sake is just be to use the CDN URL from unpkg: https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css
.
Or, you can install the library into your project using npm or Yarn:
Here’s a simple boilerplate HTML file that includes Tailwind from unpkg:
The class naming for colors is always the same, element-color-intensity
. So a red background becomes bg-red-500
, with the number value ranging from 100 to 900. This patterns applies to borders, backgrounds, and text.
Here’s a simple example:
Width and height, shortened to w
and h
, can take values from 0 to 64 (with some values missing, which you can check for in the docs or VSCode’s IntelliSense) and a few key words like auto
, full
for 100%, or screen
for 100vw/vh. Width can also use fractions out of 1-6 or out of 12, so we could write 50% like 1/2
, 2/4
, 3/6
, or 6/12
.
Spacing works very similarly, just the property, side (shorthand without the dash), then the value. So padding-bottom: 2rem;
becomes pb-8
, again ranging from 0 to 64 and the side being eiter t
,b
, l
, r
, x
for right and left, or y
for top and bottom.
Tailwind offers us many of the comforts of standard CSS positioning, like floats, position, and even Flexbox. Using them is almost exactly as you would expect, except that with Flexbox you just need to initialize it with flex
first.
Similar to size, the naming pattern is just property-value
, so a right float becomes float-right
and justify-content: center;
becomes justify-center
.
Besides the standard things we can already do in CSS, Tailwind offers some shortcuts for what would otherwise be very tedious, like adding contingency for our font-family, which we can take care of with just font-sans
, font-serif
, or font-mono
and have a group of fonts taken care of.
Instead of the 0-64 units we’ve been using, font-size
(shortened to text
) takes xs
,sm
,base
,lx
, and xl
through 6xl
.
Besides those exceptions, most of the text options is CSS are available with the same naming patterns as before.
This is where Tailwind really shines, in cutting down on the need for media queries. With these prefixes, we can limit a class to only work above a specific width, with the un-prefixed version, like what we’ve been working with so far, working for everything outside of our range.
sm
640pxmd
768pxlg
1024pxxl
1280pxHopefully this was an helpful introduction to this powerful little library. The learning curve for Tailwind is so small and its syntax so consistent that with very little experience you can start creating wonderful designs before even needing to look at the CSS.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!