33

I have the following code and for some reason the !important qualifier isn't working.

<div style="font-family : calibri; font-size: 20pt !important;">
  <ul>
    <li>
      <span style="font-size: 11px;">
        <span style="font-size: 11px;">
          Honey Glazed Applewood Smoked Spiral Ham 
        </span>
        <span style="font-size: 11px;">
          Served with Dijon Honey Mustard and Turkey Roulade
        </span>
      </span>
    </li>
  </ul>
</div>

The span tags are generated for website formatting. I was adding the div tag to change the output to PDF format instead of writing a seemingly overcomplicated find and replace function. Since this hack is for specific regions of code, I can't change the CSS sheet.

Any thoughts would be appreciated.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Jim
  • 3,425
  • 9
  • 32
  • 49
  • This is CSS rather than HTML. I retagged it but hope you could change the title. – Ryan Li Jan 06 '11 at 16:11
  • @Ryan Thanks, coffee hasn't kicked in yet. Changed. – Jim Jan 06 '11 at 16:13
  • This is a good example of why you shouldn't use in-line styles: they can be quite hard to override when you need to. Ideally the s should have a class which is styled to 11px in a separate CSS. If your
    also had a class or ID, it would then be easy to override the spans.
    – Spudley Jan 06 '11 at 17:46

3 Answers3

34

Give the <div> an id and then add this rule to your CSS stylesheet (or in a <style> tag if you don't want to change the style sheet):

#your_div_id span {
    font-family : calibri; font-size: 20pt !important;
}

!important in CSS allows the author to override inline styles (since they have a higher precedence than style sheet styles normally). It doesn't automatically make the style marked !important override everything else.

SEE: The W3C's documentation on CSS Selector Specificity.
Felix's Demo of the markup

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
26

A good subject to read up on is CSS Specificity

  1. p has a specificity of 1 (1 HTML selector)
  2. div p has a specificity of 2 (2 HTML selectors, 1+1)
  3. .tree has a specificity of 10 (1 class selector)
  4. div p.tree has a specificity of 12 (2 HTML selectors + a class selector, 1+1+10)
  5. #baobab has a specificity of 100 (1 id selector)
  6. body #content .alternative p has a specificity of 112 (HTML selector + id selector + class selector + HTML selector, 1+100+10+1)
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
diagonalbatman
  • 17,340
  • 3
  • 31
  • 31
0

my issue was that I was assigning a class using innerHTML and a class added to innerHTML is ignored. To fix it I referred to this

Apurva Thorat
  • 641
  • 5
  • 10