CSS font-size Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Typography

What You’ll Learn

The font-size property sets how large text appears. It is essential for readable body copy, clear headings, and a visual hierarchy across your site.

01

px

Fixed pixels.

02

rem

Root relative.

03

em

Parent relative.

04

Keywords

small, large.

05

Hierarchy

Headings vs body.

06

Responsive

clamp(), vw.

Introduction

The font-size property in CSS is used to set the size of the font for an element's text content. It is a crucial property for controlling the visual appearance and readability of text on web pages.

Definition and Usage

By adjusting the font size, you can create a hierarchy of text elements, making your content more accessible and visually appealing. Set a comfortable base size on body or html, then scale headings and UI labels relative to that base.

💡
Beginner Tip

Many sites use html { font-size: 100%; } (16px default) and body { font-size: 1rem; } for body text. Headings might use 1.5rem, 2rem, or larger.

📝 Syntax

The syntax for the font-size property is simple. You can specify the font size using various units:

syntax.css
element {
  font-size: value;
}

Here, value can be specified in different units such as pixels (px), em, rem, percentages (%), points (pt), and others.

Basic Example

font-size-basic.css
p {
  font-size: 20px;
}
font-size: 1rem; font-size: 1.25rem; font-size: 112.5%; font-size: clamp(1rem, 2.5vw, 1.5rem);

Default Value

The default value of the font-size property is medium, which typically translates to 16px in most browsers.

Syntax Rules

  • Accepts length units, percentages, keywords, and calculated values like clamp().
  • The property is inherited — children receive the parent's computed size unless overridden.
  • Percentage values are relative to the parent element's font size.
  • em is relative to the element's own font size (or inherited size).
  • rem is relative to the root (html) font size.

⚡ Quick Reference

QuestionAnswer
Initial valuemedium (usually 16px)
Applies toAll elements
InheritedYes
Common body size1rem or 16px
Responsive patternclamp(min, preferred, max)

💎 Property Values

Keywords

KeywordDescription
xx-smallxx-largeAbsolute-size keywords on a browser scale from extra small to extra large
mediumDefault size, typically 16px
larger / smallerOne step larger or smaller than the inherited size

Absolute units

px (pixels), pt (points), cm, mm, and in (inches) set fixed physical or pixel-based sizes.

Relative units

UnitRelative toCommon use
remRoot font sizeBody text, headings, site-wide scale
emParent or element font sizeNested components, buttons inside cards
%Parent font sizeScaling relative to parent text
vw / vhViewport width / heightFluid display headings

👀 Live Preview

The same sentence at three different font sizes:

font-size: 0.875rem; Readable text at a smaller size.
font-size: 1rem; Readable text at the default body size.
font-size: 1.25rem; Readable text at a larger size.

Examples Gallery

In this example, we set the font size of a paragraph to 20 pixels — plus rem-based typography, em nesting, and responsive clamp sizing.

🔢 Size Units

Start with the reference example — a paragraph at 20 pixels.

Example 1 — Font Size in Pixels

Set paragraph text to a fixed 20px size.

font-size-px.html
<style>
  p {
    font-size: 20px;
  }
</style>

<p>This paragraph has a font size of 20 pixels.</p>
Try It Yourself

How It Works

Pixels give a fixed size regardless of parent settings. The text always renders at 20px unless the user zooms the page.

Example 2 — rem Typography Scale

Build a simple type scale relative to the root font size.

font-size-rem.css
body { font-size: 1rem; }
h1 { font-size: 2rem; }
h2 { font-size: 1.5rem; }
small { font-size: 0.875rem; }
Try It Yourself

How It Works

Changing the root size scales every rem-based value consistently across the page.

📈 Relative & Responsive

Use em for nested scaling and clamp for fluid headings.

Example 3 — em Inside a Card

Size a button relative to its card's font size using em.

font-size-em.css
.card { font-size: 18px; }
.card button { font-size: 0.875em; }
Try It Yourself

How It Works

0.875em means 87.5% of the card's 18px, so the button renders at about 15.75px.

Example 4 — Responsive clamp()

Let a hero heading grow with the viewport but stay within min and max bounds.

font-size-clamp.css
.hero-title {
  font-size: clamp(1.75rem, 4vw, 3rem);
}
Try It Yourself

How It Works

clamp() picks the middle value when it falls between the minimum (1.75rem) and maximum (3rem), giving fluid but bounded typography.

♿ Accessibility

  • Use at least 16px (1rem) for body text on most sites for comfortable reading.
  • Prefer rem or % so users who change browser default font size still get readable text.
  • Maintain size contrast between headings and body without making headings excessively large on mobile.
  • Test zoom up to 200% to ensure text remains readable and layouts do not break.
  • Do not disable user scaling in the viewport meta tag.

🧠 How font-size Works

1

You set a size value

Choose px, rem, em, %, keywords, or a function like clamp().

Declaration
2

Browser computes size

Relative units are converted to an absolute pixel size for rendering based on root or parent values.

Computed value
3

Text renders at that size

Glyphs scale accordingly. Inherited children use the computed size unless overridden.

Rendering
=

Clear visual hierarchy

Headings, body copy, and labels each appear at an appropriate readable size.

🖥 Browser Compatibility

The font-size property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is one of the fundamental CSS properties with wide-ranging support across platforms and devices.

Baseline · Universal support

font-size everywhere

All units and keywords work in every browser, including legacy environments.

99% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
font-size property 99% supported

Bottom line: font-size is universal. Choose units thoughtfully for accessibility and responsive design.

🎉 Conclusion

The font-size property is essential for web developers to control the size of text on their websites. By using this property, you can enhance the readability and visual hierarchy of your content, making it more accessible and engaging for users.

Experiment with different units and values to find the right balance for your design. For most projects, start with rem-based body text and scale headings from there.

💡 Best Practices

✅ Do

  • Use 1rem for body text in most layouts
  • Build a consistent type scale for headings
  • Use clamp() for fluid hero headings
  • Set base size on html or body
  • Test readability on mobile and at high zoom

❌ Don’t

  • Use font sizes below 12px for important content
  • Mix too many different sizes without a system
  • Rely on px alone when accessibility matters
  • Forget that em compounds in deeply nested elements
  • Disable user font scaling in the viewport meta tag

Key Takeaways

Knowledge Unlocked

Five things to remember about font-size

Use these points when sizing text on your site.

5
Core concepts
medium 02

Default 16px

medium keyword.

Default
rem 03

Prefer rem

Root relative.

Best practice
📈 04

Hierarchy

Headings vs body.

Layout
🔁 05

Inherited

Children inherit.

Cascade

❓ Frequently Asked Questions

The font-size property sets the size of text for an element. It controls how large or small characters appear on the page.
The initial value is medium, which browsers typically render as 16px for normal body text.
rem is often preferred for body text because it scales with the root font size and respects user browser settings. px gives fixed pixel control and is still common for small UI details.
em is relative to the font size of the element or its parent. rem is relative to the root html element font size, so it is more predictable for site-wide typography.
Yes. Units like vw and vh size text relative to the viewport. clamp() is a popular way to combine viewport scaling with minimum and maximum limits.

Practice in the Live Editor

Open the HTML editor, change font-size values in px, rem, and clamp(), and see how text scales.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful