Font-size - Property CSS

font-size

Summary of characteristics of the font-size property

Quick description
Adjusts the font size, which also impacts units like em, ex, etc.
Status
Standard
Usable for
HTML
Predefined values
medium | small | x-small | xx-small | large | x-large | xx-large | xxx-large | smaller | larger
Percentages
Calculated based on the font size of the parent element.
Initial value
medium
Inherited by default
Yes.
Animation type
Computed value : during an animation, the font-size property gradually changes from one value to another.
W3C Module
CSS Fonts Module
References (W3C)
 đꎇ  
 đꎅ  
Document status: WD (Working Draft)

Document status: WD (Working Draft)

Document status: REC (Recommendation)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Syntax diagram of font-size.

Font-size property - Syntax diagramSyntax diagram of the font-size CSS property. See stylescss.free.fr for details. xx-small xx-small x-small x-small small small medium medium large large x-large x-large xx-large xx-large xxx-large xxx-large smaller smaller larger larger length / % length / % math mathfont-size:;font-size:;
Syntax diagram of the font-size property.
Detailed syntax

On the diagram, the terms in bold are the predefined words of CSS, the other terms are described below :

  • length is a positive numeric value followed by one of the CSS dimension units.
  • % is a percentage calculated relative to the character size of the parent element.

Description.

The font-size property sets the font size.

The actual font size may be significantly different from the requested typeface, due to font-size-adjust . However, children inherit the computed value of font-size that is not affected by font-size-adjust, otherwise the effect of font-size-adjust would be applied twice.

To maintain readability, font sizes smaller than 9 pixels should never be used.

For variable fonts, the font size is a factor of em. For non-variable fonts, the size is converted to absolute units, and compared to the one declared in the policy.

The different uses of font-size.

The word font-size corresponds to a property, allowing you to choose a font size (which is described on this page), but also to a descriptor usable with the following guidelines:

  • @font-face : Defines all settings for a downloadable font.

Values for font-size.

  • 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; font-size: xxx-large;

    Character sizes predefined by CSS. The default value is medium.
    Here is the ratio between these different sizes, relative to medium.

    Value CSSRatio to
    medium
    Equivalent
    title HTML
    Equivalent
    size HTML
    xx-small 0.6 (3/5) h6font size="1"
    x-small 0.75 (3/4)
    small 0.89 (8/9) h5font size="2"
    medium 1 h4font size="3"
    large 1.2 (6/5) h3font size="4"
    x-large 1.5 (3/2) h2font size="5"
    xx-large 2 h1font size="6"
    xxx-large 3 font size="7"

    In CSS1, the multiplier coefficient between two successive sizes was recommended at 1.5. In CSS2, it has been reduced to get closer to 1.2. Currently this coefficient varies around 1.18 - 1.25.


    Here's what these different sizes look like on your browser:

    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
    font-size:xxx-large
  • font-size: 12px;

    A positive numeric value, followed by a dimension unit the (see CSS dimension units). Some units are specifically adapted to font sizes: pt, em, rem, ch, ex, etc.
    The value must be positive.
    If the value is expressed in percentages, the percentages are calculated relative to the character size of the parent element.
    The em unit is evaluated in the same way, while the rem unit is evaluated against the character size of the root element (html tag or :root selector).

    Parent element, font-size:medium
    Child element, font-size:150%
  • font-size: smaller; font-size: larger;

    The character size of the target element is set relative to the character size of the parent element.

    If the parent element has a character size set to a predefined value (from xx-small to xxx-large), the smaller value is the predefined size immediately below.
    And larger is the preset size immediately above.

    If the parent element is in a non-predefined character size, the browser calculates the character size of the targeted element by applying a coefficient.

    Parent element, font-size:medium
    Child element, font-size:larger
    Child element, font-size:smaller
  • font-size: math;

    Specific rules for writing mathematical formulas are applied to determine the size of the characters: the font size varies depending on the math-depth property.

    Example:

    x + 5 2

Common values:

font-size: initial (medium) font-size: inherit font-size: revert font-size: revertLayer font-size: unset

These values ​​are described in more detail on their respective page: initial, inherit, revert, revert-layer, unset.

Animation of the font-size property.

The animation of the character size is very smooth, even if the animation is described with the predefined values (small, medium, etc.) because these values are equivalent to numbers, which allows to calculate as many intermediate values as necessary.

The quick brown fox jumps over the lazy dog

Handling the font-size property by Javascript.

Change the value of font-size to Javascript.

The code below, in Javascript, changes the value of font-size for an el element. The name of the property can be written in kebab-case (a dash to separate words) or in camel-case (a capital letter to separate the words), Javascript accepting both syntaxes.

Javascript
let el = document.getElementById('id'); let value = el.style['font-size']; // or let el = document.getElementById('id'); let value = el.style.fontSize;

Read in Javascript the value of font-size.

For this code to work, the property must have been assigned directly to the element itself and not via a CSS selector. The value is returned as defined: in the same unit, or optionally a predefined value (small, medium, etc.).

Javascript
let el = document.getElementById('id'); el.style['font-size'] = '2rem'; // or let el = document.getElementById('id'); el.style.fontSize = '2rem';

Javascript, read the computed value of font-size.

The computed value is the value that results from the evaluation of relative units, in particular in the case of font sizes, units such as rem, em, etc. If no font-size value is set for this element, the calculated value is determined by inheritance. The calculated value is rendered in pixels, regardless of the unit used to define it.

JQuery: change the value of the font-size property.

JQuery

