Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS font-size property

CSS font-size property

Example

Set the font size of different HTML elements:

h1

{

font-size:

250

%

}

h2

{

font-size:

200

%

}

p

{

font-size:

100

%

}

Try it »


Property definition and usage

The font-size property is used to set the font size.

font-size not only determines the displayed text size, but also affects em, line-height, letter-spacing and other related typographic calculations.

Default value: medium
Inherited: yes
Version: CSS1
JavaScript syntax: object.style.fontSize="larger"

/*

, absolute size values

*/

font-size:

xx-small

; font-size:

x-small

; font-size:

small

; font-size:

medium

; font-size:

large

; font-size:

x-large

; font-size:

xx-large

;

/*

, relative size values

*/

font-size:

larger

; font-size:

smaller

;

/*

, length values

*/

font-size: 12

px

; font-size: 0.8

em

;

/*

, percentage values

*/

font-size: 80%; font-size:

inherit

;


Browser support

The numbers in the table indicate the first browser version that supports this property.

property
font-size 1.0 5.5 1.0 1.0 7.0

Basic concepts

font-size Used to set the size of the text in an element.

The browser's default root font size is usually 16px.

That is, by default html { font-size: 100%; } usually equivalent to html { font-size: 16px; }.

This default value becomes the basis for calculating many relative units.

Example

/* Set the page default font size */

html

{

   

font-size

:

100%

;

/* Usually equivalent to 16px, respecting the user's browser settings */

}

/* Set the body text size */

body

{

   

font-size

:

16px

;

/* Common base size for body text */

   

line-height

:

1.6

;

/* Use a unitless value for line height to ease inheritance */

}

/* Set the heading size */

h1

{

   

font-size

:

2rem

;

/* Approximately equal to 32px by default */

}

It is not recommended to arbitrarily fix the root element's font size to a very small value, as this may affect the user's browser font preferences and accessibility experience.


Common value types for font-size

font-size You can use keywords, length units, percentages, and functions.

Different values suit different scenarios.

Value type Example Description Applicable scenarios
The keyword small, large Calculated by the browser according to preset ratios. Simple pages and scenarios that need to follow the user's default settings.
Absolute units 16px, 12pt The size is relatively fixed and highly controllable. Precise UI control and print styles.
Relative units 1rem, 1.25em Calculated relative to the parent element or root element. Responsive design and component-based pages.
Viewport units 4vw, 3vmin Calculated relative to the browser viewport size. Large headings, showcase pages, and fluid typography.
CSS Functions clamp(), calc() Dynamically calculate the font size through expressions. Modern responsive typography.

Keyword values

CSS provides two types of font-size keywords.

One type is absolute size keywords, such as small, medium, large.

The other type is relative size keywords, such as larger and smaller.

Absolute size keywords

Absolute size keywords are not fixed pixel values, but levels calculated by the browser based on the default font size.

The keyword Approximate values at the default 16px Ratio Applicable scenarios
xx-small About 9px 0.56x Very small annotations; not recommended for body text.
x-small About 10px 0.625x Footnotes or auxiliary information.
small About 13px 0.8x Auxiliary text.
medium About 16px 1x Default body text.
large About 18px 1.125x Small headings or emphasized text.
x-large About 24px 1.5x Headings.
xx-large About 32px 2x Large headings.
xxx-large About 48px 3x Display headings.

Relative size keywords

larger and smaller They scale up or down relative to the parent element's font size.

They suit simple cases that need to follow the parent element's font size.

Example

/* parent font size is 16px */

.parent

{

   

font-size

:

16px

;

}

/* larger scales up relative to the parent; the exact ratio is decided by the browser */

.child-larger

{

   

font-size

:

larger

;

}

/* smaller scales down relative to the parent; the exact ratio is decided by the browser */

.child-smaller

{

   

font-size

:

smaller

;

}

Keyword values are relatively accessibility-friendly, but less controllable, so they are less commonly used in real projects rem, em or clamp().


Absolute units

Absolute unit values are relatively fixed and do not depend on the parent element's font size.

They suit interfaces that need precise control, or print styles.

