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 trialMandy Rogers
2,009 Pointsem vs pixel
Could you go into more details about em's? Why would you use em over pixel?
8 Answers
John Coffin
10,359 PointsIt depends (sorry).
The basic difference -- what one considers when picking -- is that pixels are absolute in size and end are relative. Some cases call for pixels where we don't want our material resized. Other cases, we want things to resize so that the structure stays consistent as one resizes the browser.
Mandy Rogers
2,009 PointsJohn, thank you for your response. So it's best to use ems when coding responsively? I have also noticed percentages being used, are em's a preferred method over percentages? Thanks again!
John Coffin
10,359 PointsGood question. I'm not quite at "responsive design" so hopefully an expert will weigh in. In the meantime, I'll try to clarify with an example and ask that you take my comments regarding responsive design with a grain of salt:
A block of text such as a paragraph can be thought of as a box -- the content -- with an offset and width. One would want the text and text affects (i.e. indentation, hanging punctuation) to work consistently relative to itself. This can be achieved easily with em. We then want a responsive design, so we use percentage for the width. However, we want precision for the offset to the content, so we use pixels.
The trick is knowing want will work best and when. Try looking at some websites and try to determine what they are doing, when they are doing it, why they are doing it, and if it is working.
Mandy Rogers
2,009 PointsIt's making a bit more sense John, but what do you mean by offset?
John Coffin
10,359 PointsAnother consideration is that by using pixels and not ems, one is preventing a user from modifying the content via font size (i.e. control of design vs. accessibility).
John Coffin
10,359 PointsOffset is the distance to the block from the edge or from the top left. The design may ask that this is a fixed width for aesthetic or practical reasons.
I wish I had a better example. If I come up with a better one, I will be certain to post it here.
Mandy Rogers
2,009 PointsOh, that makes sense to me now! So basically the margins around the main content of the page. Thanks again for your input John!
John Coffin
10,359 PointsI hate to go back on what I'd said, but I rather correct my mistakes than leave them:
I've been watching the new videos with Nick Pettit regarding Responsive Web Design and Testing, and I realized not only that what I said was not very useful for responsive design but also wrong. I also saw that there was a really, really good example of when one would want to use pixels.
The use of ems that I described -- fonts and things that use font sizes such as line spacing being relatively consistent -- makes sense. It can stay.
The width using percentages can stay too.
Margins using pixels for a responsive design doesn't really make sense. Most pages that I look at have the margins adjusting as the browser/screen size adjusts so that the content is always centered. This therefore should use a percentage.
What would use pixels is the minimum and maximum widths and the breakpoint values. Here, we need a measurement that corresponds to the device type: A width of 480px corresponds to the threshold between a regular smartphone and a tablet; a width of 85% corresponds to ... welll ... it doesn't correspond to anything; a width of 85em corresponds to ... uhm ... well ... it really depends on the font, and who wants to do a lot of math to calculate pixels when they can just use pixels.
I hope that makes things clearer. Sorry about that mistake.