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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Solve Common Sizing Problems with CSS3's New rem Unit

John Esposito user avatar by
John Esposito
·
Dec. 09, 11 · Interview
Like (0)
Save
Tweet
Share
5.18K Views

Join the DZone community and get the full member experience.

Join For Free

CSS promised to keep layouts well-proportioned, ensuring consistent measurement by including its own units.

Besides pixels, the most commonly used CSS unit is probably the em, which is simply the computed font-size of the current element. Using this font-relative unit, designers can keep the relation between text and non-text constant -- crucial for pleasing and intelligible design.

The em proved a bit unexpectedly annoying in more complex layouts, because the em is calculated relative to font-size of the current element -- whose font-size may already have been calculate relative to another element. As Jonathan Snook explains, in a popular post:

 

The problem with em-based font sizing is that the font size compounds. A list within a list isn't 14px, it's 20px. Go another level deeper and it's 27px! These issues can be worked around by declaring any child elements to use 1em, avoiding the compounding effect.


The only way around this before CSS3 was to reset every child element's font-size to 1em, cutting off all compounding effects.

But that's obviously a bit of a hack, and consequently easy to forget about.

The solution? CSS3's rem (root element) unit -- measured relative to the root element, as the name suggests, not the current element.

How to do it? You can probably guess already, but if not, here's Jonathan's full explanation. (Works in all major browsers, too, now that Opera is in 11.60.)

If you've wrestled with ems before, chances are you'll find rem extremely useful at some point.

 

Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Steps for Getting Started in Deep Learning
  • 11 Observability Tools You Should Know
  • Testing Level Dynamics: Achieving Confidence From Testing
  • 10 Most Popular Frameworks for Building RESTful APIs

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: