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
- Concise and efficient: Each utility class is responsible for only one small styling operation; developers can combine different classes as needed to quickly complete page layout and styling.
- Flexibility: Through class combinations, almost any style can be achieved without writing complex CSS code.
- Responsive support: Tailwind provides built-in responsive classes, making it very easy to adjust layout and styles according to screen size.
- Avoid naming conflicts: Since each class has a very clear and independent purpose, using utility classes avoids the problems of naming conflicts and style overrides in traditional CSS.
Specific applications of utility classes
1. Text-related utility classes
text-center: Set the text alignment to center.text-lg: Set the text size tolg(large font).font-bold: Make the text bold.text-red-500: Set the text color to red (500is a specific shade of red).
Example
Hello, Tailwind!
2. Background-related utility classes
bg-blue-500: Set the element's background color to blue.bg-opacity-50: Set the background opacity to 50%.bg-cover: Resize the background image to completely cover the element.
Example
Welcome to Tailwind
3. Spacing-related utility classes
p-4: Set the element's padding to1rem。m-8: Set the element's margin to2rem。mt-4: Set the element's top margin to1rem。
Example
This box has padding and margin.
4. Layout-related utility classes
flex: Set the element's layout mode to flexbox.grid: Set the element's layout mode to CSS Grid.items-center: Vertically center the child elements in a flexbox container.justify-between: In a flexbox container, distribute the space between child elements equally.
Example
Item 1
Item 2
5. Border and border-radius-related utility classes
border: Add a border.border-2: Set the border width to2px。rounded-lg: Set the element's border radius to0.5rem。
Example
This box has a border and rounded corners.
6. Shadow-related utility classes
shadow: Add a default shadow to the element.shadow-lg: Add a larger shadow.shadow-none: Remove the shadow.
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).
sm:: Only takes effect when the screen width is greater than or equal to 640px.md:: Only takes effect when the screen width is greater than or equal to 768px.lg:: Only takes effect when the screen width is greater than or equal to 1024px.xl:: Only takes effect when the screen width is greater than or equal to 1280px.
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.
hover:bg-blue-700: When the element is hovered over with the mouse, the background color turns blue.focus:outline-none: When the element gains focus, remove the default border outline.active:bg-red-500: When the element is in the active state, the background color turns red.
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
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:
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).
| 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:
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).
| Class Names | Purpose |
|---|---|
shrink-0 |
Prevent this element from shrinking, ensuring its size is not affected by other elements in the flex container. |
Image:
| 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).
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!
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). |