5๐ Tailwind CSS Secrets Revealed: Deeper Dives Into Its Coolest Features!
5 Hidden Tailwind CSS Features You Need to Know
Hey everyone, it's your friend TAQUI here! Today I'm going to share some super sweet Tailwind CSS secrets with you. As you guys know, Tailwind is awesome for building fast sites but there are hidden features that take it to the next level.
Are you ready to uncover them? Then let's dive in!
Tip #1: Layout Classes Make Design a Breeze
If you are familiar with Tailwind then you know about Tailwind's layout classes like w-
for width and h-
for height. But did you know you can get even more specific?
Tailwind has responsive width utilities too. For example:
Class | Width |
sm:w-1/2 | 50% width on small screens and up |
md:w-1/3 | 33.33% width on medium screens and up |
lg:w-1/4 | 25% width on large screens and up |
You can also include max widths:
Class | Max Width |
max-w-sm | 640px |
max-w-md | 768px |
max-w-lg | 1024px |
max-w-xl | 1280px |
max-w-2xl | 1536px |
And min widths work the same as max widths but with min-
instead of max-
.
This allows you to control element sizes at different breakpoints easily! ๐คฏ
Here's an example layout using these:
<div class="flex">
<aside class="w-1/4 lg:w-1/5 h-screen hidden lg:block bg-gray-100">
<!-- sidebar -->
</aside>
<main class="w-3/4 lg:w-4/5">
<article class="max-w-2xl mx-auto">
<!-- content -->
</article>
</main>
</div>
The sidebar is hidden on small screens but shown and sized on larger screens. And the content is constrained to max-width for readability. Sweet right?! ๐
Tip #2: Breakpoints for Advanced Responsive Design
Last time I mentioned using breakpoints like sm:
, md:
etc to control styles. But Tailwind breakpoints are way more powerful than that!
First, you can customize the breakpoints themselves. In your tailwind.config.js
:
theme: {
screens: {
sm: '480px',
md: '768px',
lg: '976px',
xl: '1440px'
}
}
Now every breakpoint will use these sizes instead of the defaults.
You can also target multiple breakpoints like:
<div class="md:block lg:hidden">
<!-- hidden on medium but shown on large -->
</div>
Or target breakpoints conditionally:
<div class="lg:hidden md:block">
<!-- shown on medium, hidden on large -->
</div>
This is incredibly powerful for nuanced responsive designs ๐คฏ
Tip #3: Easily Customize Components
Remember how we customized buttons by modifying the theme? Well Tailwind makes it super easy to customize literally any component.
For example, to tweak all inputs:
// tailwind.config.js
theme: {
borderRadius: {
input: '0.25rem'
},
borderWidth: {
input: '1px'
},
// etc
}
Now all <input>
with rounded-input
and border-input
will look nicer out of the box.
You can also modify global styles:
theme: {
spacing: {
'8': '2rem'
},
backgroundColor: {
primary: '#3490dc'
}
}
Now mb-8
= 2rem and bg-primary
references that blue.
The possibilities are endless! ๐คฏ
Tip #4: Plugins Extend Capabilities
Plugins add whole new sets of styles and components to Tailwind. For example, the Forms plugin mentioned before gives great form styles like:
<label
class="block text-gray-700 text-sm font-bold mb-2"
for="username"
>
Username
</label>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="username"
type="text"
placeholder="Username">
</input>
This looks awesome without writing any CSS! ๐คฏ
But plugins can do way more. Check out these other great ones:
Tailwind CSS Typography - Advanced typographic styles
Hero Icons - Icon component library
Flowbite - UI components like buttons, modals etc
Tailwind CSS Plugins - Official plugins for all things like forms, lines, containers etc
The ecosystem allows you to rapidly build entire interfaces! ๐ฅ
Tip #5: Customizing and Optimizing Output
You know how we removed unused text sizes to optimize the final CSS bundle? Well here are some other Tailwind tips:
Remove unused utilities from purge list like
@apply text-xs
Disable unneeded breakpoint styles with
theme.screens
Group related utilities for better caching
Purge styles on a per page basis for minimal bundles
Lazy load only used utilities for initial paint
And you can customize literally anything to match your exact needs!
For example, say you want larger than normal line heights. Add to theme:
// tailwind.config.js
theme: {
lineHeight: {
'9': 1.5
}
}
Now all leading-9
elements have nice breathing room. ๐
The possibilities are endless! Tailwind gives you full control.
I hope these secrets help take your Tailwind skills to a whole new level! Please let me know if any other questions come up. Until next time, keep building awesome things! ๐
Like ๐
Share๐
Follow me in Hashnode โ