Unit Meaning Conversion relationships Applicable scenarios
px CSS logical pixel. The most commonly used length unit on screens. Web UI and component font sizes.
pt Print point. 1pt = 1/72 inch, approximately equal to 1.333px. Print styles.
cm / mm Centimeter / millimeter. 1cm = 37.8px, 1mm = 3.78px. Print layout, rarely used on the web.
in Inch. 1in = 96px. Printing or special size conversions.
pc Pica. 1pc = 12pt = 16px. Traditional typography, rarely used on the web.

Example

/* absolute unit conversion relationships */

/* 1in = 96px = 2.54cm = 25.4mm = 6pc = 72pt */

/* pt is usually used for printing */

@media print {

    body

{

       

font-size

:

12pt

;

   

}

}

/* px, rem or clamp() are usually used for screen display */

@media screen {

    body

{

       

font-size

:

16px

;

   

}

}

On modern screens, 1px usually means a CSS logical pixel, not necessarily one physical pixel.


Relative units

Relative units are computed dynamically based on a reference value.

They are the most commonly used font-size units in modern responsive design.

Unit Reference object Description Typical usage
em The parent element's or current element's font size. Used for font-size relative to the parent element, and multiplies when nested. Local component scaling.
rem Root element html font size. Unaffected by nesting, more stable in computation. Site-wide font-size system.
% Parent element font size. in font-size behaves similarly in em. Scales by the parent's ratio.
ch Character 0 The width of the digit 0. More often used to control line width. width: 60ch;
ex The height of the lowercase letter x. Greatly affected by font design. A few special typography cases.
lh The element's line height. A unit added in CSS Values 4. Spacing aligned with the line height.

The difference between em and rem

em When setting font-size , it is computed relative to the parent element's font size.

If multiple nested levels all use em, the font size multiplies at each level.

rem always computed relative to the root element's font size, so it is more stable.

Example

/* root element font size is 16px */

html

{

   

font-size

:

16px

;

}

/* em is affected by the parent element's font size */

.outer

{

   

font-size

:

1.5em

;

/* 16px × 1.5 = 24px */

}

.middle

{

   

font-size

:

1.5em

;

/* 24px × 1.5 = 36px */

}

/* rem is always computed relative to html */

.outer-rem

{

   

font-size

:

1.5rem

;

/* 16px × 1.5 = 24px */

}

.middle-rem

{

   

font-size

:

1.5rem

;

/* 16px x 1.5 = 24px, no compounding */

}

in font-size used on em , it is relative to the parent element's font size; but when used on the same element's padding or margin used on em , it is relative to that element's own font size.


Viewport units

Viewport units are computed based on the browser window size.

They suit fluid typography that changes with screen width.

But using viewport units alone can make text too small on phones and too large on ultra-wide screens.

Unit Meaning Description Typical scenario
vw 1% of the viewport width. The wider the viewport, the larger the font size. Responsive headings.
vh 1% of the viewport height. On mobile it is affected by the browser toolbar. Full-screen showcase pages.
vmin vw and vh 1% of the smaller value in Suitable for both landscape and portrait. Adaptive large headings.
vmax vw and vh 1% of the larger value in Greater range of variation. Display-oriented visual design.
svh / dvh / lvh Small / dynamic / large viewport height. Used to solve the problem of changing browser toolbars on mobile. Full-screen pages on mobile.
cqi / cqb Container query units. Relative to the container size, not the whole viewport. Component-level responsive typography.

Example

/* when using vw alone, font-size scaling can be too aggressive */

h1

{

   

font-size

:

5vw

;

}

/* it is better to use clamp() to set upper and lower bounds */

h1

{

   

font-size

:

clamp

(

1.5rem

,

5vw

,

3.5rem

)

;

}

/* container query units suit component-level responsive design */

.card

{

    container-type

:

inline-size

;

}

.card-title

{

   

font-size

:

clamp

(

1rem

,

4cqi

,

2rem

)

;

}

Container query units suit component libraries and card layouts, because a component's size is not necessarily the same as the browser viewport size.


The clamp() function

clamp() is a very practical function in modern responsive typography.

It can constrain the font size between a minimum and a maximum.

The middle value usually uses viewport units or a calc expression, letting the font size change with the screen.

Parameters Meaning Example Description
Minimum value The font size will not be smaller than this value. 1rem Ensures readability on mobile.
Preferred value The browser prefers to try this dynamic value first. 2vw + 1rem Lets the font size change fluidly with the screen.
Maximum value The font size will not be larger than this value. 3rem Prevents text from being too large on big screens.

Example

/* clamp(minimum, preferred, maximum) */

h1

{

   

font-size

:

clamp

(

1.75rem

,

2.5vw

1rem

,

3.5rem

)

;

}

h2

{

   

font-size

:

clamp

(

1.25rem

,

2vw

0.75rem

,

2.25rem

)

;

}

p

{

   

font-size

:

clamp

(

1rem

,

1vw

0.5rem

,

1.25rem

)

;

}

/* combine with CSS variables to build a design system */

:root

{

    --text-sm

:

clamp

(

0.8rem

,

1.5vw

0.25rem

,

0.95rem

)

;

    --text-md

:

clamp

(

1rem

,

2vw

0.5rem

,

1.25rem

)

;

    --text-lg

:

clamp

(

1.25rem

,

3vw

0.5rem

,

2rem

)

;

    --text-xl

:

clamp

(

1.75rem

,

4vw

0.75rem

,

3.5rem

)

;

}

.article-title

{

   

font-size

:

var

(

--text-xl

)

;

}


The min() and max() functions

min() takes the smaller value among several values.

max() takes the larger value among several values.

They can be used for font size, but in fluid typography they are usually less clamp() intuitive.

function Behavior Example Applicable scenarios
min() Selects the smaller computed result. min(5vw, 48px) Only limits the maximum.
max() Selects the larger computed result. max(1rem, 2vw) Only limits the minimum.
clamp() Sets the minimum, preferred, and maximum at the same time. clamp(1rem, 2vw, 2rem) A complete fluid font-size solution.

Example

/* font size will not exceed 48px, but may be too small on small screens */

h1

{

   

font-size

:

min

(

5vw

,

48px

)

;

}

/* font size will not be below 16px, but may be too large on big screens */

h2

{

   

font-size

:

max

(

16px

,

3vw

)

;

}

/* limiting both min and max suits real projects better */

h3

{

   

font-size

:

clamp

(

16px

,

3vw

,

32px

)

;

}


The calc() function

calc() can mix different units in CSS for calculations.

It is often used to combine fixed and relative values.

In font-size scenarios,calc() Often used together with vw, rem .

Example

/* use calc() to combine rem and vw */

h1

{

   

font-size

:

calc

(

1.5rem

2vw

)

;

}

/* using calc() inside clamp() makes the code clearer */

h2

{

   

font-size

:

clamp

(

1.25rem

,

calc

(

1rem

2vw

)

,

3rem

)

;

}

/* note that spaces are required on both sides of operators */

p

{

   

font-size

:

calc

(

1rem

0.25vw

)

;

}

calc(1rem+2vw) This kind of writing is wrong; spaces should be placed on both sides of operators, for example calc(1rem + 2vw).


Inheritance and cascade

font-size is an inheritable property.

If a child element does not declare a font size, it inherits the parent's computed font size.

This is also em one of the reasons why nesting easily leads to compounding problems.

Example

   

   

<p>font-size inheritance example</p>

   

   

       

This is a paragraph of body text.

       

This is a smaller piece of explanatory text.

   


Best Practices

In real projects, it is not recommended to hard-code all text to px.

A better approach is to use the root font size, CSS variables, and clamp() to build a stable font-size system.

Not recommended Recommended approach Reason
font-size: 12px; used for large bodies of text. font-size: 1rem; Body text that is too small hurts the reading experience.
All levels of components use em. Use the global font size rem, then use relative units for local scaling, em. to avoid losing control of font sizes due to nested compounding.
For headings, only write font-size: 6vw;. font-size: clamp(1.75rem, 4vw, 3.5rem); to avoid being too small on small screens and too large on big screens.
Defining font sizes arbitrarily on different pages. Use CSS variables to unify the font-size scale. Makes it easier to maintain a consistent visual system.

Example

/* recommended: use CSS variables to manage the font-size system */

:root

