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

Tailwind CSS Responsive Design

Tailwind CSS Responsive Design

Responsive design is an important part of modern web development; it ensures that web pages display well on different screen sizes.

Tailwind CSS provides very convenient tools for implementing responsive layouts; its responsive design mainly uses breakpoints and prefixes to control style application at different screen sizes.


Building Adaptive User Interfaces with Responsive Utility Classes

Every utility class in Tailwind CSS can be applied conditionally via breakpoints, making it easy to build complex responsive interfaces in HTML.

1. Ensure the Viewport Is Set Correctly

Before you begin, make sure you have added the following viewport meta tag to the section of your HTML document:





This step ensures that web page content scales and displays correctly on mobile devices.

2. Apply Utility Classes via Breakpoint Prefixes

To apply a utility class at a specific breakpoint, you only need to add the breakpoint name before the class name and separate them with a colon (:).

Example

3. Five Default Breakpoints

Tailwind CSS supports the following five breakpoints by default, which are inspired by common device resolutions:

Breakpoint Name Minimum Width Prefix Common Devices
sm 640px sm: Small screens (e.g., landscape mode of phones)
md 768px md: Medium screens (e.g., tablet devices)
lg 1024px lg: Large screens (e.g., small laptops)
xl 1280px xl: Extra large screens (e.g., desktop monitors)
2xl 1536px 2xl: Ultra-wide screens (e.g., higher resolution monitors)

How to Implement Responsive Design

1. Responsive Layout Control

In Tailwind, you can use responsive prefixes to adjust layout, fonts, colors, spacing, and more.

Example

  This text will be center-aligned on small screens, left-aligned on medium screens, and right-aligned on larger screens.

Try it »

Explanation:

2. Responsive Grid Layout

By using responsive utility classes, you can define column layouts for different screen sizes.

Example

 

Item 1

 

Item 2

 

Item 3

 

Item 4

Try it »

Explanation:

3. Custom Responsive Breakpoints

In addition to the five default breakpoints, Tailwind allows you to tailwind.config.js customize breakpoints to meet specific project requirements.

Example

module.

exports

=

{

  theme

:

{

    screens

:

{

     

'xs'

:

'480px'

,

// Extra small screen

     

'sm'

:

'640px'

,

     

'md'

:

'768px'

,

     

'lg'

:

'1024px'

,

     

'xl'

:

'1280px'

,

     

'2xl'

:

'1536px'

,

   

}

,

 

}

,

}

Custom breakpoints can be used just like the default breakpoints:

Example

  Responsive text size.

Try it »

4. Media Query Classes

Tailwind provides custom media queries, for example:

Example

  This div changes color based on screen width.

Try it »

5. Responsive Hide and Show

Through classes such as hidden and block, combined with responsive prefixes, you can hide or show content:

Example

Try it »