Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Utility Classes (Utility-First)

Tailwind CSS Utility Classes (Utility-First)

Tailwind CSS's utility classes (Utility-First) are a utility-first design approach that allows developers to apply predefined style classes directly to HTML elements without writing traditional CSS code.

The core philosophy of Tailwind CSS is Utility-First, which means you can achieve a complete style by combining single-purpose CSS classes rather than relying on complex custom styles.

Tailwind CSS provides a large number of utility classes, each performing a simple styling task; for example, text-center centers text, bg-blue-500 sets the background color, and p-4 sets the padding.

Common utility classes:

Category Utility class examples Description
Typography text-center, text-lg, font-bold Controls text alignment, font size, weight, etc.
Background bg-blue-500, bg-opacity-50 Sets an element's background color, background image, and opacity.
Spacing (Spacing) p-4, m-2, mt-8 Controls an element's padding and margin.
Layout (Layout) flex, grid, block Sets an element's display type and layout method.
Sizing (Sizing) w-32, h-48 Set element width and height.
Borders border, border-2, rounded Sets an element's border, border width, and border radius.
Shadows shadow, shadow-lg Controls an element's shadow effects.
Opacity opacity-50, opacity-100 Sets an element's opacity.
Responsive sm:text-lg, md:bg-blue-300 Controls style changes at different screen sizes.
State hover:bg-blue-700, focus:ring-4 Sets an element's styles under different interaction states (such as hover, focus, active).

Advantages of utility classes


Specific applications of utility classes

1. Text-related utility classes

Example

Hello, Tailwind!

2. Background-related utility classes

Example

 

Welcome to Tailwind

3. Spacing-related utility classes

Example

 

This box has padding and margin.

4. Layout-related utility classes

Example

 

Item 1

 

Item 2

5. Border and border-radius-related utility classes

Example

 

This box has a border and rounded corners.

6. Shadow-related utility classes

Example

 

This box has a large shadow.

7. Responsive utility classes

Tailwind provides responsive utility classes that can apply different styles at different screen sizes. Common screen sizes include sm(small screen),md(medium screen),lg(large screen),xl(extra-large screen).

Example

 

This background color changes based on the screen size.

8. State utility classes

Tailwind supports utility classes for interaction states, such as hover:, focus:, active: , etc.

Example

Example

Using the traditional method, defining custom CSS styles:

Example

 

   

 

 

   

ChitChat

   

You have a new message!

 

Try it »

Next, let's look at using Tailwind to set element styles directly in HTML using pre-existing classes, without writing CSS:

Example

 

   

ChitChat Logo

 

 

   

ChitChat

   

You have a new message!

 

Try it »

The following is an analysis of each class name in the code:

Outer container:

Class Names Purpose
p-6 Set the element's padding to 1.5rem (24px).
max-w-sm Set the element's maximum width to sm, corresponding to about 24rem (384px).
mx-auto Set the horizontal margin to autoto center the element horizontally.
bg-white Set the element's background color to white.
rounded-xl Set the border radius to xl, i.e., 0.75rem (12px).
shadow-lg Add a larger shadow effect to make the container look like it's floating.
flex Set the element's layout mode to flexboxto make it easy for child elements to arrange in a row.
items-center Center-align the child elements vertically.
gap-x-4 Set the horizontal spacing between child elements in the flex container to 1rem (16px).

Left container:

Class Names Purpose
shrink-0 Prevent this element from shrinking, ensuring its size is not affected by other elements in the flex container.

Image:ChitChat Logo

Class Names Purpose
size-12 This is not a native Tailwind class. It may be a custom class used to set the image size.

Right content container:

No class name; as a child element, it contains the text content.

Title text:
ChitChat

Class Names Purpose
text-xl Set the text size to xl, i.e., 1.25rem (20px).
font-medium Set the font weight to medium, i.e., 500.
text-black Set the text color to black.

Description text:

You have a new message!

Class Names Purpose
text-slate-500 Set the text color to slate-500, i.e., slate gray (a medium shade).