Responsive utility overrides let you change styles on different screen sizes easily. This helps your website look good on phones, tablets, and desktops.
Responsive utility overrides in Tailwind
sm:<utility> md:<utility> lg:<utility> xl:<utility> 2xl:<utility>Use prefixes like sm:, md:, lg: to apply styles at different screen widths.
These prefixes override the base utility styles when the screen is at least that size.
<div class="text-center md:text-left">Hello</div><button class="p-2 lg:p-4">Click me</button><img class="hidden sm:block" src="image.jpg" alt="Example image">
This page shows a heading and a button that change size based on screen width. The heading is centered on small screens and left-aligned on medium and bigger screens, becoming very large on large screens. The button text size also grows on bigger screens. The paragraph text aligns center on small screens and left on bigger screens.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Utility Overrides Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-center text-sm md:text-left md:text-lg lg:text-3xl font-bold mb-4"> Responsive Heading </h1> <button class="bg-blue-500 text-white px-4 py-2 rounded text-xs sm:text-sm md:text-base lg:text-lg"> Responsive Button </button> <p class="mt-4 text-gray-700 text-center sm:text-left"> Resize the browser window to see text alignment and size change. </p> </body> </html>
Always test your responsive styles by resizing the browser or using device simulation in browser DevTools.
Use meaningful class names and keep your HTML clean by combining base and responsive utilities.
Remember that responsive prefixes apply styles only if the screen is at least that size.
Responsive utility overrides let you change styles for different screen sizes easily.
Use prefixes like sm:, md:, lg: before utilities to apply them at certain screen widths.
This helps make your website look good and work well on phones, tablets, and desktops.