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

HTML How to Make a Website Adding Pages to a Website Add Iconography

Do I understand correctly that some browsers don't strictly obey css-specified "px" dimensions on images?

We keep mentioning that we use images larger than the pixel dimensions we set for them, because of "high definition devices." In that case, the device must disregard px dimensions or substitute some multiple for those values.

Wouldn't it be better to specify image dimensions in "em"s or percents to maintain consistent dimensions with the rest of the page?

3 Answers

Hi Nathan,

You have to differentiate between a css pixel and a physical pixel on the display. It's not always a 1 to 1 mapping with the higher res displays.

When you specify your background image size to be 20px square, that's 20 css pixels. It's going to be 20 css pixels on both a standard and high res display. So a high res device doesn't ignore the pixels you set but rather it has to convert that into how many physical pixels that will be on that particular display.

Let's suppose the resolution is 2 dppx "dots per pixel" meaning 1 css pixel will equal 2 physical pixels in either direction.

Your 20px square image is still a 20px image on the high res display but it's going to take up 40 physical pixels in both directions.

So if the native size of your image is only 20px then the browser has to scale it up to fit the 40 pixels producing a lower quality image. Scaling down works out better than scaling up.

So for standard displays the larger image gets scaled down to 20px and for higher res displays it can be displayed at native size or perhaps scaled down a little depending on what size it was originally.

Another solution you will see if you start looking into this more is to have two sets of images. One set would be 20x20 and the other set is 40x40.

You would still set your background size to 20px but you would use media queries to target high res devices and serve them the 40px image and serve the 20px image to low res devices.

Taylor Plante
Taylor Plante
22,283 Points

em's and % values are used all the time for that reason, but pixels are particularly useful for positioning (including margins and padding). If you use % values or em's, your page will constantly change when you adjust the browser window size, which isn't always ideal for responsive designs. For example: you may want a 10px margin between two divs when the window is 600px or wider, instead of having the space between continue to grow as you broaden the window.

That's a good point that some things shouldn't be relative. em seems like a nice compromise (stretching the window wont change anything); but I presume it could still mutate when people zoom and change the size of text.

I presume "high definition" browsers don't obey px values, or there wouldn't be a real point in using images larger than the px dimensions defined in our style sheet.