New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

For the most part, you'll find that planning a flexible layout is almost exactly the same as planning a fixed layout. Now, the biggest difference, and honestly one that adds just another entire layer of difficulty, is calculating the percentage values that you'll need for your elements. Now, rather than setting a fixed-pixel values for widths, margins, and padding you'll need to determine the proper exchange values for the desired layout. Because of the way percentage values work, this can often be a little tricky, so I wanted to take some time to go over it here.

Calculating percentage values

For the most part, you'll find that planning a flexible layout is almost exactly the same as planning a fixed layout. Now, the biggest difference, and honestly one that adds just another entire layer of difficulty, is calculating the percentage values that you'll need for your elements. Now, rather than setting a fixed-pixel values for widths, margins, and padding you'll need to determine the proper exchange values for the desired layout. Because of the way percentage values work, this can often be a little tricky, so I wanted to take some time to go over it here.

Now, again I have a document open. I've got fluid_planning.ai, which is an Illustrator file opened up in Illustrator. If you want to do this a little along with me, feel free to grab another piece of sketch paper, or you can open this up in Illustrator if you have Illustrator. It doesn't really matter. What's the most important thing here is that we're focusing on how to properly calculate percentage values for elements within your layout. When I use the term fluid and flexible, it means that the web site is responding to the size of the browser change. So, really they're the same thing. I don't mean to cause any confusion, but I find myself sort of using those terms interchangeably, and I felt like I needed to go ahead and define them for you.

Just like our fixed layout, we've got a two-column layout going on here. This time I based the grid off of 10 pixels, just again, makes a little bit easier for us to do the math. And I want to go over the steps that I have over here on the left-hand side of the page, steps 1, 2, 3, and 4, and I want you to compare them to what we were doing in the previous chapter with fixed layouts, in terms of planning. First, just like with fixed, go ahead and define a target resolution and a grid. Now, that sounds kind of odd because you're like, wait a minute, if I'm doing a fluid layout why am I establishing a target resolution? Well, essentially what you're doing when you establish a target resolution with a fluid layout is you're saying okay, this would be the "ideal size" for this, and what that's going to allow you to do is it's just going to help you pick out values for padding and margins and things like that.

If you say okay, I'm targeting an ideal size for this at 960 pixels, for example, you can say I want my padding to be 20 pixels or 40 pixels, and then it's going to help you calculate what percentage of your overall width 20 or 40 pixels is going to be. To make calculating percentages easier, you mock up that layout at an ideal size, just as we mentioned here with the target resolution. So we're going to go ahead and mock that up just like we have here with two columns and we've got some spacing in here. Then we use our ideal measurements to calculate percentage values. Now, what does that mean? Well, I'm going to turn on the measurements and show you.

In this sense, we were targeting 960 pixels' worth of resolution. We were planning this as if we were doing a fixed layout. I've got 590 pixels total width for the left-hand column, 350 pixels of total width for the right-hand column, and then 20 pixels' worth of space between them. That means that we have got 20 pixels' worth of padding over here on this side, 20 pixels' worth of padding on this side and then 330 pixels of width, 570 pixels of content width for those columns. So, we're sort of creating those numbers first as a starting point for our fluid layout.

The most important thing that we're going to cover in this entire lesson is point number 4. Remember that margins and padding calculate their values as a percentage of the parent element. We'll talk about what that means as we go through the planning stages. Okay, so the first thing that I do is when I have these sort of top global values, I come up with just whatever the percentages of those are going to be. I'm going to bring up the calculator. This is pretty easy to do. I'm just going to say okay, 590 pixels divided by 960 pixels gives me .614, so forth and so on, so around 62% if you will.

Now, browsers can handle decimal points just fine and if you look in certain people's layouts when they do fluid layouts, you'll find percentage values that go out to the 100ths or 1000ths or 10,000ths of a point. It's up to you to decide kind of how precise you want to get with this. In this case just to keep things simple, we're going to round our values up. Same thing for the 20 pixels. So if I take 20 pixels divide that by 960, I get around 2%. If I take 350 pixels and divide that by 960, I get around 36%. And again, I can round these values if I just want to make things a little bit simple for us, because remember, we're not expecting this to be a specific size.

We're more interested in what the proper percentages is to get this ratio. All right! So, I'm going to go back into Illustrator. I'm just going to turn on those top- level percentages, and you can see we're rounding here: 62% for that, 2% for the space between them, and then 36% for the overall right-hand column width. Now, those values are helpful to know, but they're not as important as the values underneath them. Now, values underneath them, remember, are the padding for left-hand column; the content width for the left-hand column, which is 570 pixels; the spacing or the margin between those two columns, which is 20 pixels; and then the content width and the padding of the right-hand column.

When I calculate these percentages--and I'll go ahead and turn those on--here is where people typically get confused when doing fluid layouts. Inside this particular element we have a width of 570 pixels and we have padding of 20 pixels. Now, when you're calculating this it's really, really tempting to say okay, what percentage of 590 is 20 pixels, and that's what I need for my padding, but that is wrong. Padding and margins are calculated based upon their parent elements. So, this element, this orange column right here, that element's parent element is the body tag, which is based off of the 960 pixels.

So, in order to get the correct percentage for that padding, I have to say what percentage of 960 pixels is 20 pixels and in this case it's 2%. So, I break my percentages down. 2% for the padding, 60% for the content width, 2% for the spacing between them, 34% for the content width here, and then 2% for the padding. Now, that's pretty easy to calculate on a top-level basis, but the more complex your layout gets, the harder it gets to calculate the proper percentage values, especially for margins and padding. And let's demonstrate that by taking a look at the possibility of adding interior shapes to our layout.

So, if I just do the interior measurements of this particular element, we have 250 pixels for the content width of this and we have 10 pixels for the padding all the way around it. Now, that's going to leave us with right around 20 to 30 pixels' worth of spacing in between that. If these interior shapes have padding more than 10 pixels, remember their parent element is this orange left-hand column. So, that 10 pixels needs to be calculated as a percentage of the width of this column.

However, for padding and margins you don't use the total width; you use the content width. So, I need to know what the percentage 10 pixels would be of 570 and not 590. That is a really big distinction, because when you're calculating the content with itself, in this case 250 pixels, I have to calculate that based on the container width itself. To make this point again: when you're calculating percentages just for content width, you calculate that based on total width.

When you calculate percentages for margin and padding you calculate that percentage based on just content width, and so because of that, when I turn on these values again, you can see that for the padding of this I'm at 1.75%. Again, I'm rounding here. For content width I'm at 44.5, but for spacing between these two, I'm actually at 4%. And again, those values have just been rounded. So, I can't just pass this 2% value here or here and get what I'm expecting.

So, I can't just calculate the interior padding or margin between these based on this 2% value. I have to go back to the overall content width and container width from that column to do that. Now, we have the percentage values that we need to create our fluid layout. Now remember that above all else fluid layouts are really just about the relationship between elements. Don't get too caught up in finding the absolutely precise percentage points for a target measurement. I mean it's helpful to use this as a starting guide, but don't get too carried away with it.

What you do need to remember, however-- and this is the most important thing from this movie--that margins and padding values are calculated based on the content width of their parent elements. In most cases when the fluid layouts fail it's these values that typically turn out to be the problem, so you want to pay extra attention to them when you're planning and writing your styles.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40101 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

Are you sure you want to delete this note?

No

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.