CSS Reference Property

font-size 4d36n

The font-size property is used to set the size, or height, of the font. 545d3f

Because the value of the font-size property is used to compute the values of exs are relative to the font size set on the element.

You can read more about relative length units in the CSS <length> entry.

The font size can be specified as absolute or relative values. Absolute values can be specified using predefined keywords or <percentage> values. Negative values for font sizes are not allowed.

The actual value of the font-size may differ from the computed value due a numerical value on font-size-adjust property entry.

The font size of an element cascades; this means that the element’s children will inherit the same font size as their parent, unless specified otherwise.

Official Syntax 4j1l4a

  • Syntax:

    font-size: <absolute-size> | <relative-size> | <length> | <percentage> | inherit  
  • Initial: medium
  • Applies To: all elements
  • Animatable: yes, as a <length>

Values 4w6m73

<absolute-size>
An <absolute-size> keyword refers to an entry in a table of font sizes computed and kept by the browser. Each keyword refers to a specific font size specified in the browser. Possible keywords are: xx-small, x-small, small, medium, large, x-large, and xx-large.
<relative-size>

In addition to the above absolute keywords, two relative keywords exist: larger and smaller, which calculate the font size of an element relative to its parent’s font size. So, if, for example, the parent’s font size is set to large and the element’s font size is set to larger, the element’s font size will be x-large.

<length>

See the <length> entry for a list of possible length values. Negative values are not allowed.

When relative length units are used, the font size is determined relative to the font size of the element’s parent. For example, a font size of ‘2em’ would compute to double the font size of the element’s parent.

<percentage>

See the <percentage> entry for a list of possible values. Negative values are not allowed.

Percentage values are computed relative to the font size of the element’s parent. For example, a font size of ‘50%’ will compute to half the font size of the element’s parent.

Notes 5i3os

As mentioned earlier, keep in mind that relative font sizes have a direct effect on other aspects of an element that use relative units that are related to the font size. If the element’s padding for example is set to ‘2em’ and the element’s font size is set to ’16px’, then the padding would compute to ’32px’.

Choosing relative or fixed font sizes depends on the design you’re working on. However, fixed font sizes are better avoided because they are not scalable—the will not be able to zoom the page in and increase the font size in some browsers, so in this case, using relative units is the way to go to maintain a more accessible and -friendly interface.

With that said, you need to keep in mind that using relative font sizes has effects on the element’s font size, and on other aspects of the design that use relative units such as ems, which compute their values relative to the value of the font size.

The topic of choosing and using font sizes is a big one, and elaborating on it would turn this entry into a long article. So we strongly encourage you to read the Techniques For Responsive Typography article—it covers everything you need to know about font sizing and creating responsive typography on the web, with a list of best practices and recommendations.

Examples 3y6d6i

font-size: 50%;
font-size: 1em;
font-size: 20px;
font-size: xx-small;
font-size: 200%;
font-size: 12pt;
                

Live Demo 176n59

Have a look at the live demo:

View this demo on the Codrops Playground

Browser s162l

The font-size property works in all major browsers: Chrome, Firefox, Safari, Opera, Internet Explorer, and on Android and iOS.

Written by

Last updated June 14, 2020 at 5:12 pm by Mary Lou

Do you have a suggestion, question or want to contribute? Submit an issue.