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 Build a Responsive Website Creating a Fluid Foundation Converting Pixels to Percentages

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

a

1 Answer

Wayne Priestley
Wayne Priestley
19,579 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.