Placeholder canvas

Font Size in CSS: The Easiest Guide On The Internet (2024)

font size in CSS

The font size in CSS is like telling your computer how big or small you want the text to appear on a specific webpage. It is very much like choosing the size of the letters so they are the most legible. The exact property can be applied to any element and has a default value of medium. 

In web development, text presentation is one of the most vital factors directly affecting a user’s experience. Cascading Style Sheets (CSS) are essential for defining how web content appears, and among the many other styling properties, font size is one of the crucial elements. The text size on a webpage has a significant impact on readability, aesthetics, and overall accessibility. 

Developers can accurately and very quickly control and change the font size of their document with CSS, which lends them a range of options to create a responsive design that fits various devices and user preferences. Knowing how to use font size in CSS to modify the font size is essential for creating appealing and user-friendly websites in today’s age.

And this simple guide is all about the font size in CSS. This exploration of CSS font size will cover the ideas, techniques, and best practices related to adjusting text dimensions, enabling developers to design interactive and user-friendly websites.

Let’s take a quick look at the syntax.

You can set the font size in CSS using the ‘font-size’ property. Here is the basic syntax that will help you develop your sail straight.

css

selector {

    font-size: value;

}

Selector: This is the HTML element or class to which you want to apply the font size.

Value: It represents the desired font size. You can easily specify the size using various units such as pixels (`px`), em units (`em`), percentages (%), viewport width (`vw`), or absolute length units (‘cm’, ‘mm’, `in`, `pt`, `pc`).

Examples:

"`css

/* Font size in pixels */

body {

    font-size: 16px;

}

/* Font size in em units */

h1 {

    font-size: 2em;

}

/* Font size in percentages */

p {

    font-size: 120%;

}

/* Font size in viewport width (vw) */

div {

    font-size: 5vw;

}

/* Font size in centimeters (cm) */

span {

    font-size: 1.5cm;

}

```

You can choose the appropriate unit based on your design requirements and the level of responsiveness you want to achieve in your web layout.

As a user, you can easily adjust these values based on your design requirements. However, remember that using relative units like ’em’ or ‘%’ can be beneficial for creating more flexible, responsive, and overall better layouts.

The Absolute and Relative Font Size In CSS

Font Size in CSS can be specified using absolute or relative units.

Understanding these units’ differences is vital for creating more flexible and responsive designs that cater to your audience’s needs.

One more thing to remember is that while using absolute size is useful when we know the physical size of the output, it is impossible to tweak the text’s size across all browsers. 

Absolute Font Size In CSS:

Absolute units provide a fixed measurement that does not change with the user’s device’s context or settings. The most common fundamental unit for specifying font size is the pixel (‘px’). Here’s an example:

css

body {

    font-size: 16px;

}

In this example, the font size for the entire document is set to 16 pixels. While pixel-based sizing provides precise control, it may adapt poorly to varying screen sizes and user preferences.

Relative Font Size In CSS:

Relative units are more flexible and responsive based on the context of the parent element or the user’s preferences. Standard relative units include:

’em:’ The ’em’ unit is relative to the parent element’s font size. For example:

       css

    h1 {

        font-size: 2em; /* Twice the size of its parent element */

    }

‘rem:’ is Similar to ’em’ but relative to the root element’s font size (‘HTML), providing a consistent base. Example:

       css

    body {

        font-size: 16px;

    }

    p {

        font-size: 1.2rem; /* 1.2 times the root element’s font size (16px) */

    }

‘% (Percentage):’ The percentage is relative to the parent element’s font size. For instance:

       css

    div {

        font-size: 150%; /* 150% of its parent element’s font size */

    }

Relative units are favored for responsive design because they adapt more naturally to different screen sizes and user preferences, making it easier to create layouts that are accessible and visually appealing across various devices.

In practice, a combination of absolute and relative font sizes may be employed to balance precision and adaptability in web design. 

To remember it easily, pair the absolute font size with the word exclusive in your mind, and for the relative font size, pair the word with all-inclusive(since it considers different screen sizes). That’s it. This will help you remember their roles much more accessible.

Note: The default size for regular text, such as paragraphs, is 16 px, or 1 em. This is for when you have yet to specify a font size up front. The formula for converting the size from pixels to em is em = pixels/16. So basically, 1 em is equal to 16 pixels!

Let us now take a look at another critical topic.

Read Also: Harnessing The Power Of Top 15 CSS Frameworks: Responsive Web Design Unleashed

Font-Size with Pixels

By setting the font size in pixels, we get more control over the size of our document text. Let’s see how to set the font size in pixels below:

Example

<!DOCTYPE html>  

<html>  

<head>  

<style>  

#A {  

  font-size: 32px;  

}  

#B {  

  font-size: 20px;  

}  

</style>  

</head>  

<body>  

<p id="first"> This is a paragraph with a size of 32px. </p>  

<p id= "second"> This is another paragraph with a font size of 20px. </p>  

</body>  

</html>

Font-Size with ‘em’ in CSS.

Let’s see an example of converting font sizes between pixels and em units in CSS. In this example, we’ll set a base font size for the ‘body’ element in pixels and then use ’em’ units for specific elements for the conversion.

“‘ css

/* Base font size in pixels for the entire document */

body {

    font-size: 16px;

}

/* Header with font size in em units */

h1 {

    font-size: 2em; /* 2 times the base font size (16px) = 32px */

}

/* Paragraph with font size in em units */

p {

    font-size: 1.2em; /* 1.2 times the base font size (16px) = 19.2px */

}

/* Example of converting em to pixels */

div {

    font-size: 1.5em; /* 1.5 times the base font size (16px) = 24px */

    line-height: 1.5; /* Using a line height of 1.5 for better readability */

}

/* Example of converting pixels to em */

span {

    font-size: 18px; /* Font size in pixels */

    font-size: 1.125em; /* Conversion to em: 18px / 16px (base font size) = 1.125em */

}

”’

In this example:

  • The ‘body’ element is used to set the base font size for the entire document to 16 pixels.
  • The ‘h1’ element uses ‘2em’, making its font twice the base font size (32 pixels).
  • The ‘p’ element uses ‘1.2em’, resulting in a font size of 1.2 times the base font size (19.2 pixels).
  • The ‘div’ element demonstrates converting ’em’ to pixels by setting its font size to ‘1.5em’, resulting in 24 pixels.
  • The’span’ element shows converting pixels to ’em’. First, it sets a font size of 18 pixels, then converts this to ’em’ by dividing by the base font size, resulting in ‘1.125em’.

Responsive Font Size in CSS

Here, we’ll use the ‘vw’ (viewport width) unit for font size, a relative unit based on the viewport’s width. Remember that ‘vw’ can help create responsive designs that adapt to the width of the user’s screen.

"' css

/* Base font size in pixels for the entire document */

body {

    font-size: 16px;

}

/* Header with font size in vw units */

h1 {

    font-size: 6vw; /* 6% of the viewport width */

}

/* Paragraph with font size in vw units */

p {

    font-size: 2.5vw; /* 2.5% of the viewport width */

}

/* Example of converting vw to pixels */

div {

    font-size: 4vw; /* 4% of the viewport width */

    line-height: 1.5; /* Using a line height of 1.5 for better readability */

}

/* Example of converting pixels to vw */

span {

    font-size: 18px; /* Font size in pixels */

    font-size: 1.125vw; /* Conversion to vw: 18px / 16px (base font size) = 1.125vw */

}

'''

In this example:

  • The ‘body’ element sets the base font size for the entire document to 16 pixels.
  • The ‘h1’ element uses ‘6vw’, making its font size 6% of the viewport width.
  • The ‘p’ element uses ‘2.5vw’, resulting in a font size of 2.5% of the viewport width.
  • The ‘div’ element demonstrates converting ‘vw’ to pixels by setting its font size to ‘4vw’.
  • The ‘span’ element shows converting pixels to ‘vw’. It first sets a font size of 18 pixels and then converts this to ‘vw’ by dividing it by the base font size.

Font size with the length property.

When you set the font size to a given length value, you’re developing a specific measurement for the size. 

The measurement can be in different units – pixels (px), em units (em), viewport width (vw), and more.

Remember that with the length property, you cannot use negative values in font size.

Let’s now look at the example below for a better understanding.

<!DOCTYPE html>   

<html>   

    <head>   

        <style>   

            .length {   

                color: blue;   

                font-size: 7cm;   

            }   

        </style>   

    </head>   

    <body>   

        <h1>font-size property</h1>   

        <p class = "length"> A paragraph having length 7cm.</p>   

    </body>   

</html>

Conclusion: font size in CSS

The property font size in CSS is a vital styling feature that gives developers the flexibility to change the text size on a webpage. It doesn’t matter what your content is about.

If the readers cannot READ, they will not NEED it! (your content.) Text that is not easily readable can increase your website’s bounce rate, which could be better SEO-wise.

The ability to play with font size in CSS gives you the much-needed flexibility to choose between units like pixels, em, percentages, viewport width, or absolute length units.

Choosing the suitable unit and using the relative sizing can significantly help you create a layout that is more legible and easy to access on various devices, ranging from smartphones to larger-screen laptops and sometimes even TV sets! You can head to this website and learn more about the best font size for the web!

I hope this simple guide on font size in CSS helped you better understand how to use the different syntaxes we saw earlier and deliver a better user experience for your audiences, which will help you create a better standing on the web.

Recommended Reading: 

Text Formatting In HTML And CSS: A 5 Step Tutorial

Resolving CSS Conflicts: A Comprehensive Guide

FAQs: font size in CSS

How does the vw unit in font size contribute to responsiveness?

The vw or viewport width, makes it easier to scale the fonts to the sizes that scale with the width of the user’s screen. So, if you want to create responsive content for your audience, then the VW unit is your best friend.

Why is relative sizing important in web design?

Relative sizing, like em or percentages, is crucial for creating responsive designs that react well to different screen sizes and user preferences. Like the viewport width, relative sizing is essential when drafting good SEO-friendly and responsive web content.

Can I use different units for font size within the same CSS file?

Yes, you can use other units for font size within the same CSS file. This allows you to choose the most appropriate unit for specific elements based on design requirements.

Are there best practices for choosing the suitable unit for font size?

Relative units, such as em or percentages, are generally used in best practices to promote responsiveness. The user experience and the design context are considered when choosing the appropriate unit for font size in CSS.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.