CSS and Web Fonts
Learn more about fonts
This article will delve into the world of fonts and explore how to effectively style them on your website using CSS and web fonts.
CSS Font
CSS (Cascading Style Sheets) provides a powerful mechanism to define and control the appearance of text on a web page. Here's how you can manipulate fonts using CSS:
/* Basic Font Properties */
body {
font-family: 'Arial', sans-serif; /* Default font family */
font-size: 16px; /* Default font size */
font-weight: normal; /* Default font weight */
font-style: normal; /* Default font style */
line-height: 1.5; /* Line height for better readability */
}
h1 {
font-size: 2.5rem; /* Large heading size */
font-weight: bold; /* Bold heading */
}
/* Specifying Font Family */
.heading {
font-family: 'Times New Roman', serif; /* Serif font */
}
.body-text {
font-family: 'Helvetica', sans-serif; /* Sans-serif font */
}
/* Using Google Fonts */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
.google-font {
font-family: 'Roboto', sans-serif;
}
Web Font
Web fonts extend the font options available beyond the standard system fonts. Popular platforms like Google Fonts and Adobe Fonts offer a vast library of free and paid web fonts. Here's how to incorporate a web font into your website:
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
/* Using Roboto font from Google Fonts */
.web-font {
font-family: 'Roboto', sans-serif;
}
Do You Know?
You can use multiple font families in your `font-family` property to create fallback options. If a user's system doesn't have the first font, it will try the next one in the list.
Avoid This
Avoid using too many different fonts on a single page, as it can make the design look cluttered and unprofessional.
Important Note
When choosing web fonts, consider the readability and accessibility of the font. Ensure that the font works well in various font sizes and for people with visual impairments.
Summary
- CSS offers comprehensive control over fonts, allowing you to customize family, size, weight, style, and line height.
- Web fonts expand your font selection by providing access to thousands of online font libraries.
- Use CSS to style your web pages, and web fonts to enhance your design with unique and beautiful fonts.