3

How can I override an inline CSS rule with using an external stylesheet file?

This is my HTML code:

<div class="mydiv" style="background:#000"> lorem ipsom</div>

I want to change the background color using CSS. This is my CSS code:

.mydiv {background:#f00; color: #000;}

But this is not working, but I this is possible.

Is there a way to change the background color in Internet Explorer?

5 Answers 5

6

This is very simple. Use !important after your rule style. Here is the example:

.mydiv {background:#f00 !important; color: #000;}

URL: http://jsfiddle.net/msJxL/

And for Internet Explorer, check out How To Create an IE-Only Stylesheet | CSS-Tricks.

Sign up to request clarification or add additional context in comments.

2 Comments

also plz let me know show can i change color in a single style shee, the link u send me is for external ie css which i have to attach in my document but the problem is i cannot edit my html document
Check your spelling @Vipul ... it's "simple" not simpale... since it's the accepted answer I thought it would be nice if it was cleaner.
6

Inline style is treated as having a higher specificity than any rule-set.

The only ways to override it are to change it on the element or use an !important rule.

!important rules are a sledgehammer of a solution and only work once (if you want to override again, you are stuck; there is no such thing as a double !important rule), so changing the style attribute value (preferably removing it entirely in favour of a stylesheet) is the best option.

If you really want to use !important then the syntax is:

.mydiv {
    background:#f00 !important;
    color: #000;
}

Comments

1

Use the !important for this. It will override other CSS. Try the following code:

.mydiv {background:#f00 !important; color: #000;}

Comments

0

Use this:

.mydiv {
     background: #f00 !important;
     /* This will increase the rule score */
     color: #000;
}

Detailed information: Stack Overflow question How can I override inline styles with external CSS?.

Comments

-1

You can use the CSS attribute selector:

 <style>
     div[style] {
        background: blue !important;
     }
 </style>

 <div style="background: red;">
     The inline styles.
 </div>

1 Comment

Attribute selectors have nothing to do with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.