Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

HTML & CSS IntroductionChapter 11. CSS: Styling Text and Images

font-size

font-size

Setting Font Size in CSS

The font-size property is used to set the size of fonts. You can use various length units or keywords to set a property value.

px, em, rem

px, em, and rem are often used as font-size units.

px (pixel) is the length unit used to define the size of elements in absolute pixel size. px is useful when you want to fix the size of elements regardless of the browser's viewport size.

em defines the length of elements relative to the parent element's font-size. em is used in the specific design element that you want to keep proportional to the parent element.

rem defines the size relative to the root html element's font-size. rem is usually used to define font size. If you have many nesting elements in your website design, using rem makes managing different sizes easier as it won't impact the parent element's font size. If you use em in a website with multi-layer nesting elements, managing font sizes can be very complicated. Thus, rem is often preferred for font size settings.

Inheritance

Each element inherits its parent font size, which means the font-size set for the body element is usually applied to descendant elements. However, there are some elements that have different font sizes as a default, such as headings. This is because each browser has its default stylesheets, called user-agent stylesheets, and the stylesheets have different font sizes for particular elements.

Idea62.5%

As the default font size of the root element (<html> element) is usually 16px, adjusting the font size of the <html> element to 62.5%, which is equal to 10px, is often used as a technique. This makes it easier for us to use rem sizing in a more intuitive way. For example, 1rem becomes 10px, 1.6rem becomes 16px or 2rem becomes 20px.

To maintain the normal font size setting the same as the default setting, you can set the body font size to 1.6rem. By doing this, you can maintain the default font size while adjusting the font size to 1rem.

Here is the CSS code often applied to the <html> and <body> elements at the beginning of a project.

html{
  font-size: 62.5%;
}

body{
  font-size: 1.6rem;
}

Heading Font Size

The default font size of the root element (<html>) is 16px in most browsers. As explained, each element inherits the font size, however, some elements, such as heading elements, have different default settings.

In most browsers, headings' default font sizes are defined by the em unit and differ by level. The <h1> is the largest while <h6> is the smallest. The <h4> and <p> have the same font size.

This is the summary of the heading tag and paragraph tag's default font size setting.

HTML Heading Font Size

Practice 1

Objective:
Check the default headings' font sizes

1. Create a new HTML file for this chapter

  • Create a copy of the chapter10.html file and change the name to chapter11.html.
  • Change the <title> section to 11. CSS: Styling Text and Images.
  • Also, delete the existing content of the <body> element that was created in the previous chapter.
  • Add the <h1> tag to show the chapter title of this page at the top of the page.
  • The code should look like the one below.
chapter11.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="css/practice.css">
  <title>Chapter 11. CSS: Styling Text and Images</title>
</head>
<body>
  <h1>Chapter 11. CSS: Styling Text and Images</h1>
</body>
</html>

2. Update the body section

Add the code below in the body section of the HTML file. We are adding the font-size-box class to add a group for adding the font-size styling.

chapter11.html
<h2>Font Size</h2>
<h3>Font Size Original</h3>
  <div class="font-size-box">
  <h1>h1 Font Size</h1>
  <h2>h2 Font Size</h2>
  <h3>h3 Font Size</h3>
  <h4>h4 Font Size</h4>
  <h5>h5 Font Size</h5>
  <h6>h6 Font Size</h6>
<p>Paragraph Font Size</p>
</div>

3. Update the CSS file

Open the practice.css file and add new code to add styles to the font-size-box class. We haven't explained the border properties yet. For now, just copy and paste the code to your file. For better code readability, add a comment upfront.

practice.css
/* Chapter 11. CSS: Styling Text and Images */
.font-size-box{
  width: 300px;
  padding: 5px;
  border:1px dotted #26BCCE;
}

4. Check the result with a browser

  • Open chapter11.html with a browser
  • You can see how headings are displayed as default settings.

You can also check the sample result here (Demo Site).

link

Practice 2

Objective:
Check how the parent font size impacts heading font sizes

1. Update the body section of the HTML file

Add the code below to the chapter11.html file. In this code, we are changing the parent element font size to 10px.

chapter11.html
<h3>Font Size: Parent Font Size 10px (Child Font Size Original)</h3>
<div class="font-size-box" style="font-size: 10px;">
  <h1>h1 Font Size</h1>
  <h2>h2 Font Size</h2>
  <h3>h3 Font Size</h3>
  <h4>h4 Font Size</h4>
  <h5>h5 Font Size</h5>
  <h6>h6 Font Size</h6>
  <p>Paragraph Font Size</p>
</div>

2. Check the result with a browser

  • Open chapter11.html with a browser.
  • You can see how each heading's font size changes.

You can also check the sample result here (Demo Site).

link

Practice 3

Objective:
Check how each length (size) unit behaves differently.

1. Update the body section

Add the code below to the chapter11.html file. In this code, we are changing the <h1> element's font size using different length units.

chapter11.html
<h3>Font Size: Parent Font Size 10px (Child Font Size with Different Unit)</h3>
<div class="font-size-box" style="font-size: 10px;">
  <h1>h1 Font Size: No Setting</h1>
  <h1 style="font-size: 2em">h1 Font Size: 2em</h1>
  <h1 style="font-size: 200%">h1 Font Size: 200%</h1>
  <h1 style="font-size: 20px">h1 Font Size: 20px</h1>
  <h1 style="font-size: 2rem">h1 Font Size: 2 rem</h1>
</div>
<hr>

2. Check the result with a browser

  • Open chapter11.html with a browser
  • You can confirm several points:
    • <h1> default font size setting is 2em.
    • 200% is the same as 2em (proportion to the parent element).
    • 20px is the same as 2em (as the parent font size is 10px now).
    • rem doesn't follow the parent font size change.

You can also check the sample result here (Demo Site).

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How to Choose Where to Add CSS

Where To Type CSS?

The Essentials of CSS Basic Selectors

CSS Basic Selectors

Styling Cards in HTML & CSS

Cards

How to Use Nested Flex Box for Multi-layer Layouts

Nested Flex Box

How to Choose Where to Add CSS

Where To Type CSS?

The Essentials of CSS Basic Selectors

CSS Basic Selectors

Styling Cards in HTML & CSS

Cards

How to Use Nested Flex Box for Multi-layer Layouts

Nested Flex Box

Tags:

Inheritance

Length

Alignment

Text Styling

Font

HTML & CSS Introduction
Course Content

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;