{

    --font-size-xs

:

0.75rem

;

    --font-size-sm

:

0.875rem

;

    --font-size-base

:

1rem

;

    --font-size-lg

:

1.125rem

;

    --font-size-xl

:

clamp

(

1.5rem

,

3vw

,

2.5rem

)

;

    --font-size-hero

:

clamp

(

2rem

,

6vw

,

4.5rem

)

;

}

body

{

   

font-size

:

var

(

--font-size-base

)

;

   

line-height

:

1.6

;

}

.caption

{

   

font-size

:

var

(

--font-size-sm

)

;

}

.article-title

{

   

font-size

:

var

(

--font-size-xl

)

;

}

.hero-title

{

   

font-size

:

var

(

--font-size-hero

)

;

}

Generally, you can use rem to establish a global font-size system, use em for local proportional scaling, use clamp() for responsive headings.

Recommended font scale system

Here is a font-size token system based on the Modular Scale idea.

Each token uses clamp() to ensure a suitable size on both the smallest and largest screens.

Token Purpose Recommended clamp() values Notes
--text-xs Labels and badges clamp(0.7rem, 0.5vw + 0.5rem, 0.8rem) Not recommended below 11px.
--text-sm Helper text and captions clamp(0.85rem, 0.8vw + 0.6rem, 0.95rem) Common in annotations and secondary information.
--text-md Body text baseline clamp(1rem, 1vw + 0.75rem, 1.125rem) Recommended as the default body font size.
--text-lg Subtitle clamp(1.15rem, 1.5vw + 0.8rem, 1.5rem) Small headings for articles or cards.
--text-xl h3-level heading clamp(1.25rem, 2vw + 0.75rem, 2rem) Section heading.
--text-2xl h2-level heading clamp(1.5rem, 2.5vw + 1rem, 2.5rem) Chapter heading.
--text-3xl h1-level heading clamp(2rem, 4vw + 1rem, 3.5rem) Main page title.
--text-hero Display headline clamp(2.5rem, 6vw + 1rem, 5rem) Home page or landing page title.

Example

/* recommended: use clamp() + CSS variables to manage the font-size system */

:root

{

    --text-xs

:

  clamp

(

0.7rem

,

0.5vw

0.5rem

,

0.8rem

)

;

    --text-sm

:

  clamp

(

0.85rem

,

0.8vw

0.6rem

,

0.95rem

)

;

    --text-md

:

  clamp

(

1rem

,

1vw

0.75rem

,

1.125rem

)

;

    --text-lg

:

  clamp

(

1.15rem

,

1.5vw

0.8rem

,

1.5rem

)

;

    --text-xl

:

  clamp

(

1.25rem

,

2vw

0.75rem

,

2rem

)

;

    --text-2xl

:

 clamp

(

1.5rem

,

2.5vw

1rem

,

2.5rem

)

;

    --text-3xl

:

 clamp

(

2rem

,

4vw

1rem

,

3.5rem

)

;

    --text-hero

:

clamp

(

2.5rem

,

6vw

1rem

,

5rem

)

;

}

body

{

   

font-size

:

var

(

--text-md

)

;

   

line-height

:

1.6

;

}

h1

{

font-size

:

var

(

--text-3xl

)

;

}

h2

{

font-size

:

var

(

--text-2xl

)

;

}

h3

{

font-size

:

var

(

--text-xl

)

;

}

small

{

font-size

:

var

(

--text-sm

)

;

}

Recommended and avoided practices

Below are recommended and not-recommended practices for several common scenarios.

Scenario Not recommended Recommended approach
Root font-size setting html { font-size: 10px; } html { font-size: 100%; }
Body font size p { font-size: 14px; } p { font-size: 1rem; }
Heading font size h1 { font-size: 48px; } h1 { font-size: clamp(1.75rem, 4vw + 1rem, 3.5rem); }
Small text small { font-size: 11px; } small { font-size: max(0.75rem, 12px); }
Responsive font size Use multiple @media breakpoints to switch fixed values Use clamp() to achieve fluid transitions
Accessibility html { font-size: 62.5%; } Do not set the root font size, or use 100%

