Sometimes, different CSS classes try to change the same style in different ways. Class conflict resolution helps decide which style wins so your page looks right.
Class conflict resolution strategies in Tailwind
<!-- Tailwind classes in HTML element -->
<div class="class1 class2 class3">Content</div>hover: or md: applies styles only in certain states or screen sizes, helping avoid conflicts.text-blue-500 comes after text-red-500 and overrides it.<div class="text-red-500 text-blue-500">Hello</div>p-4.<div class="p-4 p-2">Box</div>hover: prefix.<button class="bg-green-500 hover:bg-green-700">Click me</button>md: prefix to resolve conflicts by screen size.<div class="text-sm md:text-lg">Text</div>This example shows how Tailwind resolves conflicts by applying the last class for the same property. The heading text is blue because text-blue-700 overrides text-red-500. The paragraph has smaller padding because p-2 overrides p-6. The button changes background color on hover using the hover: prefix.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Tailwind Class Conflict Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-red-500 text-blue-700 font-bold">Hello World</h1> <p class="p-6 p-2 bg-gray-100">This paragraph has padding conflict.</p> <button class="bg-blue-500 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded"> Hover me </button> </body> </html>
Order matters: Tailwind applies classes from left to right, so put the class you want to win last.
Use prefixes like hover:, focus:, or responsive prefixes like sm:, md: to avoid conflicts by targeting states or screen sizes.
Inspect elements in browser DevTools to see which styles are applied and which are overridden.
Tailwind resolves class conflicts by applying the last class that affects the same CSS property.
Use state and responsive prefixes to manage styles in different situations without conflicts.
Check your class order and use browser tools to debug style conflicts.
Practice
Solution
Step 1: Understand Tailwind class conflict behavior
Tailwind applies CSS classes in the order they appear in the HTML class attribute.Step 2: Identify which class overrides
The last class that sets the same CSS property overrides earlier ones.Final Answer:
The last class listed in the class attribute -> Option AQuick Check:
Last class wins = C [OK]
- Thinking the first class always applies
- Believing conflicting classes combine styles
- Assuming Tailwind errors on conflicts
Solution
Step 1: Understand responsive prefixes in Tailwind
Use prefixes likemd:to apply styles at specific screen sizes.Step 2: Apply red background by default and blue on medium screens
bg-red-500applies red always;md:bg-blue-500overrides it on medium screens.Final Answer:
<code>bg-red-500 md:bg-blue-500</code> -> Option CQuick Check:
Responsive prefix changes style at breakpoint = B [OK]
- Placing responsive prefix after the class
- Repeating same class without prefix
- Incorrect order causing no override
class="bg-green-400 bg-red-500 hover:bg-blue-600"Assume the user is not hovering.
Solution
Step 1: Identify conflicting background classes
There are two background classes:bg-green-400andbg-red-500.Step 2: Determine which class applies
Sincebg-red-500comes afterbg-green-400, it overrides the green background.Step 3: Consider hover state
User is not hovering, sohover:bg-blue-600does not apply.Final Answer:
Red background -> Option BQuick Check:
Last non-hover background class applies = A [OK]
- Choosing green because it appears first
- Choosing blue without hover
- Ignoring class order
class="text-lg text-sm md:text-xl text-md"But the text size is not what you expect on medium screens. What is the problem?
Solution
Step 1: Analyze class order and prefixes
The classes aretext-lg,text-sm,md:text-xl, andtext-mdin that order.Step 2: Understand how Tailwind applies classes
On medium screens,md:text-xlapplies, buttext-mdcomes after and overrides it because it has no prefix.Final Answer:
Thetext-mdclass overridesmd:text-xlbecause it comes last -> Option AQuick Check:
Unprefixed class after prefixed overrides = D [OK]
- Thinking md: prefix is invalid
- Believing Tailwind forbids multiple text sizes
- Ignoring class order importance
Solution
Step 1: Identify required styles and their order
Normal: green background (bg-green-500), hover: red (hover:bg-red-500), medium: blue (md:bg-blue-500), medium hover: red (md:hover:bg-red-500).Step 2: Check class order for correct overrides
The order must be normal green, then hover red, then medium blue, then medium hover red last to ensure correct overrides.Step 3: Verify
bg-green-500 hover:bg-red-500 md:bg-blue-500 md:hover:bg-red-500matches this orderbg-green-500 hover:bg-red-500 md:bg-blue-500 md:hover:bg-red-500lists classes in this exact order, so it resolves conflicts correctly.Final Answer:
<code>bg-green-500 hover:bg-red-500 md:bg-blue-500 md:hover:bg-red-500</code> -> Option DQuick Check:
Order: normal, hover, medium, medium hover = A [OK]
- Placing hover after responsive classes incorrectly
- Mixing order causing hover not to apply on medium
- Omitting md:hover class
Ready to prove it?
Pass the test and this pattern moves unknown
