burger menu icon
WillMaster

WillMaster > Blog > CSS

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

CSS em Vs rem

The two most used font size-relative measurements in CSS are em and rem. Both refer to the width of the capital letter "M" in traditional fonts, like Arial. The font size affects the width.

Measurement 1em is relative to the size of the font being published. Size 2em is twice that.

Size 1rem is the width of the letter "M" at the browser's default font size.

The font size-relative measurements em and rem can be used for dimensions, spacing, and thickness. Here is a demonstration using em for measurements:

Adjust number to adjust font size:

Change my font size. The box width, border thickness, and padding all adjust accordingly.

The code for the demonstration is below. (How to adjust font sizes within a box is outside the scope of this article.)

<div style="font-size:20px; width:10em; border:.25em dotted black; padding:.5em;">
Change my font size. The box width, border thickness, and padding all adjust accordingly.
</div>

Note: If you change all the em sizes with rem sizes in the above code, there will be no size and dimension shifts. That's because rem is the browser default. It won't change unless the user changes the browser setting.

If you try to set a font size with an em value, the em value will be obtained from the parent element. If there is no parent, then the value of em will be the same as the rem value.

An em is variable. It changes whenever you change the font size.

A rem is not variable, generally a 16px "M". But the default browser size can be adjusted in browser settings.

Which font-relative measurement you use depends on which gives you the properties you desire.

For automatically adjustable things, like the demonstration, em typically works well. For boxes and spacings and fonts that you won't want changed, rem is the most likely candidate.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

Access-Restricted Page

Make a restricted-access page with logging.

Image Swap

Swap a series of images in and out of an IMG tag.

No-Menu Page Printing

Site visitors may print your web page without printing the menus.

JavaScript-Formatted Date and Time

A JavaScript function to publish date/time according to your custom format.

Friendly Anchor Link

How to insert a bit of space above a normal anchor link scroll position.

Form Field Prefill With a Tap

When a link is tapped, a form field is filled in.

Beacon

Send a beacon to your script. Optionally, also send information.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC