I was tasked with removing unnecessary tags from computer-generated html that had a lot of useless tags (I only want to keep color/strong/em information). I came along something similar to this html:
<b>
<span style="FONT: 20pt "Arial"">
<strong>bold</strong> not bold <b>bold</b> not bold
</span>
</b>
For me (on chrome & firefox), it shows the bold
text as bold and the not bold
text as not bold, and I am confused to why this is. In particular, this makes my task more complicated.
I tried to find out what I could put in the FONT
style to reproduce this behaviour:
Replacing Arial
with foo
kept the behaviour:
<b>
<span style="FONT: 20pt foo">
<strong>bold</strong> not bold <b>bold</b> not bold <!-- not bold is actually not bold! 20pt is applied -->
</span>
</b>
Switching the size and font changed the behaviour:
<b>
<span style="FONT: "Arial" 20pt">
<strong>bold</strong> not bold <b>bold</b> not bold <!-- everything is bold. 20pt is _not_ applied -->
</span>
</b>
Any of the two values on their own did nothing much:
<b>
<span style="FONT: "Arial"">
<strong>bold</strong> not bold <b>bold</b> not bold <!-- everything is bold -->
</span>
</b>
<b>
<span style="FONT: 20pt">
<strong>bold</strong> not bold <b>bold</b> not bold <!-- everything is bold -->
</span>
</b>
<b>
<span style="FONT: 20pt "Arial"">
<strong>bold</strong> not bold <b>bold</b> not bold
</span>
</b>
<div>Replacing `Arial` with `foo` kept the behaviour:</div>
<b>
<span style="FONT: 20pt foo">
<strong>bold</strong> not bold <b>bold</b> not bold
<!-- not bold is actually not bold! 20pt is applied -->
</span>
</b>
<div>Switching the size and font changed the behaviour:</div>
<b>
<span style="FONT: "Arial" 20pt">
<strong>bold</strong> not bold <b>bold</b> not bold
<!-- everything is bold. 20pt is _not_ applied -->
</span>
</b>
<div>Any of the two values on their own did nothing much:</div>
<b>
<span style="FONT: "Arial"">
<strong>bold</strong> not bold <b>bold</b> not bold
<!-- everything is bold -->
</span>
</b>
<b>
<span style="FONT: 20pt">
<strong>bold</strong> not bold <b>bold</b> not bold
<!-- everything is bold -->
</span>
</b>
Can anyone explain this behaviour, or at least tell me what syles I should look for that cancel outer styles?
CodePudding user response:
Fonts depend on your actual browser and actual font. ref: https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
Fonts have a particular weight which corresponds to one of the numbers in Common weight name mapping. However some fonts, called variable fonts, can support a range of weights with a more or less fine granularity, and this can give the designer a much closer degree of control over the chosen weight.
WHICH of the copyrighted or not fonts do you have? Depends. Browser, computer, CSS etc. but we cannot tell here.
Always specify a fallback (sans-serif
in this case) This can all get a bit "tricky".
Bottom line: put all this in CSS and use classes so you can actually control it best and less HTML overall when you decide to change things Like pt
is not normally great for visual but is for printed but not used much in modern HTML as a size. Now if you decide to change the size you have a lot of places to change it but if in CSS only one.
Now for some examples: (not all inclusive of the topic by any means)
.container {
font-weight: normal;
margin: 1em;
}
.container .fonty {
font-size: 20pt;
font-family: Arial, Helvetica, Verdana, "Gill Sans", sans-serif;
}
.heavy-one {
font-weight: 600;
}
.heavy-two {
font-weight: 900;
}
<div >
<span >
<span >bold</span> not bold <span >bolder</span> not bold
</span>
</div>
<div >
<span >
<span >bold</span> not bold <span >bolder</span> not bold
</span>
</div>
<p>One with style in tags: (don't do this)
<span>
<span style="font-weight: normal; font-size: 20pt; font-family: 'arial', 'Arial', sans-serif;">
<span style="font-weight: bold;">bold</span> not bold <span style="font-weight: bold;">bold</span> not bold</span>
</span>
</p>
<p>Second with style in tags: (don't do this, bolder not great IMHO as it is one step heavier)</p>
<span style="font-weight: normal; font-size: 20pt; font-family: 'arial', 'Arial', sans-serif;">
<span style="font-weight: bold;">bold</span> not bold <span style="font-weight: bolder;">bold</span> not bold</span>
<p>Third with style in tags and <strong>: (don't do this) This also shows multiple similar font families and the fallback to "sans-serif" you should always include</p>
<span style="font-weight: normal; font-size: 20pt; font-family: Arial, Helvetica, Verdana, sans-serif;"><strong>bold</strong> not bold <strong>bold</strong> not bold</span>
CodePudding user response:
I think I found the answer to my question, on the font css property documentation page. It states:
As with any shorthand property, any individual value that is not specified is set to its corresponding initial value (possibly overriding values previously set using non-shorthand properties). Though not directly settable by font, the longhands font-size-adjust and font-kerning are also reset to their initial values.
(My emphasis)
And a bit further down:
Initial value as each of the properties of the shorthand:
- font-style: normal
- font-variant: normal
- font-weight: normal
- font-stretch: normal
- font-size: medium
- line-height: normal
- font-family: depends on user agent
So setting font: 20pt arial
is equivalent to setting font-style: normal;font-variant: normal;font-weight: normal;font-stretch: normal;font-size: 20pt;line-height: normal;font-family: arial
In particular, the font-weight
is reset from bold
(or whatever it was) to normal
.
So to solve my underlying question, I should look for font
tags that do not specify the weight.
P.S. The reason that font: arial 20pt
did not have this behaviour is because this is not an allowed value for font
, so it is ignored:
If font is specified as a shorthand for several font-related properties, then:
- it must include values for:
<font-size>
<font-family>
font-style
,font-variant
andfont-weight
must precedefont-size
font-family
must be the last value specified.
And in font: arial 20pt
the font-family is not the last value specified.