Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

When to use em versus px?

So I've been curious watching Guil Hernandez and Nick Pettit, when one should use em versus px versus vw etc.

Obviously for manipulating font sizes its generally suggested to use ems for example:

   font-size: 2.5em;

but what about if you are say manipulating padding or margin on an element like a header or footer? px would be static but em just seems odd because its font size based

header, footer {
   margin-top: 50px;
   padding: 20px auto;
} 

Do you try to size everything off of font-size?

2 Answers

Hey John, take a look at this article.

Thanks, I just wanted to be sure I'm not violating rules by using font size units for non font things. Should I still set min/max in PX? I ask because they say a touch area for a mobile device should be no smaller than 40px by 40px so if I set mine to 40px and h/w by em it should always be at least touch friendly.

Maybe Guil Hernandez can provide even further clarification.

Generally, I use em's because it's a relative measurement and it ensures that all users who may view my site all see it similarly to me. However, if you really want a header, for example, to have a definite size then I'd suppose you'd want to use the px measurement.

(Hope this helps)