I have a custom font being downloaded from a s3 bucket. I’m setting its weight accordingly to the default weight the font has like so:
@font-face{font-family:XXX;font-weight:400;font-style:normal;}
but the user agent stylesheet is overriding its font-weight when I use it in an h1 tag with font-weight: bold.
How can I prevent it?
searching in the docs to find a CSS rule to make the h1 behavior let the default font-weight as it is
2
Answers
Add a CSS rule to overwrite the bold font-weight setting every browser applies by default, similar to this:
This should make all h1 tags use the regular font-weight setting you defined in your @font-face rule.
If any h1 rule in your stylesheet is more specific and overrules this, you might want to add
!important
Responding to the OP’s comment
A
@font-face
can map any specific font file to anyfont-family
,font-weight
orfont-style
.In other words: the browser doesn’t care about the "intrinsic" properties of a font file – although a font actually contains info about the intended weight and style. You can inspect a font file e.g via an online tool like wakamaifondue
Map font file to font-family/font-weight/font-style
Here’s an example mapping a light/thin Montserrat to a bold font weight – even to another font-family (Open Sans).
Although, the loaded Montserrat font should be used as a "Montserrat light italic" – we can easily map the applied fonts as we like.
User agent styles are just fallbacks
They don’t override anything.
This is an important difference. In contrast to overridden style rules e.g caused by a main style sheet, theme or "framework" (like bootstrap) – a user agent default doesn’t introduce higher specificities.
As already commented, a font face rule doesn’t apply a font-family to specific elements. Besides, some elements don’t inherit font families from their parent elements – for instance form elements like
<input>
.So we need to be more specific: