DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Trending

  • Unlocking AI Coding Assistants Part 2: Generating Code
  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  1. DZone
  2. Coding
  3. Languages
  4. CSS Relative Font Size

CSS Relative Font Size

In this article, we look at several different ways in which front-end web developers can manipulate the font fo their web page using CSS.

By 
Tatjana Boskovic user avatar
Tatjana Boskovic
·
Feb. 20, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
101.5K Views

Join the DZone community and get the full member experience.

Join For Free

One of the biggest confusions in web design is caused by none other than the font-size property.

Alongside with this property, confusion is commonly caused by CSS Columns. 

If you are interested in layout properties, check out this article about the property that is used often - position. 

In CSS, there are multiple units that can be used which can only cause the designer additional headache. In this article, we will clarify the usage of those units and any misconceptions.

PX Unit

The most common and popular unit is the pixel (px) unit. Most people start with using the pixel unit because it gives you full control over the text size. If the font size is not specified, the default size for normal text, like a paragraph, is 16px.

The main problem with using pixel units is that they are not scalable and making changes in the font size on different screen sizes can be challenging.

Also, the px unit is not related to anything. So if you want to change the size of, for example, the entire page based on the screen size, you would have to change the font size of each element individually.

EM Unit

The em unit is a scalable font size unit. It is related to the font size of the parent container. One em (1em) is equal to the current font size. So for example, if the parent element has the font size of 16px than 1em is equal to 16px, 2em is equal to 32px, and so on.

Making your design responsive becomes much easier if you use em units instead of px.

But, there is one thing that needs to be looked after. When using ems you should be careful with nesting.

For example, let's say you created a section and set its font size to 2ems. Now, you want to add a paragraph within the section that has a font size of 1em. The paragraph's font size is related to the font size of the section. If the nesting continues, or if it is used on multiple segments of the project, you can quickly lose sight of what is relative to what, and get completely lost.

Now, it is time to introduce the rem unit.

REM Unit

The rem unit is another scalable font size, but unlike the em unit, it is related to the root element (HTML) instead of the parent element. That's where it got its name (root em = rem).

This means that, if you use the rem unit on elements, you can quickly change the font size of the entire project, just by adjusting the root font size. This way is fast, easy, and avoids any nesting complications you might get while using the em unit.

So, Which Unit Should You Use?

The best way to answer this question is by using an example.

First, we will use the px unit.

html { font-size: 100% } //usually 16px 
div { font-size: 16px; } 
div>p { font-size: 14px; } 

Here, you can see that the font size is set individually for each element, and they have no relation between each other.

Next, we'll use the em unit for the same code segment.

html { font-size: 100% } 
div { font-size: 0.875em; } // 14px 
div>p { font-size: 2em; } // 28px 

In this example, the difference between the px and em units is clear. And, the relativeness of the em unit is clear. Just by changing the font size of the container div, we can see that the paragraph font size updated accordingly.

Finally, using the rem unit.

html { font-size: 100% } 
div { font-size: 1rem; } // 16px 
div>p { font-size: 1.5rem; } // 24px 

When using the rem unit, it is clear that all font sizes are related to the root font size. Both the div and the paragraph font sizes are related to the root, despite the div being the parent of the paragraph.

Conclusion

There are no right or wrong units. It all depends on your skill level, project type, and size and personal preference.

If you would like to have complete control over the font size, the pixel unit is for you. If you would like a little bit more flexibility when manipulating the web page than the em or rem units are the best choices. Either way, the best result will be achieved when the units are fully understood.

Check out other detailed articles related to CSS properties such as this one: CSS Positions,  SASS and LESS Nesting.

CSS

Published at DZone with permission of Tatjana Boskovic, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: