Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Cascading Style Sheets _ Precedence for font-size attribute in a class?

Posted by: Woody20 Mar 13 2012, 12:39 AM

I have an unusual question, arising from a pgm which reads RTF and outputs HTML.

The pgm is outputting HTML like

CODE
<style>
.jus{ text-align: justify; }
.sz10{ font-size: 10pt; }
.sz17{ font-size: 17pt; }
.f0{ font-family: Times New Roman, Times, serif; }
.f32{ font-family: Verdana, sans-serif; }
</style>
<p class="f0 sz17 cl1 jus sz10" style="text-indent: 12pt;">
<span class="f32">Some sample text</span>

The intent is that "Some sample text" is 10-point Verdana, but it appears as 17-point. It looks like the <p> class gives the first definition of font-size (i e sz17) precedence over the second (sz10).

What I want to know is, is there a rule for which size will take precedence (first, last, or something else)? And is this rule, if there is one, standard HTML, or different among browsers?

A related question: would the same rule apply for other attributes (e g, font-family, justify vs left, etc)?

Posted by: Darin McGrew Mar 13 2012, 02:34 AM

The p element has 2 font-size properties. The two selectors have the same specificity, so the one specified later wins. Since the rule for the .sz17 selector appears after the rule for the .sz10 selector, the rule for the .sz17 selector wins.

Is there a reason to include both the sz10 and sz17 class? Only one can have any affect, so you should just omit the unused one.

See also:
http://www.w3.org/TR/CSS2/cascade.html#cascading-order

Posted by: Woody20 Mar 13 2012, 12:45 PM

QUOTE(Darin McGrew @ Mar 13 2012, 02:34 AM) *

Is there a reason to include both the sz10 and sz17 class? Only one can have any affect, so you should just omit the unused one.
The output is being produced by a pgm, not a person. What is desired is for the one appearing later in the class to dominate, so I guess I will have to add code to remove the earlier, undesired one from the list.

Posted by: Darin McGrew Mar 13 2012, 12:53 PM

QUOTE
What is desired is for the one appearing later in the class to dominate
Yeah, CSS doesn't work that way. The order the classes appear in the class attribute is irrelevant. It's the order the selectors appear in the style sheet(s) that matters.

Posted by: Christian J Mar 13 2012, 02:09 PM

You might give the first rule higher specificity with the !important keyword:

CODE
.sz10{ font-size: 10pt !important; }
.sz17{ font-size: 17pt; }

or by adding the element selector to the rule:

CODE
p.sz10{ font-size: 10pt; }
.sz17{ font-size: 17pt; }


BTW, the pt unit is meant for printed media. For screen font sizes you should use percent or em. (This used to matter in older browsers, don't know if there are still any practical consequences of using pt with screen media.)

Posted by: pandy Mar 13 2012, 02:37 PM

If editing the style sheet is a workable option it would be enough to reorder the rules. But I guess it isn't always the same class that is desired to take precedence. If it's is, this is pretty much a non-issue.

Posted by: Christian J Mar 13 2012, 03:38 PM

Sorry, misunderstood!

Posted by: pandy Mar 13 2012, 03:54 PM

I don't know. I'm just guessing. blush.gif

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)