Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Fonts

CSS Fonts


CSS font properties define the font, boldness, size, and text style.


The difference between serif and sans-serif fonts

Remark On computer screens, sans-serif fonts are considered easier to read than serif fonts


CSS Font Family

In CSS, there are two types of font family names:

Generic family Font family Description
Serif Times New Roman
Georgia Characters in Serif fonts have extra decorations at the end of strokes
Sans-serif Arial
Verdana "Sans" means without - these fonts have no extra decorations at the end
Monospace Courier New
Lucida Console All monospace characters have the same width

Font family

The font-family property sets the font family of text.

The font-family property should be set with several font names as a "fallback" mechanism; if the browser does not support the first font, it will try the next one.

Note: If a font family name consists of more than one word, it must be in quotes, e.g. Font Family: "SimSun".

Multiple font families are specified separated by commas:

Example

p{font-family:"Times New Roman", Times, serif;}

Try it »

For commonly used font combinations, see our Web Safe Font Combinations。


Font styles

Mainly used to specify the font style property for italic text.

This property has three values:

Example

p.normal {font-style:normal;}

p.italic {font-style:italic;}

p.oblique {font-style:oblique;}

Try it »


Font size

The font-size property sets the size of text.

Being able to manage text size is very important in web design. However, you cannot make a paragraph look like a heading or a heading look like a paragraph by adjusting the font size.

Be sure to use the proper HTML tags, just as

-

represents a heading and

represents a paragraph:

Font size values can be absolute or relative sizes.

Absolute size:

  • Set text to a specified size
  • Does not allow users to change the text size in any browser
  • Absolute size is useful when the physical size of the output is fixed

Relative size:

  • Set the size relative to surrounding elements
  • Allows users to change the text size in the browser

Remark If you do not specify a font size, the default size is the same as normal text paragraphs, which is 16 pixels (16px=1em).


Set font size in pixels

Set the text size in pixels to give you complete control over the text size:

Example

h1 {font-size:40px;}

h2 {font-size:30px;}

p {font-size:14px;}

Try it »

The above example can adjust text size by zooming the browser in Internet Explorer 9, Firefox, Chrome, Opera, and Safari.

Although text size can be adjusted with the browser's zoom tool, this adjusts the entire page, not just the text


Set font size with em

To avoid the problem in Internet Explorer of not being able to adjust text size, many developers use em units instead of pixels.

The em unit of size is recommended by the W3C.

1em is equal to the current font size. The default text size in browsers is 16px.

Therefore, the default size of 1em is 16px. You can convert pixels to em using the following formula: px/16=em

Example

h1 {font-size:2.5em;} /* 40px/16=2.5em */

h2 {font-size:1.875em;} /* 30px/16=1.875em */

p {font-size:0.875em;} /* 14px/16=0.875em */

Try it »

In the example above, the em text size is the same as the pixels in the previous example. However, using em units allows text size to be adjusted in all browsers.

Unfortunately, there is still an issue with IE browsers: when adjusting text size, it becomes larger or smaller than normal.


Using a combination of percentages and em

For a solution that works in all browsers, set the default font size of the element to a percentage:

Example

body {font-size:100%;}

h1 {font-size:2.5em;}

h2 {font-size:1.875em;}

p {font-size:0.875em;}

Try it »

Our code works very well. In all browsers, it displays the same text size and allows all browsers to scale the text.


More examples

Set font weight
This example demonstrates how to set the font weight.

You can set font variants
This example demonstrates how to set font variants.

All font properties in a single declaration
This example demonstrates how to use the shorthand property to set font properties in one declaration.


All CSS font properties

Property Description
font Sets all font properties in one declaration
font-family Specifies the font family of text
font-size Specifies the font size of text
font-style Specifies the font style of text
font-variant Displays text in small-caps or normal font.
font-weight Specifies the weight of the font.