Home >Web Front-end >CSS Tutorial >How do you control the font weight using the font-weight property?
The font weight of text can be controlled using the CSS font-weight
property. This property specifies the weight or boldness of the font, which can range from thin to thick. To use this property, you simply assign a value to it within your CSS rule. For example, if you want to set the font weight of a paragraph to bold, you would write:
<code class="css">p { font-weight: bold; }</code>
This will make all text within <p></p>
tags bold. The font-weight
property can be applied to any element that contains text, allowing you to control the text's appearance precisely across your webpage.
The font-weight
property accepts several values that allow you to adjust the boldness of text. Here are the possible values:
Keywords:
normal
: This is the default value and represents the normal weight of the font.bold
: This value represents a bold weight of the font.lighter
: This value specifies a weight lighter than the inherited value.bolder
: This value specifies a weight bolder than the inherited value.Numeric values:
100
, 200
, 300
, 400
, 500
, 600
, 700
, 800
, 900
: These are numeric weights that represent a scale from thin (100) to heavy (900). The value 400
is equivalent to normal
, and 700
is equivalent to bold
.You can choose the most suitable value based on the design requirements of your webpage. Keep in mind that not all fonts support all these weights; the exact appearance can vary depending on the font family being used.
The font-weight
property directly impacts the visual weight or thickness of the text on a webpage, which in turn affects its overall appearance and readability. Here’s how it influences the text:
Yes, the font-weight
property can be combined with other font properties to achieve more comprehensive and refined text styling on your webpage. Here’s how you can use it with other properties:
font-size
: You can adjust the size of the text along with its weight. For example, larger text with a lighter weight can still be prominent without being overwhelming.
<code class="css">h1 { font-size: 2em; font-weight: 300; }</code>
font-style
: This property allows you to set the text to italic, oblique, or normal. Combining this with font-weight
can create varied and dynamic text styles.
<code class="css">.emphasized { font-weight: bold; font-style: italic; }</code>
font-family
: Different fonts support different weights. By choosing a font family that offers a wide range of weights, you can create more subtle variations in your text.
<code class="css">body { font-family: 'Roboto', sans-serif; font-weight: 400; } h2 { font-family: 'Roboto', sans-serif; font-weight: 700; }</code>
line-height
: This can be used to adjust the space between lines of text, which can enhance readability when combined with different font weights.
<code class="css">p { font-weight: normal; line-height: 1.5; }</code>
By carefully combining font-weight
with these and other properties, you can achieve a sophisticated and visually appealing text layout on your webpage.
The above is the detailed content of How do you control the font weight using the font-weight property?. For more information, please follow other related articles on the PHP Chinese website!