Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of using ems instead of % in CSS?

I understand the advantages of using ems in favor of static measurements like pixels and points, but why do most of the CSS people out there (SO excepted) prefer using ems instead of % values?

like image 430
Alix Axel Avatar asked Feb 01 '10 13:02

Alix Axel


People also ask

What is the advantage of using em units for font related properties rather than pixels?

The main reason for using em or percentages is to allow the user to change the text size without breaking the design. If you design with fonts specified in px, they do not change size (in IE 6 and others) if the user chooses text size - larger. This is very bad for users with visual handicaps.

What is the advantage of using EMS as a measurement in media queries?

Ems are awesome for making flexible grids and measurements. For example, if a container width is specified in ems, I can proportionally resize the container and its contents in my media queries with one declaration. In this example, resizing the font also resizes its container proportionally.

What is the difference between using EMS and pixels as measurement values in CSS?

What is the difference between PX, EM and Percent? Pixel is a static measurement, while percent and EM are relative measurements. The size of an EM or percent depends on its parent. If the text size of body is 16 pixels, then 150% or 1.5 EM will be 24 pixels (1.5 * 16).

Why do we use em in CSS?

The em is simply the font size. In an element with a 2in font, 1em thus means 2in. Expressing sizes, such as margins and paddings, in em means they are related to the font size, and if the user has a big font (e.g., on a big screen) or a small font (e.g., on a handheld device), the sizes will be in proportion.


2 Answers

For font sizes:

  • em and % are both relative to the font size of the parent element (i.e. 2em and 200% always give same result)
  • ems have a history in typography (although a CSS em is not the same as a typographic em)
  • Browser implementations of CSS have fewer bugs with % than with em

For everything else (e.g. the width of an element):

  • em is relative to the font size
  • % is relative to the parent element

… so they do completely different things and need to be considered on a case by case basis.

like image 65
Quentin Avatar answered Sep 20 '22 09:09

Quentin


Ems are based on a known and consistent value (the font-size) whereas percentage is based on the size of the container. It's much harder to design and understand CSS when the basis of your dimensions and sizes is changing depending on the context (which element/container you are in).

like image 32
Nate B Avatar answered Sep 18 '22 09:09

Nate B