$('#id').css('font-size', '2rem');
// or
$('#id').css('fontSize', '2rem');

JQuery: read the computed value of the font-size property.

As in Javascript, the value is returned in pixels, regardless of the unit used to define it.

JQuery
let value = $('#id').css('font-size');

Other code samples.

More examples of Javascript and JQuery code are given on the Javascript and CSS page.

Test it yourself.

The buttons below apply the entered value to the font-size property and then display either the value as applied, or the calculated value. This second option allows you to see how the value of font-size is stored (serialized). In particular, we see that all units are converted into pixels.

Simulator.

font-size :
Pack my box with five dozen liquor jugs

Browser compatibility.

Column 1
Support for the font-size property to set the font size.
Column 2
Support for the xxx-large value for the font-size property. This value was updated after the property was initially set, but is now supported correctly.
1
font-size
property
2
xxx-large
value
Estimated overall support.
96%
93%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Opéra

Safari

Safari sur IOS

Firefox pour AndroĂŻd

Samsung Internet

Chrome

Edge

Firefox

AndroĂŻd Brower

Chrome pour AndroĂŻd

Baidu Browser

QQ Browser

UC Browser pour AndroĂŻd

Opéra mini

font-size property historic.

font-size is an old property (already present on the first CSS specification). Some stocks have have been added as new versions of the specification are released.

  • Cascading Style Sheets specification - Version 1

    Initial definition of the font-size property.
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    Support for new values for property:
    - A value followed by a size unit or a percentage.
    - The inherit value.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • CSS Fonts Module Level 3

    No change regarding this property.
    WD
    July 21, 1997
    Working Draft.
    CR
    October 03, 2013
    Candidate Recommendation.
    PR
    August 14, 2018
    Proposed for recommendation.
    REC
    September 20, 2018
    Recommendation .
  • CSS Fonts Module Level 4

    Adding the math value.
    Adding xxx-large value.
    WD
    July 11, 2017
    Working Draft.
    CR
    PR
    REC
  • CSS Fonts Module Level 5

    No changes to the property itself.
    font-size is also a descriptor for the @font-face directive.
    WD
    June 29, 2021
    Working Draft.
    CR
    PR
    REC

See also, about fonts.

Properties that concern to fonts are grouped together in the CSS Fonts Module module. You can find their definition below:

Properties:

font
Langue française
Shorthand to set most properties for fonts.
âś—
font-effect
Langue française
Allows you to apply some effects to the text: relief, border...
font-family
Selects a font from those that are available or downloaded.
âš 
font-feature-settings
Langue française
Permet d’exploiter les fonctionnalités spécifiques des polices de caractères OTF, WOFF, etc.
font-kerning
Langue française
Adjusting the kerning between characters.
âš 
font-language-override
Langue française
Defines the language to be considered when choosing characters.
âš 
font-optical-sizing
Langue française
Optimizes the shape of characters based on their size.
font-palette
Langue française
Defines the color palette that can be used for colored fonts.
font-size-adjust
Langue française
Definition of the aspect factor of the font.
âś—
font-smooth
Langue française
Defines whether and which smoothing algorithm should be used.
font-stretch
Langue française
Changes the stretching or compression of characters.
font-style
Langue française
Selects the italic or oblique font styles.
font-synthesis
Langue française
Allows or disallows calculation of missing styles in the typeface.
âš 
font-synthesis-position
Langue française
Whether or not allows the browser to synthesize characters in superscript or subscript.
âš 
font-synthesis-small-caps
Langue française
Whether or not allows the browser to calculate the glyphs of characters in small caps.
âš 
font-synthesis-style
Langue française
Defines whether the browser is allowed to synthesize italic forms of characters.
âš 
font-synthesis-weight
Langue française
Defines whether or not the browser can calculate the bold glyph.
font-variant
Langue française
Shorthand to define advanced typographic attributes: ligatures, character substitution, etc.
font-variant-alternates
Langue française
Defines character substitution (OTF, WOFF fonts).
font-variant-caps
Langue française
Applies character capitalization.
font-variant-east-asian
Langue française
Typography specific to Chinese or Japanese characters.
âš 
font-variant-emoji
Langue française
Choose between an emoji or text presentation.
font-variant-ligatures
Langue française
Enables or disables character ligation.
font-variant-numeric
Langue française
Defines how evolved numbers are displayed: fractions, numbers, etc.
font-variant-position
Langue française
Superscript or subscript of characters.
âš 
font-variation-settings
Langue française
Provides access to the stylistic possibilities of OTF fonts.
font-weight
Selects a weight for the characters.
font-width
Langue française
Synonymous with font-stretch (character width/enlargement).

Functions:

âš 
palette-mix()
Langue française
Establish the mixture of two color palettes from two different typefaces.

At-rules:

@font-face
Langue française
Defines all settings for a downloadable font.
âš 
@font-feature-values
Langue française
Defines the typographic variations to use.
@font-palette-values
Langue française
Defines a color palette that can then be applied to a font.

Descriptors :

ascent-override
Langue française
Descriptor for @font-face. Sets the height of the characters above the baseline.
descent-override
Langue française
Descriptor for @font-face. Sets the height of the characters below the baseline.
âš 
font-display
Langue française
Descriptor for @font-face. Determines how the browser behaves when faced with fonts that take a long time to load.
line-gap-override
Langue française
Descriptor for @font-face. Sets the line spacing of the font.
src
Langue française
Defines the source of a font file to download, or an image to display.
unicode-range
Langue française
Descriptor for @font-face. Defines the range of character codes to be downloaded into a font.