In this section, we will explore the various properties in CSS that allow you to control the appearance of text fonts on your web pages. Understanding these properties will enable you to create visually appealing and readable text content.
Key Font Properties
- font-family
- font-size
- font-weight
- font-style
- font-variant
- line-height
- font
- font-family
The font-family
property specifies the font for an element. You can list multiple fonts as a "fallback" system. If the browser does not support the first font, it tries the next one.
- font-size
The font-size
property sets the size of the font. It can be defined in various units such as pixels (px), em, rem, percentages (%), and more.
- font-weight
The font-weight
property sets the weight (or boldness) of the font. Common values are normal
, bold
, bolder
, lighter
, or numerical values ranging from 100 to 900.
- font-style
The font-style
property specifies the style of the font. It can be normal
, italic
, or oblique
.
- font-variant
The font-variant
property is used to display text in a small-caps font. It can be normal
or small-caps
.
- line-height
The line-height
property sets the height of a line box. It's commonly used to set the distance between lines of text.
- font (Shorthand Property)
The font
property is a shorthand for setting multiple font-related properties in one declaration. It can include font-style
, font-variant
, font-weight
, font-size
, line-height
, and font-family
.
Practical Examples
Example 1: Basic Font Styling
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font-family: "Arial", sans-serif; } h1 { font-size: 2em; font-weight: bold; } p { font-size: 1em; line-height: 1.5; } em { font-style: italic; } </style> <title>Font Properties Example</title> </head> <body> <h1>Welcome to CSS Font Properties</h1> <p>This is a paragraph demonstrating various <em>font properties</em> in CSS.</p> </body> </html>
Example 2: Advanced Font Styling with Shorthand
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font: normal 400 16px/1.5 "Helvetica", sans-serif; } h1 { font: italic small-caps bold 24px/1.2 "Georgia", serif; } p { font: normal 300 14px/1.6 "Verdana", sans-serif; } </style> <title>Advanced Font Properties Example</title> </head> <body> <h1>Advanced Font Styling</h1> <p>This example demonstrates the use of the <code>font</code> shorthand property to apply multiple font-related styles in one declaration.</p> </body> </html>
Exercises
Exercise 1: Basic Font Styling
Task: Create a simple HTML page with a heading and a paragraph. Apply different font properties to the heading and paragraph.
Solution:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font-family: "Arial", sans-serif; } h1 { font-size: 2em; font-weight: bold; font-style: italic; } p { font-size: 1em; line-height: 1.5; font-variant: small-caps; } </style> <title>Exercise 1: Basic Font Styling</title> </head> <body> <h1>Exercise 1 Heading</h1> <p>This is a paragraph for Exercise 1.</p> </body> </html>
Exercise 2: Advanced Font Styling
Task: Create an HTML page with multiple headings and paragraphs. Use the font
shorthand property to apply various font styles.
Solution:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font: normal 400 16px/1.5 "Helvetica", sans-serif; } h1 { font: italic small-caps bold 24px/1.2 "Georgia", serif; } h2 { font: normal 600 20px/1.3 "Times New Roman", serif; } p { font: normal 300 14px/1.6 "Verdana", sans-serif; } </style> <title>Exercise 2: Advanced Font Styling</title> </head> <body> <h1>Exercise 2 Main Heading</h1> <h2>Subheading 1</h2> <p>This is the first paragraph for Exercise 2.</p> <h2>Subheading 2</h2> <p>This is the second paragraph for Exercise 2.</p> </body> </html>
Conclusion
In this section, we covered the essential font properties in CSS, including font-family
, font-size
, font-weight
, font-style
, font-variant
, line-height
, and the shorthand font
property. By mastering these properties, you can significantly enhance the typography of your web pages, making them more readable and visually appealing. Next, we will delve into text alignment and spacing to further refine your text styling skills.
CSS Mastery: From Beginner to Advanced
Module 1: Introduction to CSS
- What is CSS?
- CSS Syntax and Selectors
- How to Add CSS to HTML
- Basic CSS Properties
- CSS Colors
- CSS Units and Measurements
Module 2: Text and Font Styling
- Text Properties
- Font Properties
- Google Fonts Integration
- Text Alignment and Spacing
- Text Decoration and Transformation
Module 3: Box Model and Layout
- Understanding the Box Model
- Margin and Padding
- Border and Outline
- Width and Height
- Box Sizing
- CSS Display Property
Module 4: Positioning and Floating
- CSS Position Property
- Static, Relative, Absolute, and Fixed Positioning
- CSS Float and Clear
- Creating Layouts with Float
- CSS Z-Index
Module 5: Flexbox
- Introduction to Flexbox
- Flex Container Properties
- Flex Item Properties
- Creating Layouts with Flexbox
- Responsive Design with Flexbox
Module 6: CSS Grid
- Introduction to CSS Grid
- Grid Container Properties
- Grid Item Properties
- Creating Layouts with CSS Grid
- Responsive Design with CSS Grid
Module 7: Advanced CSS Techniques
Module 8: Responsive Design
- Introduction to Responsive Design
- Media Queries
- Responsive Typography
- Responsive Images
- Mobile-First Design
Module 9: Preprocessors and Frameworks
- Introduction to CSS Preprocessors
- Sass Basics
- Less Basics
- Introduction to CSS Frameworks
- Using Bootstrap