Tailwind CSS and Responsive Design
Tailwind CSS utilizes responsive design by default. It achieves this using a mobile-first breakpoint system. This system works by applying different styles at different viewport sizes via a set of predefined CSS classes. These classes are used in conjunction with the screen
directive in your stylesheets.
Tailwind's default breakpoints are:
sm
: 640pxmd
: 768pxlg
: 1024pxxl
: 1280px2xl
: 1536px
You can add these as class prefixes to apply styles at these breakpoints. For instance:
<div class="text-sm md:text-lg lg:text-xl">Responsive Text</div>
In this example, the text size will be sm
by default, it will change to lg
at the md
breakpoint (768px), and then to xl
at the lg
breakpoint (1024px).
If you want to customize these breakpoints, you can do so by modifying your tailwind.config.js
file:
module.exports = {
theme: {
screens: {
'tablet': '640px',
'laptop': '1024px',
'desktop': '1280px',
},
},
variants: {},
plugins: [],
}
In this example, we've defined custom names for our breakpoints: tablet
, laptop
, and desktop
. We can then use these in our HTML just like we would the default breakpoints:
<div class="text-sm tablet:text-md laptop:text-lg desktop:text-xl">Responsive Text</div>
References