CSS Tutorial - CSS Fonts

Posted by Ridwan Fadilah on Aug 19, 2020 - 08:49 pm

CSS Fonts

Learn about CSS Fonts. Define the font family, size, boldness, the style of the text, and all about CSS Fonts.

A website has text. The different web will have a different font. There are many reasons for a specific font used on a website, such as text legibility, stylishness, design adjustment, etc.

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")

Table of Contents

Font Family

Use the CSS font-family property to set the font family of the text.

The CSS font-family property should hold several font names as a "fallback" system. It will try the next font and so on if the browser does not support the first font.

You can 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.

Important to note: If the name of a font family is more than one word, it must be in quotation marks.

Example:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>CSS font-family</h1>
  <p class="serif">This is a paragraph, shown in the Times New Roman font.</p>
  <p class="sansserif">This is a paragraph, shown in the Arial font.</p>
  <p class="monospace">This is a paragraph, shown in the Lucida Console font.</p>
</body>

</html>

- CSS


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

.sansserif {
  font-family: Arial, Helvetica, sans-serif;
}

.monospace {
  font-family: "Lucida Console", Courier, monospace;
}

Use a comma to separate the font family list if you have more than one font family.

Here's the result of the example above:

CSS Font Family Example

CSS Font Style

The CSS Fonts have several styles that you can use on a web page.

Font Style

You can use the CSS font-style property to specify the italic style of the font that will use.

Example:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>CSS font-style</h1>
  <p class="normal">This is a normal font style.</p>
  <p class="italic">This is an italic font style.</p>
  <p class="oblique">This is an oblique font style.</p>
</body>

</html>

- CSS


p.normal {
  font-style: normal;
}

p.italic {
  font-style: italic;
}

p.oblique {
  font-style: oblique;
}

The example above will display by the browser like this:

CSS Font Style Example

The CSS font-style 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)
Font Weight

By using CSS Fonts Properties, you can also set weight of font.

Use the CSS font-weight property to specifies the weight of a font.

Example:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>CSS font-weight</h1>
  <p class="normal">This is a normal font weight.</p>
  <p class="thick">This is a thick font weight.</p>
</body>

</html>

- CSS


p.normal {
  font-weight: normal;
}

p.thick {
  font-weight: bold;
}

The example above will display by the browser like this:

CSS Font Weight Example
Font Variant

Use the CSS font-variant property to 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 appear in smaller font size than the original uppercase letters in the text.

Example:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>CSS font-variant</h1>
  <p class="normal">This is a paragraph.</p>
  <p class="small">This is another paragraph.</p>
</body>

</html>

- CSS


p.normal {
  font-variant: normal;
}

p.small {
  font-variant: small-caps;
}

The example above will display by the browser like this:

CSS Font Variant Example

CSS Font Size

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

Absolute size:

  • Sets the text to a specified size
  • Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
  • Absolute size is useful when the physical size of the output is known

Relative size:

  • Sets the size relative to surrounding elements
  • Allows a user to change the text size in browsers

The value of the CSS font-size property could be set in pixel, em, or percent unit. A combination of the units is also allowed by the browser.

Important to note: The default size for normal text, like paragraphs, is 16px (16px=1em).

Set Font Size With Pixel Unit

Pixel is a mostly use unit, setting the text size with pixels gives you full control over the text size.

Here's the example of the pixel unit use:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>CSS font-size With Pixel Unit</h1>
  <p class="p-1">This is a paragraph.</p>
  <p class="p-2">This is another paragraph.</p>
</body>

</html>

- CSS


h1 {
  font-size: 40px;
}

p.p-1 {
  font-size: 14px;
}
p.p-2 {
  font-size: 18px;
}

The example above will display by the browser like this:

CSS Font Size with Pixel Unit Example
Set Font Size With Em Unit

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

Here's the example of the em unit use:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>CSS font-size With Em Unit</h1>
  <p class="p-1">This is a paragraph.</p>
  <p class="p-2">This is another paragraph.</p>
</body>

</html>

- CSS


h1 {
  font-size: 2.5em;
}

p.p-1 {
  font-size: 0.875em;
}

p.p-2 {
  font-size: 1.125em;
}

The example above will display by the browser like this:

CSS Font Size with Em Unit Example
A Combination of Percent and Em Unit

Specifying the font-size in percent and em displays the same size in all major browsers and allows all browsers to resize the text!

Example:

- HTML


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="style.css">
  <title>Document</title>
</head>

<body>
  <h1>A Combination of Percent and Em Unit</h1>
  <p class="p-1">This is a paragraph.</p>
  <p class="p-2">This is another paragraph.</p>
</body>

</html>

- CSS


body {
  font-size: 100%;
}

h1 {
  font-size: 2.5em;
}

p.p-1 {
  font-size: 0.875em;
}

p.p-2 {
  font-size: 1.125em;
}

The example above will display by the browser like this:

CSS Font Size Value Combination Example

Okay, that's the CSS Fonts tutorial. You will learn more about the CSS basics in the next chapter.

You can read another CSS basics tutorial in the related article below. You can also find another tutorial on our YouTube Channel.

Thanks for reading.


Related Articles