CSS Fonts


Selecting the correct font for your website is crucial!

Font Selection is Important

Selecting the correct font greatly affects how website visitors perceive it.

Choosing the correct font can help establish a powerful image for your brand.

It's essential to choose a font that people can easily read. The font you choose makes your text better. Also, make sure to select the right color and size for your text.


The CSS font-family Property

In CSS, we use the font-family property to choose the font of a text.

Note: If you have a font name with more than one word, you should put it in quotes, for example: "Times New Roman"

Tip:The font-family property should have multiple font names. This is like a backup plan to make sure your text looks good on different web browsers and operating systems. You should begin with the specific font you prefer. After that, you should list some general font families. These general ones act as a safety net in case the preferred font isn't available. Separate the font names with commas. This way, the browser can pick a similar font from the general list if it can't find the preferred font.


CSS Web Safe Fonts

Web-safe fonts are fonts that work on all web browsers and devices without any problems.


Fallback Fonts

However, it's important to know that there are no fonts that work perfectly for all web browsers. Sometimes, a font might not be available or might not be set up correctly.

Hence, it's crucial to consistently employ substitute fonts.

In the font-family property, include a list of backup fonts. If the first font doesn't work, the browser will automatically try the next one in the list, and so on. Make sure to finish the list with a common font name.


CSS Font Style

The font-style property is commonly used to make text look slanted, like it's leaning to one side.

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

The font-weight property tells us how bold or light a font appears.


Font Variant

The font-variant property tells us if text should appear in small capital letters or not.

When you use a small-caps font, it turns all the small letters into big ones. But, these converted big letters look smaller than the regular big letters in the text.


CSS Font Size

The font-size attribute controls how big or small the text appears on the screen.

Remember to use the correct HTML tags when creating content. For headings, use <h1> - <h6> for headings and <p> for paragraphs.

The font size can either be an exact size or a size that depends on something else.

Absolute size:

  • Changes the text to a specific size.
  • Prevents users from adjusting the text size in any web browser (not good for accessibility).
  • Absolute size is handy when you already know the exact physical size of the content.

Relative size:

  • Adjusts the text size based on nearby elements.
  • Enables users to modify text size in web browsers.

Set Font Size With Pixels

Using pixels to set the text size allows you to have complete control over how big or small the text appears on your webpage.


Set Font Size With Em

In websites, some developers use 'em' instead of 'pixels' for text size. This helps users change text size in the browser menu.

1em is the same as the font size you see right now. Most browsers start with a default text size of 16 pixels. So, when you see 1em, it means 16 pixels.

You can figure out the size in "em" by dividing the number of pixels by 16. This is done using the formula: pixels/16 = em.


Use a Combination of Percent and Em

To make sure your website looks good on all web browsers, you can set a default font size in percentage for the<body> element:


Responsive Font Size

You can adjust the text size using the "vw" unit, which stands for "viewport width."

In this way, the text will adjust its size according to the size of the web browser window.

Hello World

Change the size of your internet browser's window to observe how the text's font size adjusts accordingly.



CSS Google Fonts

If you don't like the usual fonts in HTML, you can use Google Fonts..

Google Fonts are available for free, and they offer over 1000 different fonts for you to pick from.


How To Use Google Fonts

Simply include a link to a unique style sheet in the part of your HTML document, and then mention this font in your CSS.


Use Multiple Google Fonts

To use more than one Google font, just write the font names with a vertical bar character (|) in between, like this:


CSS Font Property

You can make your code shorter by putting all the different font settings into one property.

The font property is a way to make text look different in various ways:

  • font-style
  • font-variant
  • font-weight
  • font-size/line-height
  • font-family

Note: The text says that you must have both font-size and font-family values. If any of the other values are not there, then the default values will be used.