author Jeremiah Swank

Tailwind: A utility class approach to styling

If you’re just stepping into the world of front-end development or looking for ways to streamline your CSS, you might have come across Tailwind CSS. Tailwind is a utility-first CSS framework that has been rapidly gaining popularity among developers, particularly those working with React. But what exactly makes Tailwind so appealing, and how can it benefit you as a beginner? Let’s dive in!

What is Tailwind CSS?

Tailwind CSS is different from traditional CSS frameworks like Bootstrap. Instead of providing pre-designed components, Tailwind offers low-level utility classes that allow you to build custom designs directly in your markup. This utility-first approach means you can rapidly create unique and responsive layouts without ever leaving your HTML or JSX.

Benefits of Tailwind CSS

  • Highly Customizable: Tailwind’s utility classes are like building blocks that you can mix and match to create any design you can imagine. You’re not restricted by predefined components, making it easier to implement your unique vision.

  • Consistency Across Your Project: By using the same set of utility classes throughout your project, you ensure a consistent look and feel. Tailwind’s default configuration is also based on a design system, which promotes visual consistency. If desired you can override the default design system with your own custom design system.

  • Small Bundle Size: With css your bundle size grows as more css is added. When functionality is removed the styling is often missed leading to a bloated bundle size. Tailwind does not have this problem. Tailwind includes only the styles you use in your project. This leads to smaller CSS files and faster load times, which is great for performance.

  • Naming things is hard: With Tailwind, you don’t have to worry about naming things. Your time and energy can be better spent on development.

Tailwind with React

When working with React, Tailwind’s utility-first approach truly shines. React developers often face the challenge of maintaining CSS in JS or scoped styles. In other words we want our components styles to only apply to a specific comonent and not bleed into the rest of the application causing side effects. This keeps your component files clean and easy to manage. With Tailwind, you can style your components directly in the JSX using utility classes.

Using Tailwind with React

Lets take a look at a real world example of styling a react component with tailwind:

function Navbar() {
  return (
    <nav className="bg-gray-800 p-4">
      <ul className="flex space-x-4">
        <li>
          <a href="#" className="text-white hover:text-gray-400">
            Home
          </a>
        </li>
        <li>
          <a href="#" className="text-white hover:text-gray-400">
            About
          </a>
        </li>
        <li>
          <a href="#" className="text-white hover:text-gray-400">
            Contact
          </a>
        </li>
      </ul>
    </nav>
  );
}

Explanations:

  • bg-gray-800: Sets the background color of the navbar to dark gray. In tailwind the default color shades range from 50 to 950. 50 being the lightest and 950 being the darkest.
  • p-4: Adds padding around the navbar. 1 unit of padding is equal to 0.25rem so 4 units of padding is equivalent to 1rem.
  • flex space-x-4: Uses flexbox for layout and adds 1 rem of space between the items.
  • text-white: Sets the text color of the links to white.
  • hover:text-gray-400: Changes the text color to gray when hovering over the links.

While it might seem counterintuitive, Tailwind can actually make your code more readable. The utility class names are descriptive and often self-explanatory, making it easier to understand how a component is styled at a glance.

Common Utility Classes

Tailwind provides a wide range of utility classes that can be used to style your components. There are too many to cover in this article. Here are some links to common utility classes:

To find more utlitiy classes use the quick search bar in the tailwind documentation.

Flexbox layouts

Tailwind CSS provides a wide array of utility classes to make working with Flexbox simple and intuitive. Suppose you want to create a container that centers its content both horizontally and vertically.

<div className="flex items-center justify-center h-screen">
  <div className="text-white bg-blue-500 p-8 rounded">
    Centered Content
  </div>
</div>

Explanation:

  • flex: Sets the outer div as a flex container.
  • items-center: Centers the content vertically within the container.
  • justify-center: Centers the content horizontally within the container.
  • h-screen: Sets the height of the container to be 100% of the viewport height, making it a full-screen container.
  • text-white bg-blue-500 p-8 rounded: Styles the inner div with white text, a blue background, padding, and rounded corners.

This setup will center the “Centered Content” box both horizontally and vertically within the browser window.

Responsive Layouts

Responsive layouts are crucial in modern web development, allowing your site or application to adapt to different screen sizes and devices. Tailwind CSS makes creating responsive layouts straightforward with its built-in responsive utilities.

Responsive Breakpoints

Tailwind provides default responsive breakpoints, which you can use to apply different styles at various screen widths. These breakpoints are:

  • sm (Small): min-width: 640px
  • md (Medium): min-width: 768px
  • lg (Large): min-width: 1024px
  • xl (Extra Large): min-width: 1280px
  • 2xl (2X Large): min-width: 1536px

You can use these breakpoints to conditionally apply styles based on the screen size.

Responsive Flexbox Layout

To create responsive layouts, you prepend the breakpoint prefix to any utility class. For example:

<div className="flex flex-col md:flex-row">
  <div className="bg-blue-500 p-4">Column 1</div>
  <div className="bg-green-500 p-4">Column 2</div>
</div>

Explanation:

  • flex flex-col: By default, the flex container arranges items in a column (vertically).
  • md:flex-row: On medium screens and up, the flex container arranges items in a row (horizontally).

Conclusion

Tailwind CSS is a powerful utility-first framework that provides developers with a flexible, consistent, and efficient approach to styling web applications. By offering a wide range of pre-defined utility classes, Tailwind allows you to build complex, responsive layouts directly in your HTML or JSX, reducing the need for custom CSS. Its mobile-first design philosophy, extensive customization options, and seamless integration with modern frameworks like React make it an excellent choice for developers looking to streamline their workflow and maintain clean, maintainable code. Whether you’re building simple pages or complex applications, Tailwind’s utility-based approach empowers you to create visually appealing and responsive designs with ease.