Skip to content

Instantly share code, notes, and snippets.

@bryanstedman
Last active December 16, 2015 13:59
Show Gist options
  • Save bryanstedman/5445488 to your computer and use it in GitHub Desktop.
Save bryanstedman/5445488 to your computer and use it in GitHub Desktop.
A quick article on the merits of using em based media queries over px

Quick Tip: Em based media queries

Using pxs in your media queries assumes a set font-size. This means that, in some browsers, things can break pretty quickly if the user has set a larger default font-size or manually increases the font on your site. (It is worth noting that some browsers will require a refresh to render properly if the font-size is increased after the page has already loaded.)

The way to avoid this is to use ems instead of pxs. Ems are based on the font-size of their parent container and so when the font increases so does the container it lives in.

Another option similar to ems is to use rems (which stands for "root ems"). Rems, like ems, are based on font-size, but instead of using the font-size of the parent container, rems are calculated based on the root font-size - that is the font-size of the html tag.

Whether you use ems or rems, by creating a fluid system of proportions rather than rigid (px) layout, things keep working and are readable for users that need larger fonts.

###Further Reading http://blog.cloudfour.com/the-ems-have-it-proportional-media-queries-ftw/
http://css-tricks.com/zooming-squishes/

@davatron5000
Copy link

Rad. I think for a "Further Reading section" it'd be good to include a link to:

Also maybe hyphenate font-size (just like CSS or whatever) for readability.

@grayghostvisuals
Copy link

This is totally awesome! 🤘

One thing to note is that each media query condition containing ems will always be based on the default 16px of the body no matter what, but correct me if I am wrong O_o

With rems to clarify you can change the base size of the root (HTML) and use that as your base instead of the constant 16px used by the body when using ems in MQ conditions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment