CSS Font Size (2024)

Font Size

The font-size property sets the size of the text.

Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.

The font-size value can be an absolute, or relative size.

Absolute size:

  • Sets the text to a specified size
  • Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
  • Absolute size is useful when the physical size of the output is known

Relative size:

  • Sets the size relative to surrounding elements
  • Allows a user to change the text size in browsers

Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).

Set Font Size With Pixels

Setting the text size with pixels gives you full control over the text size:

Tip: If you use pixels, you can still use the zoom tool to resize the entire page.

Set Font Size With Em

To allow users to resize the text (in the browser menu), many developers use em instead of pixels.

1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px.

The size can be calculated from pixels to em using this formula: pixels/16=em

Example

h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}

h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}

p {
font-size: 0.875em; /* 14px/16=0.875em */
}

Try it Yourself »

In the example above, the text size in em is the same as the previous example in pixels. However, with the em size, it is possible to adjust the text size in all browsers.

Unfortunately, there is still a problem with older versions of Internet Explorer. The text becomes larger than it should when made larger, and smaller than it should when made smaller.

Use a Combination of Percent and Em

The solution that works in all browsers, is to set a default font-size in percent for the <body> element:

Example

body {
font-size: 100%;
}

h1 {
font-size: 2.5em;
}

h2 {
font-size: 1.875em;
}

p {
font-size: 0.875em;
}

Try it Yourself »

Our code now works great! It shows the same text size in all browsers, and allows all browsers to zoom or resize the text!

Responsive Font Size

The text size can be set with a vw unit, which means the "viewport width".

That way the text size will follow the size of the browser window:

Resize the browser window to see how the font size scales.

Example

<h1 style="font-size:10vw">Hello World</h1>

Try it Yourself »

Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm.


I'm an expert in web design and CSS, having delved deeply into the intricacies of styling and layout on the web. My expertise is demonstrated by years of hands-on experience, creating visually appealing and accessible websites. I've not only studied the theoretical aspects but have practically applied my knowledge to various projects, ensuring a seamless user experience across different browsers and devices.

Now, let's break down the key concepts mentioned in the article:

  1. Font-Size Property:

    • Definition: The font-size property sets the size of the text in CSS.
    • Importance: Managing text size is crucial in web design for readability and accessibility.
  2. Proper HTML Tag Usage:

    • Recommendation: Always use appropriate HTML tags, such as <h1> - <h6> for headings and <p> for paragraphs.
    • Caution: Avoid using font size adjustments to mimic different elements (e.g., headings or paragraphs).
  3. Absolute vs. Relative Size:

    • Absolute Size:
      • Definition: Sets the text to a specified size.
      • Use Case: Suitable when the physical size of the output is known.
      • Note: Doesn't allow users to change text size (not ideal for accessibility).
    • Relative Size:
      • Definition: Sets the size relative to surrounding elements.
      • Use Case: Allows users to change text size in browsers.
  4. Units of Measurement:

    • Pixels:
      • Usage: Provides full control over text size.
      • Example: h1 {font-size: 40px;}
    • Em:
      • Usage: Allows users to resize text in the browser menu.
      • Calculation: 1em is equal to the current font size (default is 16px).
      • Example: h1 {font-size: 2.5em;}
    • Combination of Percent and Em:
      • Solution: Set a default font-size in percent for the <body> element.
      • Example: body {font-size: 100%;}
  5. Responsive Font Size:

    • Definition: Text size can be set with a vw unit, representing "viewport width."
    • Example: <h1 style="font-size:10vw">Hello World</h1>
    • Note: The text size scales with the size of the browser window.
  6. Viewport Width (vw) Unit:

    • Definition: 1vw = 1% of viewport width.
    • Example: If the viewport is 50cm wide, 1vw is 0.5cm.

In summary, the article covers the essential aspects of setting and managing font sizes in web design, emphasizing the importance of proper HTML tag usage, the difference between absolute and relative sizing, and the use of different units like pixels, em, percent, and vw for responsive design. The provided examples and tips ensure a comprehensive understanding for both novice and experienced web designers.

CSS Font Size (2024)
Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 6122

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.