Help - Search - Members - Calendar
Full Version: Precedence for font-size attribute in a class?
HTMLHelp Forums > Web Authoring > Cascading Style Sheets
Woody20
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)?
Darin McGrew
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
Woody20
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.
Darin McGrew
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.
Christian J
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.)
pandy
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.
Christian J
Sorry, misunderstood!
pandy
I don't know. I'm just guessing. blush.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.