Connect with us

CSS

How to Override CSS Styles: A Complete Guide

Spread the love

CSS (Cascading Style Sheets) is designed to style and control the layout of web pages. However, when multiple styles apply to the same element, you might need to override CSS rules to achieve the desired styling.

In this blog, we’ll explore different ways to override CSS styles, ensuring you have full control over your webpage design.

1. How Does CSS Decide Which Style Applies?

Before overriding styles, it’s important to understand CSS specificity and how browsers determine which style takes priority.

CSS Specificity Hierarchy

1️⃣ Inline styles (style="") → Highest priority
2️⃣ !important declaration → Overrides all styles (except another !important with higher specificity)
3️⃣ ID selectors (#id) → More specific than class or element selectors
4️⃣ Class selectors (.class) → More specific than element selectors
5️⃣ Element selectors (div, p, h1, etc.) → Lowest priority
6️⃣ CSS source order → If specificity is equal, the last declared rule applies

Understanding this hierarchy helps in effectively overriding styles.


2. Ways to Override CSS Styles

1. Using Higher Specificity Selectors

If a weaker rule is being overridden, increase its specificity.

Example: Override with ID Selector

🔴 Default style (not applying due to low specificity)

p {
    color: blue;
}

Override using an ID (higher specificity)

#custom-text {
    color: red;
}
<p id="custom-text">This text will be red.</p>

Why it works?
🔹 #custom-text (ID) is more specific than p (element selector), so it takes priority.


2. Using !important (Highest Priority Override)

The !important declaration forces a style to override all other rules.

Example: Override Background Color

🔴 Default CSS (being overridden)

body {
    background-color: blue;
}

Force Override with !important

body {
    background-color: red !important;
}

Why it works?
🔹 !important overrides all other styles, even inline styles (unless the inline style also has !important).

Use !important sparingly, as it makes debugging difficult and can lead to conflicts.


3. Using Inline Styles (Overrides External & Internal CSS)

Inline styles override both external and internal styles unless !important is used.

Example: Override Using Inline Style

<p style="color: green;">This text will be green.</p>

Even if you have this CSS:

p {
    color: blue;
}

💡 The inline style wins because it has the highest priority.


4. Overriding Styles Using More Specific Selectors

If two selectors have the same specificity, the last declared rule takes effect.

Example: Override a Class by Using Multiple Classes

.button {
    background-color: blue;
}
.button.primary {
    background-color: red;
}
<button class="button primary">Click Me</button>

🔹 Since .button.primary is more specific than .button, the button background becomes red.


5. Using the :where() and :is() Pseudo-Classes (Modern Approach)

The :where() function reduces specificity, making it easier to override.

Example: Overriding Styles with :where()

:where(.card) {
    background-color: blue;
}
.card {
    background-color: red;
}

🔹 .card wins because :where() has zero specificity.

The :is() function behaves normally with specificity.

Example: Using :is()

:is(.box, .container) {
    padding: 20px;
}

🔹 This applies to both .box and .container with normal specificity.


6. Using inherit, initial, and unset to Reset Styles

If an inherited style is causing conflicts, you can reset it using:

PropertyDescription
inheritInherits the value from the parent element
initialResets the property to the default browser value
unsetRemoves any applied styles (acts as inherit for inherited properties and initial otherwise)

Example: Reset a Specific Element’s Style

p {
    color: red;
}
.reset {
    color: initial; /* Resets to default black */
}
<p class="reset">This text resets to the default color.</p>

3. Best Practices for Overriding CSS

Use more specific selectors instead of !important to maintain code clarity.
Keep CSS organized to avoid unnecessary overrides.
Use :where() for easy overrides without increasing specificity.
Minimize inline styles—they are hard to manage and debug.
Use CSS preprocessors like SASS/LESS for better control over styles.


4. Conclusion

Overriding CSS effectively ensures your styles are consistent, maintainable, and optimized.

Quick Recap of Methods to Override CSS Styles:

Increase specificity (use IDs, multiple classes, and element combinations).
Use !important as a last resort for forceful overrides.
Utilize inline styles for one-time changes.
Use inherit, initial, or unset to reset styles.
Leverage :where() and :is() for better control over specificity.

By following these techniques, you’ll have full control over CSS overrides while keeping your stylesheets clean, efficient, and easy to manage.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *