Tailwind allows you to build responsive designs in the same way you build the rest of your design — using utility classes. Every utility in Tailwind is also available in screen-size specific variations. For example, the .font-bold utility can be used on small screen sizes using the .sm:font-bold class, on medium screen sizes using the .md:font-bold class, on large screen sizes using the .lg:font-bold class and on extra large screen sizes using the .xl:font-bold class.

This is done using predefined screen sizes (media query breakpoints), each of which are given a unique name like sm, md, lg and xl. By default Tailwind takes a “mobile first” approach, where each screen size represents a minimum viewport width. Any classes you apply at smaller screen sizes are also applied to larger sizes, unless of course you override them, which is the whole point! This approach, while simple, is actually very powerful and can be used to build complex, beautiful, responsive designs.

Responsive example

Responsive Design - 图1

all

Responsive Design - 图2

sm

Responsive Design - 图3

md

Responsive Design - 图4

lg

Responsive Design - 图5

xl

Responsive Design - 图6

Customizing screens

You define your project’s screen sizes in your Tailwind config under the screens key. Screens in Tailwind are essentially CSS media queries. If you provide a single value for a screen, Tailwind will treat this as the minimum screen size value for that screen breakpoint.

Here are the default screen sizes:

  1. screens: {
  2. 'sm': '576px',
  3. // => @media (min-width: 576px) { ... }
  4. 'md': '768px',
  5. // => @media (min-width: 768px) { ... }
  6. 'lg': '992px',
  7. // => @media (min-width: 992px) { ... }
  8. 'xl': '1200px',
  9. // => @media (min-width: 1200px) { ... }
  10. },

Feel free to have as few or as many screens as you want, naming them in whatever way you’d prefer for your project.

For example, you could use device names instead of sizes:

  1. screens: {
  2. 'tablet': '576px',
  3. // => @media (min-width: 576px) { ... }
  4. 'laptop': '992px',
  5. // => @media (min-width: 992px) { ... }
  6. 'desktop': '1200px',
  7. // => @media (min-width: 1200px) { ... }
  8. },

These screen names will be reflected in your utilities, so your .bg-red utilities would now look like this:

  1. .bg-red { background-color: config('colors.red'); }
  2. @media (min-width: 576px) {
  3. .tablet\:bg-red { background-color: config('colors.red'); }
  4. }
  5. @media (min-width: 992px) {
  6. .laptop\:bg-red { background-color: config('colors.red'); }
  7. }
  8. @media (min-width: 1200px) {
  9. .desktop\:bg-red { background-color: config('colors.red'); }
  10. }

Advanced screens

Tailwind also allows for more complex screen definitions, which can be useful in certain situations. For example, if you wanted to define both the minimum and maximum size for your screens, you could do that like this:

  1. screens: {
  2. 'sm': {'min': '576px', 'max': '767px'},
  3. 'md': {'min': '768px', 'max': '991px'},
  4. 'lg': {'min': '992px', 'max': '1199px'},
  5. 'xl': {'min': '1200px'},
  6. },

You can also provide multiple ranges per screen. This is useful in situations where you have a sidebar navigation and want to maintain consistent content breakpoints, regardless of the navigation being visible or not. Here’s an example:

  1. screens: {
  2. 'sm': '500px',
  3. 'md': [
  4. // Sidebar appears at 768px, so revert to `sm:` styles between 768px
  5. // and 868px, after which the main content area is wide enough again to
  6. // apply the `md:` styles.
  7. {'min': '668px', 'max': '767px'},
  8. {'min': '868px'}
  9. ],
  10. 'lg': '1100px',
  11. 'xl': '1400px',
  12. },

Print screens

As mentioned earlier, screens in Tailwind are essentially just CSS media queries. So while you normally define your screensizes in pixels, it’s possible to also define non-regular screens using the raw key. Here is an example of how you could use this to create a print-only screen size.

  1. screens: {
  2. 'sm': '576px',
  3. 'md': '768px',
  4. 'lg': '992px',
  5. 'xl': '1200px',
  6. 'print': {'raw': 'print'}
  7. },