CSS is the most important part of web design. It allows web pages to present various styles and layouts. One of the most basic CSS properties is font, which is the font property. By setting font, we can control the size, color, font and other related attributes of the text in the web page.
The following are some tips and knowledge points on how to set font, which can help you better master this commonly used CSS property.
In CSS, the font-size attribute is usually used to set the size of text. Its value can be pixel (px), percentage (%), em, rem, etc. Generally speaking, pixels, as absolute units of length, are more suitable for fixed-size layouts. For example, you can set the font size of the h1 element to 36 pixels:
h1 {
font-size: 36px;
}
while percentages and em etc. are more suitable for relative length units and scalable Designed website. For example, you can set the font size of a paragraph element to 1.2 times:
p {
font-size: 120%;
}
In addition to font size, font (font-family) is another important attribute. By setting font-family, we can change the default font of text. Typically, there are multiple fonts that may be used on a web page. Therefore, when setting font attributes, you can separate multiple fonts with commas to ensure that visitors can correctly render the fonts on the web page. For example:
body {
font-family: "Microsoft YaHei", "SimHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
In this example, if the first font "Microsoft YaHei" is not available on the visitor's computer, it will be tried in sequence. other fonts until the web page renders correctly.
The font-weight property can be used to set the thickness of the font. Its value can be normal (default), bold (bold), bolder (bolder), lighter (lighter), 100-900 (the larger the number, the bolder the font), etc.
For example, to bold the font of the h2 element:
h2 {
font-weight: bold;
}
The font-style attribute can be used to set whether the text appears in italics , its value can be normal (default), italic (italic), oblique (oblique), etc.
For example, to set a paragraph of text to italics:
p {
font-style: italic;
}
Line-height refers to the vertical distance between text lines. Its default value is normal, which is specified by the font itself. By setting the line-height property, we can control the distance between lines, thereby affecting the overall layout effect of the paragraph.
For example, set the line height of the h3 element to 1.6 times:
h3 {
line-height: 1.6;
}
In short, by carefully mastering the font attribute, We can easily adjust various aspects of font size, color, font, weight and line height to create a beautiful and unique web design effect.
The above is the detailed content of How to set font using css. For more information, please follow other related articles on the PHP Chinese website!