Back to Web Development
2025-12-037 min read

font (Web Development)

Learn font (Web Development) step by step with clear examples and exercises.

Why This Matters

Understanding and mastering font properties in web development is crucial for creating visually appealing and easy-to-read websites. CSS (Cascading Style Sheets) offers various properties that allow you to control font styles on your web pages, enhancing the overall user experience. By learning these properties, you can create engaging and professional-looking websites that stand out from the competition.

Prerequisites

Before diving into CSS font properties, it is essential to have a basic understanding of:

  1. HTML (HyperText Markup Language) - the standard markup language for creating web pages
  2. CSS Basics - knowledge about CSS selectors, properties, values, and the box model concept
  3. Box Model - understanding of how elements are laid out on a web page using the box model concept
  4. Familiarity with HTML elements like ` to , , and inline elements such as and `
  5. Basic understanding of CSS selectors like class, id, and element selectors

Core Concept

CSS provides several font-related properties to style text on your web pages. We will cover some essential properties in this section:

  1. font-family
  2. font-size
  3. font-weight
  4. line-height
  5. text-align
  6. text-transform
  7. letter-spacing
  8. word-spacing
  9. font-style
  10. text-decoration

1. font-family

The font-family property sets the typeface for text elements on your web page. You can specify multiple font families separated by commas, in order of preference. If a specified font is not available on the user's device, the browser will use the next available font in the list.

h1 {
font-family: 'Roboto', Arial, sans-serif;
}

In this example, if Roboto is not available on the user's device, the browser will default to Arial or a sans-serif font.

2. font-size

The font-size property controls the size of the text. You can use various units like pixels (px), em, and rem for specifying the size.

h1 {
font-size: 36px;
}

p {
font-size: 16px;
}

In this example, h1 elements will have a font size of 36 pixels, while p elements will have a font size of 16 pixels.

3. font-weight

The font-weight property sets the thickness or boldness of the text. You can use various values like normal, bold, bolder, lighter, and numerical values from 100 to 900.

h1 {
font-weight: 700; /* Semi-bold */
}

In this example, h1 elements will have a semi-bold font weight (700).

4. line-height

The line-height property sets the height of a line box. It can help control the spacing between lines of text and improve readability.

p {
line-height: 1.5;
}

In this example, the line height for p elements will be 1.5 times their font size, improving the readability of the text.

5. text-align

The text-align property aligns text within an element. Valid values include left, center, right, and justify.

p {
text-align: justify;
}

In this example, the text inside p elements will be justified, meaning it will be aligned to both margins.

6. text-transform

The text-transform property changes the capitalization of the text. Valid values include uppercase, lowercase, capitalize, and none.

h1 {
text-transform: capitalize;
}

In this example, h1 elements will have the first letter of each word capitalized.

7. letter-spacing

The letter-spacing property sets the space between letters. It can help improve readability and create a unique visual style.

h1 {
letter-spacing: 2px;
}

In this example, there will be an additional space of 2 pixels between each letter in h1 elements.

8. word-spacing

The word-spacing property sets the space between words. It can help improve readability and create a unique visual style.

h1 {
word-spacing: 5px;
}

In this example, there will be an additional space of 5 pixels between each word in h1 elements.

9. font-style

The font-style property sets the style of the text, such as italic or oblique. Valid values include normal (or inherit), italic, and oblique.

p {
font-style: italic;
}

In this example, all p elements will be displayed in italic font style.

10. text-decoration

The text-decoration property adds decorative lines to text, such as underlines or strikethroughs. Valid values include none, underline, overline, line-through (or strike), and blink.

a {
text-decoration: underline;
}

In this example, all anchor tags (``) will be underlined.

Worked Example

Let's create a simple HTML page with CSS styles to demonstrate the font properties we discussed earlier.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Font Properties</title>
<style>
body {
font-family: 'Roboto', Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
}

h1 {
font-weight: 700;
text-align: center;
text-transform: capitalize;
letter-spacing: 2px;
word-spacing: 5px;
font-style: italic;
text-decoration: underline;
}

p {
font-weight: 400;
font-style: normal;
text-decoration: none;
}
</style>
</head>
<body>
<h1>Welcome to the CSS Font Properties Lesson!</h1>
<p>This is a paragraph with regular text style.</p>
</body>
</html>

Save this code as font_properties.html, and open it in your web browser to see the result.

Common Mistakes

  1. Forgetting to close the ` tag: Always remember to close the style tag with `.
  2. Not specifying fallback fonts: Always provide multiple font families separated by commas, in order of preference.
  3. Incorrectly setting line-height: Ensure that line-height is set relative to the font size for optimal readability (e.g., line-height: 1.5).
  4. Overuse of letter-spacing and word-spacing: Use these properties sparingly as excessive spacing can negatively impact readability.
  5. Not understanding the difference between font-family, font-style, and text-decoration: These properties control different aspects of text styling.
  6. Incorrect use of text-transform: Be aware that uppercase and lowercase transformations may affect the readability of some text.
  7. Forgetting to include units with font-size: Always include a unit (px, em, or rem) when setting font size.
  8. Not considering accessibility: Ensure that your chosen fonts are easy to read for users with various visual impairments.

Practice Questions

  1. What is the purpose of the font-family property in CSS?
  2. How do you set the font size for a specific HTML element using CSS?
  3. What are some common values for the font-weight property, and what do they represent?
  4. Explain the difference between letter-spacing and word-spacing.
  5. How can you justify text within an HTML element using CSS?
  6. What is the purpose of the text-transform property in CSS?
  7. How do you create a drop-cap effect using CSS?
  8. What is the difference between font-style and text-decoration properties in CSS?
  9. Why is it important to provide multiple font families separated by commas, in order of preference?
  10. How can you ensure that your chosen fonts are accessible for users with visual impairments?

FAQ

Q: Can I use custom fonts on my website with CSS?

A: Yes, you can use custom fonts by uploading the font files to your server and linking to them in your CSS file or directly in your HTML file using the @font-face rule.

Q: What is the default font family for most browsers if I don't specify a font family?

A: The default font family for most modern browsers is sans-serif, but it can vary depending on the browser and operating system.

Q: Can I use em or rem units for setting font sizes in CSS?

A: Yes, you can use both em and rem units to set font sizes in CSS. Em represents the font size of the parent element, while rem represents the root font size (usually 16 pixels).

Q: How do I create a drop-cap effect using CSS?

A: You can create a drop-cap effect by setting a larger font size and letter-spacing for the first letter of a paragraph or heading, and then floating it to the left using negative margin or padding.

Q: What is the difference between font-style and text-decoration properties in CSS?

A: The font-style property sets the style of the text (italic or oblique), while the text-decoration property adds decorative lines to text, such as underlines or strikethroughs.

Q: Why is it important to provide multiple font families separated by commas, in order of preference?

A: By providing multiple font families, you increase the chances that your chosen font will be available on the user's device. If the first font is not available, the browser will use the next available font in the list.

Q: How can I ensure that my chosen fonts are accessible for users with visual impairments?

A: To make your website more accessible, choose fonts with high contrast and large enough sizes to be easily readable by users with various visual impairments. You may also consider using CSS media queries to adjust the font size based on screen resolution or device type.

font (Web Development) | Web Development | XQA Learn