English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
CSS font properties allow you to set various styles for fonts, such as the font family, size, boldness, variants, etc.
In CSS styles for text content, such as providing several properties:font-family
,font-Font style
,font-variant
,font-weight
andfont-size
. The next section will introduce these properties one by one.
Thefont-family
CSS property allows you to set the font family name of the text content, the priority list of font usage.
Thefont-family
Properties can contain multiple font names asFallbackFont. First list the font you want, followed by all fonts (if not available). If the browser does not support the first font, it will try the next one. A font family declaration may look like this:
p { font-family: "Times New Roman", Times, serif; }Test See‹/›
Note:If the name of the font family exceeds one word, it must be enclosed in quotes, like"Times New Roman"
,"Times New Roman"
,"Courier New"
"Trebuchet MS"
etc.
throughfont-Font style
style
property sets the font style of the element's text content.normal
,At first glance, oblique and italic styles may look the same, but they are different. The
The possible values of this property are:font, and
.
or font-p.one { } style: normal; font-p.two { } style: italic; font-p.three { }Test See‹/›
style: oblique;Note:At first glance, oblique and italic styles may look the same, but they are different. The
italicstyle useditalic versionfont, and
obliqueOn the other hand, text is simply aItalic version
font-size
Font size
property to set the font size of the element's text content.
Using keyword to set font sizex-small
,medium-small
,small
,By setting a keyword font size on the body element, you can set relative font sizes on other parts of the page, thereby easily scaling the font throughout the page. Specify an absolute size using one of the following keywords:
,xx
,medium-xx
,x-xx
.
largeRelative sizes are specified using one of the following keywords:
smaller、、
.
body { font-larger } h1 { font-size: large; } p { font-size: larger; }Test See‹/›
Using pixels to set font size12When pixel accuracy is needed, use pixel values (such as16px,
h1 { font-size: 24px, etc.) is a good choice for setting font size. However, the results may vary from browser to browser because they use different algorithms to achieve similar effects. } p { font-size: 14px, etc.) is a good choice for setting font size. However, the results may vary from browser to browser because they use different algorithms to achieve similar effects. }Test See‹/›
px;
Tip:Font size can be set by defining font size in pixels, but this method is not very flexible because users cannot change the font size through browser settings. For example, users with poor vision may want to set the font size to be larger than the size you specify. Therefore, if you want to create a design that includes text, you should avoid using pixel values for font size.can be usedZoom tool
em
Using Em to set font size
em
The size of the value is dynamic. The unit refers to the font size of the parent element.font-size
property, an em
equals the font size applied to the parent element of the element.
Iffont-size
Set a as20px, then1em=20px
and2em=40px
.
If you have not set the font size anywhere on the page, it is the default value of the browser, which is16px. Therefore, it is the default value of the browser, which is1em=16px
and2em=32px
.
h1 { font-size: 2em; /* 32px/16px=2em */ } p { font-size: 0.875em; /* 14px/16px=0.875em */ }Test See‹/›
Warning: IE6and IE7enlarged the size of the text adjusted after the size adjustment. (Test it last before publishing).
to achieve a similar effect in all browsersfont-size
To set the default percentage for the body element. One popular technique isfont-size
set the body's to62.5%
(i.e. default16px of62.5%),equal to10px or 0.625em.
Now, you can setfont-size
By converting any element using em units, it has an easy-to-remember conversionpx
through10values are determined in this way10px=1em
,12px=1.2em
,14px=1.4em
,16px=1.6em
and so on. See the following examples:
body { font-size: 62.5%; /* font-size 1em = 10px */ } p { font-size: 1.6em; /* 1.6em = 16px */ }Test See‹/›
Tip:inWorld Wide Web Consortium (W3C)It is recommended to use EM or percentage (%) values to create more powerful, scalable layouts.
font-weight
The property specifies the thickness or boldness of the font.
font-The possible values of the style property are:normal
,bold
,bolder
,lighter
,100
,200
,300
,400
,500
,600
,700
,800
,900
andinherit
.
Numerical100
- 900
specified font weight, where each number represents a darker weight than its predecessor.400
withnormal
&700
the samebold
.
ofbolder
andlighter
, while other values are absolute font weights, which are relative to the inherited font thickness.
p { font-weight: bold; }Test See‹/›
Since most fonts can only be used in limited weights. Usually, they are only available inNormalandBold Display. If the font is not available in the specified weight, a substitute font will be selected, which is the closest available weight.
font-variant
The property allows text to be displayed in a special uppercase letter form.
Lowercase Uppercase Letters orLowercase Uppercase Lettersslightly different from the normal uppercase letters, in which lowercase letters are displayed as a smaller version of the corresponding uppercase letter.
font-variant
The value of the property may benormal
,small-caps
andinherit
.
p { font-variant: small-caps; }Test See‹/›