Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS Build a Responsive Website Creating a Fluid Foundation Converting Pixels to Percentages

Can somebody explain how exactly target ÷ context = result works by live examples?


1 Answer

Wayne Priestley
Wayne Priestley
19,567 Points

Hi Akhil,

The two most common instances you would use them are,

To convert pixels to em or rem.
Lets say you needed to convert a font size of 55px to em's.
Target= 55px
Context= 16px (the default base font size for most browsers)
55 ÷ 16 = 3.4375
Answer = 3.4375em

The second example would be getting a percentage of something.
Lets say you had a image that was 400px wide, your site has a width of 960px and you wanted to give your image a max width in %.

Target = 400px (image width) Context = 960px (website width)
400 ÷ 960 = 0.41666667
When working out the % we have to move the decimal point two places to the right to get our answer.
Answer = Image width is 41.666667%

Hope this helps.