Do not use html or body a fixed value on px to set the root font size, which overrides the user's preferred font size set in the browser and breaks large-font accessibility features.


Deriving the clamp() fluid font-size formula

clamp() 's middle value usually uses a viewport unit plus a rem base.

This forms an "anchor + slope" linear interpolation formula, precisely controlling the rate at which the font size changes across different screen widths.

Formula derivation steps

Suppose we need the font size to transition smoothly from 16px to 24px between viewport widths of 320px and 1240px.

Step Formula This example calculates
1. Calculate the slope (max px - min px) / (max vp - min vp) (24 - 16) / (1240 - 320) = 0.00869, approximately 0.87vw
2. Calculate the intercept min px - slope x min vp 16 - 0.00869 x 320 = 13.2px, approximately 0.825rem
3. Derive the preferred value slope x 100vw + intercept 0.87vw + 0.825rem

Example

/* formula: preferred = slope x 100vw + intercept */

/* slope = (max px - min px) / (max vp - min vp) */

/* intercept = min px - slope x min vp */

/* example: transition from 16px to 24px across 320px to 1240px viewports */

/* slope = (24-16) / (1240-320) = 0.00869 ~= 0.87vw */

/* intercept = 16 - 0.00869x320 ~= 13.2px ~= 0.825rem */

p

{

   

font-size

:

clamp

(

1rem

,

0.87vw

0.825rem

,

1.5rem

)

;

}

clamp() Supported in Chrome 79+, Firefox 75+, Safari 13.1+, Edge 79+, so it is safe for production use.


Details of the min() and max() functions

min(a, b, ...) Returns the smallest of several values, equivalent to setting an "upper limit".

max(a, b, ...) Returns the largest of several values, equivalent to setting a "lower limit".

They can take any number of arguments, and different units can be mixed among the arguments.

function Behavior effect Applicable scenarios
min() Selects the smallest among all arguments Sets an upper limit Prevents the font size from being too large.
max() Selects the largest among all arguments Sets a lower limit Ensures a minimum readable font size.
clamp() Limits the minimum, preferred, and maximum at the same time Sets both upper and lower limits A complete fluid font-size solution.

Example

/* min() - font size does not exceed 2rem */

h2

{

   

font-size

:

min

(

5vw

,

2rem

)

;

}

/* max() - font size is not below 1rem */

small

{

   

font-size

:

max

(

0.75vw

,

1rem

)

;

}

/* combination: min and max are equivalent to clamp */

/* clamp(MIN, VAL, MAX) is equivalent to max(MIN, min(VAL, MAX)) */

h1

{

   

font-size

:

max

(

1.5rem

,

min

(

4vw

,

3rem

)

)

;

}

/* multiple arguments: take the smallest of the three */

.label

{

   

font-size

:

min

(

1rem

,

3vw

,

14px

)

;

}

/* mixed units */

.fluid

{

   

font-size

:

max

(

12px

,

1.2vw

0.5rem

)

;

}


Details of the calc() function

calc() Supports the four basic arithmetic operations and allows mixing any units.

In font-size scenarios, it is often used with rem and vw together.

Rules of arithmetic operations

Operation Syntax Limitation Example
Addition calc(1rem + 0.5vw) Spaces are required on both sides of an operator. font-size: calc(1rem + 0.5vw);
Subtraction calc(2rem - 4px) Spaces are required on both sides of an operator. font-size: calc(2rem - 4px);
Multiplication calc(1.5 * 16px) One of the values must be unitless. font-size: calc(1.5 * 16px);
Division calc(24px / 1.5) The right side must be a unitless number and must not be 0. font-size: calc(24px / 1.5);

calc(1rem+2px) is wrong; spaces should be on both sides of operators, the correct form is calc(1rem + 2px).

Example

/* basic arithmetic operations */

font-size

:

calc

(

1rem

0.5vw

)

;

     

/* addition */

font-size

:

calc

(

2rem -

4px

)

;

       

/* subtraction */

font-size

:

calc

(

1.5

16px

)

;

       

/* multiplication (one must be a unitless number) */

font-size

:

calc

(

24px

/

1.5

)

;

       

/* division (right side must be a unitless number) */

/* combining with CSS variables */

:root

