Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

!important is not overriding inline CSS

Tags:

css

I have some inline CSS I can't change

<span style = "color: #404040; font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 12px; line-height: 17px;">

stuff

</span>

and it is overriding my external CSS. I tried putting !important

.product-description 
{
  font-family:wide latin !important;
}

but it had no effect

If I go to inspect element and delete the style attribute, my external CSS works

like image 937
abcdefg4321 Avatar asked Jul 22 '16 19:07

abcdefg4321


People also ask

Can important override inline CSS?

Using ! important and inline style. Adding the ! important keyword to any CSS rule lets the rule forcefully precede over all the other CSS rules for that element. It even overrides the inline styles from the markup.

How do you override inline CSS without important?

The only way to override a CSS rule without using ! important is to use a more specific selector. No selector is more specific than the style attribute.

Why We Should not Use inline CSS?

Inline styles, while they have a purpose, generally are not the best way to maintain your website. They go against every one of the best practices: Inline styles don't separate content from design: Inline styles are exactly the same as embedded font and other clunky design tags that modern developers rail against.

How do I override inline styles in CSS?

Inline CSS will, as a general rule, override any CSS in your stylesheet assuming the same level of specificity. This reply has been reported for inappropriate content. The only way to override inline styles is using !important in your stylesheet, unless… the inline style already has !important. Short answer: impossible.

How to override the ‘important’ rule in CSS?

In a specificity tie, the last defined rule wins. The first is the lowest, whereas the third is the highest. You can add the same selector after the existing one. Let’s see step by step how to override the !important rule. Use a <div> element with the class, id, and style attributes. Add !important to the style attribute.

Why are you using “!important” in inline CSS?

Why are you using `!important`? You can’t double up on it and make it “stronger”. Inline CSS will, as a general rule, override any CSS in your stylesheet assuming the same level of specificity. Not impossible: you can override it by adding another inline style after that:

How do I override all other declarations in CSS?

An !important declaration will override all other declarations, whether linked, blocked, or inline…except for any other !important declarations that fall below! Here is the spec’s explanation of it. Try it yourself! style.css: p { color: red !important; }


1 Answers

Update 1

OP mentioned they only have access to the CSS file.

In this case, you will need to change your CSS selector up a bit but is still doable. The example below has the class applied to the parent element of an element you'd like to change.

.change p {
  color: pink !important;
}
<div class="change">
  <p style="color: blue;">
    This is some text.
  </p>
</div>

You might have to get even more specific with your CSS selector if there are a lot of child elements to wade through when you hook into a CSS class selector. Try to hook into the CSS class selector that is the closest to the element you want to target.

.change div p:nth-child(2) {
  color: pink !important;
}
<div class="change">

  <p style="color: blue;">
    This is some text.
  </p>

  <div>

    <p style="color: green;">
      This is some text.
    </p>

    <p style="color: orange;">
      This is some text. (only change me)
    </p>

  </div>

</div>

Original Answer

My guess is that you're not applying the CSS class directly to the element you want changed as I do not see .product-description being applied to the example <span>.

Look at the two examples below.

  1. I think you're attempting this one, class applied to outer element of the element you want changed. <p> would inherit the color of .change if <p> didn't have something with a higher specificity applied to it, like inline CSS or another CSS class.

  2. Here we apply the class directly to the element we want changed, that way the specificity of !important can override the specificity of the inline CSS.

.change {
  color: pink !important;
}
<!-- #1 -->
<div class="change">
  <p style="color: green;">
    This is some text.
  </p>
</div>

<!-- #2 -->
<div>
  <p class="change" style="color: green;">
    This is some text.
  </p>
</div>
like image 154
hungerstar Avatar answered Oct 20 '22 15:10

hungerstar