CSS Fonts

The CSS font properties define the font family, boldness, size, and the style of a text.

CSS Font Families

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

  • generic family – a group of font families with a similar look (like “Serif” or “Monospace”)
  • font-family – a specific font family (like “Times New Roman” or “Arial”)

Font Family

The font family of a text is set with the font-family property.

The font-family property should hold several font names as a “fallback” system. If the browser does not support the first font, it tries the next font, and so on.

Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available. If the name of a font family is more than one word, it must be in quotation marks, like “Times New Roman”.

More than one font family is specified in a comma-separated list:

Example-

<!DOCTYPE html>
<html lang = “en-us”>
<head>
<style>
h1 {
font-family: “Times New Roman”, Times, serif;
}

p{
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>

<h1>This sentence is shown in the Times New Roman font.</h1>
<p>This sentence is shown in the Arial font.</p>

</body>
</html>

Output-

Font Style

The font-style property is mostly used to specify an italic text.

This property has three values:

  • normal – The text is shown normally.
  • italic – The text is shown in italics.
  • oblique – The text is “leaning” (oblique is very similar to italic, but less supported).

Example-

<!DOCTYPE html>
<html lang = “en-us”>
<head>
<style>
h1 {
font-style: normal;
}

h2 {
font-style: italic;
}

h3{
font-style: oblique;
}
</style>
</head>
<body>

<h1>This is a paragraph in normal style.</h1>
<h2>This is a paragraph in italic style.</h2>
<h3>This is a paragraph in oblique style.</h3>

</body>
</html>

Output-

Font Size

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

Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

Always use the proper HTML tags, like <h1> – <h6> for headings and <p> for paragraphs.

The font-size value can be an absolute, or relative size.

Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px.

Set Font Size With Pixels

Setting the text size with pixels gives you full control over the text size.

Example-

<!DOCTYPE html>
<html lang = “en-us”>
<head>
<style>
h1 {
font-size: 50px;
}

h2 {
font-size: 40px;
}

p {
font-size: 15px;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Output-

Set Font Size With Em

To allow users to resize the text , many developers use em instead of pixels.

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

The size can be calculated from pixels to em using this formula: pixels/16=em

Example-

<!DOCTYPE html>

<html lang = “en-us”>

<head>
<style>
h1 {
font-size: 2.5em;
}

h2 {
font-size: 1.875em;
}

p {
font-size: 0.875em;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
</body>
</html>

Font Weight

The font-weight property specifies the weight of a font.

Example-

<!DOCTYPE html>

<html lang = “en-us”>
<head>
<style>
h1 {
font-weight: normal;
}

h2 {
font-weight: bold;
}

</style>
</head>
<body>

<h1>This is a normal.</h1>
<h2>This is a bold.</h2>

</body>
</html>

Output-

Font Variant

The font-variant property specifies whether or not a text should be displayed in a small-caps font.

In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a smaller font size than the original uppercase letters in the text.

Example-

<!DOCTYPE html>
<html lang = “en-us”>
<head>
<style>
p {

font-variant: small-caps;
}
</style>
</head>
<body>

<p> Our school name is Science Academy of Chicago</p>

</body>
</html>

Output-

 

Exercise-

  1. Set the font family for the page to “Courier New”, and the font family for <h1> to “Verdana”.

2. Show <p> elements as “italic” text and heading in bold.

3. Set the font size for the page to “20px”, and the font size for <h1> to “40px”.