{

    --scale

:

1.25

;

    --base

:

1rem

;

}

h3

{

   

font-size

:

calc

(

var

(

--base

)

var

(

--scale

)

)

;

}

/* nested calc (modern browsers allow omitting the inner calc) */

font-size

:

calc

(

clamp

(

1rem

,

2vw

,

2rem

)

0.8

)

;

Using calc to build a modular font scale

Pass calc() and CSS variables, you can build a font-size scale based on a fixed ratio.

The following example uses the Major Third (1.25) ratio.

Example

/* use calc + CSS variables to implement the Major Third (1.25) ratio */

:root

{

    --ratio

:

1.25

;

    --step-

0

:

1rem

;

                                     

/* 16px */

    --step-

1

:

calc

(

var

(

--step-

0

)

var

(

--ratio

)

)

;

       

/* 20px */

    --step-

2

:

calc

(

var

(

--step-

1

)

var

(

--ratio

)

)

;

       

/* 25px */

    --step-

3

:

calc

(

var

(

--step-

2

)

var

(

--ratio

)

)

;

       

/* 31.25px */

    --step--

1

:

calc

(

var

(

--step-

0

)

/ var

(

--ratio

)

)

;

       

/* 12.8px */

}

p    

{

font-size

:

var

(

--step-

0

)

;

}

h3    

{

font-size

:

var

(

--step-

1

)

;

}

h2    

{

font-size

:

var

(

--step-

2

)

;

}

h1    

{

font-size

:

var

(

--step-

3

)

;

}

small

{

font-size

:

var

(

--step--

1

)

;

}


Inheritance and cascade

font-size is an inheritable property.

If a child element does not explicitly set a font size, it inherits the parent's computed value, not the declared value.

The inheritance computation process

The following shows a typical font-size inheritance chain.

Selector Declared value The computed result Description
html 100% 16px Browser default value.
body inherit 16px Inherited from html.
.article 1.125rem 18px Computed based on the root font size.
.article p inherit 18px What is inherited is the computed 18px, not 1.125rem.
.article em 1em 18px em Based on the parent's computed value, 1 x 18px = 18px.

Specificity and cascade order

When multiple rules set font-size at the same time, they take effect according to the following priority from high to low.

Precedence Source Example
1 (highest) !important Inline Styles

2 !important Stylesheet rules p { font-size: 20px !important; }
3 Inline Styles

4 ID selector #title { font-size: 24px; }
5 Class / pseudo-class / attribute selector .heading { font-size: 22px; }
6 Element / pseudo-element selector p { font-size: 16px; }
7 (lowest) Inherited / browser default styles From the parent element or browser default.

Example

/* specificity from high to low */

/* 1. !important inline style */

/*

*/

/* 2. !important stylesheet rule */

p

{

font-size

:

20px

!important

;

}

/* 3. inline style */

/*

*/

/* 4. ID selector */

#title

{

font-size

:

24px

;

}

/* 5. class/pseudo-class/attribute selector */

.heading

{

font-size

:

22px

;

}

/* 6. element/pseudo-element selector */

p

{

font-size

:

16px

;

}

/* 7. inherited / browser default styles */

Do not use html or body a fixed value on px setting the root font size, because this overrides the user's browser font preference. Please use html { font-size: 100%; } or simply do not set it.


Interactive demo

Through the demo below, you can see the font-size effects of different units intuitively.

Select a unit type and drag the value slider; the preview area updates in real time.

font-size unit conversion and preview

16px

CSS font-size tutorial sample text


Summary

font-size is a fundamental property of the CSS typography system.

px suits precise control,rem suits a global font-size system,em suits local scaling.

Viewport units suit fluid typography, but it is best to combine them with clamp() to limit the upper and lower bounds.

In real projects, it is recommended to use CSS variables for unified font-size management, and to prioritize readability and responsive experience.

Recommended for on-screen body text rem + clamp(), and use pt for print styles pt.

Do not lock the root font size, and always set a minimum lower bound for small text (recommended not below 12px / 0.75rem).

For component-level responsiveness, prefer container query units cqi / cqb.


Related Articles

CSS Tutorial: CSS Font

CSS Reference Manual: font property