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 Modular CSS with Sass Sass Grid System Column Layout

Alice Stiles
Alice Stiles
3,898 Points

I don't understand the purpose of the px to em function

Surely if you want something to be a fixed size, you can use px, and if you want it to depend on font size you can use em? Why have a function to take a px value and convert to em? Am I missing something?

1 Answer

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

Hiya Alice Stiles! :wave:

You're not missing something, it's a great question!

One core reason is accessibility. Users can adjust their browser's default font size for readability. Elements sized with em units will scale accordingly, respecting the user's preference. In contrast, px units are fixed and won't adjust, potentially leading to readability issues. Also, using em units allows your layout to adapt when the context changes. For example, if a parent element's font size changes due to a media query, all child elements using em units will scale proportionally, maintaining consistent visual proportions across different screen sizes.

Lastly, designers often provide specifications in pixels. Converting these to em units bridges the gap between design precision and responsive implementation. The function automates this conversion, reducing manual calculation errors.