The font-weight
property is used to set the thickness or boldness of a font. It can be defined using keywords like normal
and bold
, or with numeric values ranging from 100 to 900. Not all fonts have all nine weight values available.
Example 1: Using the bold
Keyword
/* This rule targets elements with the class "important". */
.important {
/* It makes the text bold. */
font-weight: bold;
}
Explanation
The bold
keyword is a common way to make text stand out. It is typically equivalent to a numeric value of 700
.
Example 2: Using the normal
Keyword
/* This rule targets all bold elements within a specific section. */
.some-section b {
/* It resets the font weight to normal. */
font-weight: normal;
}
Explanation
The normal
keyword sets the font weight to its default, non-bold state. This is equivalent to a numeric value of 400
.
Example 3: Using a Numeric Value
/* This rule applies to all h3 elements. */
h3 {
/* It sets a specific numeric font weight. */
/* A value of 600 is considered semi-bold. */
font-weight: 600;
}
Explanation
Numeric values provide more granular control over the font's thickness, assuming the font family supports different weights.
Example 4: Using a Lighter Font Weight
/* This rule targets elements with the class "subtle-text". */
.subtle-text {
/* It sets a lighter font weight. */
/* A value of 300 is a common light weight. */
font-weight: 300;
}
Explanation
Lighter font weights, like 300
, can be used to de-emphasize text or create a more delicate and modern look.
Example 5: Using the bolder
Keyword
/* This rule targets a nested strong element. */
p strong {
/* It makes the text bolder than its parent element. */
font-weight: bolder;
}
Explanation
The bolder
keyword makes the text one step bolder than the parent element's font weight.
Example 6: Using the lighter
Keyword
/* This rule applies to a span within a heading. */
h2 span {
/* It makes the text lighter than its parent element. */
font-weight: lighter;
}
Explanation
Similar to bolder
, the lighter
keyword makes the text one step lighter than the parent element's font weight.
Example 7: Specifying a Range for Variable Fonts
/* This rule targets a paragraph and assumes a variable font is being used. */
p {
/* It sets a specific weight within the font's available range. */
font-weight: 550;
}
Explanation
With variable fonts, you are not limited to increments of 100 and can specify any numeric value within the font's supported range.