Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Calculating percentage values

From: CSS: Page Layouts

Video: Calculating percentage values

Even though percentage values are always defined as being relative to another value, they're still defined separately in the CSS specification, meaning they are not grouped with other relative units of measurement; they are just sort of defined by themselves. Now one of the reasons for this is that percentages have their own very unique rules when it comes to how their values are calculated. To use percentage values in your layout with confidence, you're going to need to understand exactly how their values are computed. So if you are going to do this along with me, go ahead and open up percentages.htm, which can be found in 01_05 directory.

Calculating percentage values

Even though percentage values are always defined as being relative to another value, they're still defined separately in the CSS specification, meaning they are not grouped with other relative units of measurement; they are just sort of defined by themselves. Now one of the reasons for this is that percentages have their own very unique rules when it comes to how their values are calculated. To use percentage values in your layout with confidence, you're going to need to understand exactly how their values are computed. So if you are going to do this along with me, go ahead and open up percentages.htm, which can be found in 01_05 directory.

This is exactly the same file that we used in our last movie, so if did the calculating em values with this--let me just go ahead and just preview this--you can see it's pretty the same file. Same structure, same everything. Now I am going to jump back into my code here, and let's talk about this for a moment. If I scroll down into my body, you can see that currently right now the width of this page is set to 960 pixels. Well, let's just go ahead and set that to 80%, so I am going to take it to 80%, go ahead and save that, and if I go back into my browser and refresh that, you can see that the width of the page changes a little bit there.

You know the other thing that it does for us is now the width is relative to the viewport. You can see that resizing the page is causing those elements to flex because the actual size of the page is flexing, and if there's not enough room for these elements, you can see it's dropping the elements down, based upon how wide the page is. Now, let's talk about percentages and how they are calculated. If you use percentages on box model properties, like width, padding, and margins, then those values, they are calculated relative to the width of the parent element.

Think about our body for just a second. The body is relative to the viewport, so that 80%, we're getting 80% of the available viewport. That means that any time you set percentage on one of those properties, it's going to be that percent of the element's parent. Now that's pretty easy when it comes to width, because you are saying, hey, I want to be, you know half the size of the element I am sitting inside of. But it does get a little bit harder to remember that when you're doing padding and margin, so let's go ahead and kind of experiment with that a little bit. Okay, I am going to take a look at these two selectors right here: em and percentages.

Remember, those are those two sections that we were looking at earlier. And I am going to change the em's width to, say, 60%. And then I'm going to take the percentages and I am going to change its width to, say, 30%. Now, 60%, 30% of course is 90%, right? But that's not 90% of the viewport. If I scroll back down, for example, I can see that this section with the class attribute of em and this section with the class attribute of percentages, it's actually sitting inside of this particular article tag.

So essentially, that 60% and 30% are relative to the width of this article. Not even the body. Now the article as a block-level element is getting its width from body, because we haven't explicitly gave article a width yet, so it's basically sort of expanding to fill the whole body tag. So essentially, that 60% and the 30%, they're really asking for a 60% of the body or 30% of the body, because we don't have an explicit width set for article. So if I save this, go back in my browser and refresh that, you can see that those values change rather dramatically. And now, if I resize the page, you can see that they are basically changing their values, but they are still staying relative to the fact that this is 60% of the available space within the body and this is 30%.

The body is 80% of course. So it's actually 60% of that 80%. Now if you go too far down that rabbit hole you can drive yourself crazy; just remember, it's 60% of its parent element. So now, I mentioned earlier that it gets little trickier when you start talking about margins and padding and things like that. Let's take a closer look at that. Go into both of these selectors here for the em and the percentages, and I am going to go ahead and give them a padding of 1%. Same thing for percentages; I am going to give it a padding of 1%.

Now one of the most difficult things to do when you're working with a layout like this, and we are going to talk more about flexible layouts defined with percentages is little bit later on-- we have a whole chapter on those as a matter of fact-- but one of the most difficult things about calculating these values is exactly how much do I need to get the visual amount of padding that I want for this? And one of the things that you might want to consider doing is thinking about starting with sort of preferred size if you will. Even though we've stated this to be 80%, you know if think that the preferred size of it is at 950 pixels, for example, I can say okay, at 960 pixels, I want 10 pixels' worth of padding, all right.

So then you look at 10 pixels on one side of an element and 10 pixels on other side and you can add that up to 20. You can say okay, 960, if I take a look at 960, what percentage of that is 20? That calculates out to about you know 1%. So if I save this and test this, you can see now we are getting you know, at 960 pixels we're getting roughly 10 pixels' worth of padding, which is kind of cool. And if I resize that, notice that that value gets smaller as I get smaller and as the page gets bigger, that value also gets a little bit larger based upon the available size for it.

Now if your calculations that you do happen to turn up decimal points every now and then, don't be worried about using those. Browsers can handle decimal points really, really well. So if I come and say 1.4% instead of 1% and test that, you can see, it increases just fine. Nothing breaks. The computer isn't smoking. There is no fire coming out of it. It can certainly handle that type of math. That's not an issue. I am going to go in and talk a little bit about what happens when you start dealing with some of these interior elements.

So if I scroll down, I am going to go down to this h2 selector here and I am going to change it's margin-bottom value. I am going to set that margin-bottom value to 5%. Now the reason I'm doing this is because I want to illustrate the fact that anytime you use percentages on a box model property, in terms of calculating the value of that, it calculates relative to its parent. So this h2, remember is inside of this section, which is em, and it's inside this section, which is percentages.

Remember, em is set to 60%, so it's available width is a bit larger than percentages, which is set to 30%, so it's available width is smaller. So if this h2 is giving me, say, 5%, it's going to give me 5% based on that 60% size, whereas the percentages is going to give me that 5% based on its 30% size. So if I save this and preview it, indeed you can see that I'm getting more bottom margin here on this h2 than I am on this h2.

It has everything to do with the relative size of those elements. And if I shrink that, notice that the margin is shrinking. If I expand it, notice that the margin begins to expand as well. Another thing that I want to point out to you guys here. We don't really need to illustrate this. I just want to talk about it. Percentages should not be used for border width. This is actually per the W3C CSS specification. They are not allowed. If you go ahead and use them anyway, well, what the browser will do is it's just going to display whatever the default border width is, regardless of which value you used.

So you could say 400% on a browser border width value and you still just get the default border. We are going to have a chance to put what we've learned here about percentage values to the test a little bit later on when we create percentage-based flexible layouts. Now that you know how browsers calculate percentage values, you'll be better prepared to plan out element sizing when working with those layouts.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40829 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Page Layouts.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.