How to use !important in TailwindCSS
<div class="!bg-red-600">Lorem ipsum</div>
In CSS, try to avoid the use of the !important modifier if possible. However, sometimes it is still necessary. TailwindCSS has a simple way to do this. Just add the !
character to the beginning of the selected class. Example:
<div class="!bg-red-600">Lorem ipsum</div>
When can it be useful?
Imagine you have a React button component whose code you can't change and its color is set to bg-sky-600
:
<button class="bg-sky-600">Test button</button>
If the component allows you to add extra classes, you can override the button color to do so:
<button class="bg-sky-600 bg-red-600">Test button</button>
The button still remains blue, because the bg-sky-600
is defined later than the bg-red-600
.
However, if you use the !
prefix for the class bg-red-600
, Tailwind will automatically create a new class using the !important keyword and the button will be displayed in red.
- Create blinking background with TailwindCSS
- Create a neon effect with TailwindCSS
- Rotate element with TailwindCSS
- Change SVG icon color with TailwindCSS
- Using :not in TailwindCSS
- Specify exact width value in TailwindCSS
- Set element width over 100% using Tailwind CSS
- Stretch children to fill horizontally
- Create a fixed navbar with TailwindCSS
- Use font from local files in TailwindCSS
- Make an element invisible in mobile size in Tailwind CSS
- Create a 20/80 grid with Tailwind CSS
- Select the nth child in TailwindCSS
- Add class to child when hovering on parent in TailwindCSS
- Fill the full height of the screen in TailwindCSS
- Create transition with TailwindCSS
- Create horizontal rule with text using TailwindCSS
- Set background image with TailwindCSS
- How to use CSS variables in TailwindCSS
- Center an absolute element in TailwindCSS
- Remove arrows from number input in TailwindCSS
- How to use !important in TailwindCSS
- Select first child in TailwindCSS
- Access child elements in TailwindCSS
- Create semi transparent background in TailwindCSS
- How to use calc() in TailwindCSS
- Add text shadow with Tailwind CSS