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

Calculating element dimensions

From: CSS: Page Layouts

Video: Calculating element dimensions

One of the most important skills in CSS regarding page layout has nothing to do with positioning elements on the page. It is in fact the ability to accurately calculate an element's dimensions. Now on the surface that sounds like something that's pretty easy to do, but as we are going to see, it gets a little bit more complicated than maybe you'd think at first glance. So to illustrate this concept, we are going to be working with the dimensions.htm file, and you can find this one in the 01_02 directory, in the Chapter_01 folder. Okay, so if I take a real quick look at the structure of the page, you are going to see that it's very similar to the exercise file we just worked with.

Calculating element dimensions

One of the most important skills in CSS regarding page layout has nothing to do with positioning elements on the page. It is in fact the ability to accurately calculate an element's dimensions. Now on the surface that sounds like something that's pretty easy to do, but as we are going to see, it gets a little bit more complicated than maybe you'd think at first glance. So to illustrate this concept, we are going to be working with the dimensions.htm file, and you can find this one in the 01_02 directory, in the Chapter_01 folder. Okay, so if I take a real quick look at the structure of the page, you are going to see that it's very similar to the exercise file we just worked with.

We have two divs, an outer div and then an inner div with the class of box applied to it, and that has a paragraph inside of that. Our sort of default styles that we have going on: the outer div tag is now set to a width of 600px wide, it has a margin of zero for top and bottom, and then left and right is set to auto. Now what that does for us is when we set left and right to auto, it'll go ahead and just take that content and center it within whatever parent element it's floating inside of, which in this case is the body tag. And then we have a background color applied for our outer that's a light-blue color.

For inner box, the only thing that we have right now is just a background color. And for our paragraph, we have not only a background color that's going to allow us to sort of visually define the contents of the box, but we also have the height set to 100%, and that way no matter what happens with the inner box, the paragraph will sort of expand to fill the height of it as well. So the first thing we are going to do is we are going to go into our box here, and we are going to go ahead and define its width as being 300 pixels wide. Easy enough. I am going to save that, and let's just preview that in one of our browsers. Okay, so what we're seeing here is we are seeing the overall width of 600 pixels--that's our outer div tag--and then our inner div, or the box div tag, is 300px wide.

So we're getting exactly what we expect here. We are getting exactly half of that outer div. Okay, so I am going to go back into my box and let's add some more box model properties. On the next line I am going to go ahead and do padding of 50px. Now remember, padding and width are cumulative, so this is going to add 50 pixels worth of padding all the way around our box, so it's going to increase both our height and our width, but we are just going to focus on width. So what's happening here is we have 50 pixels' worth of the padding on the left-hand side, 50 pixels' worth of padding on the right-hand side, and then a width of 300px. That gives us a total width of our element now of 400 pixels.

So I am going to save this, refresh that in my browser, and that is indeed exactly what we are seeing. We are seeing the background color now of the box div tag. This is the background color of the paragraph, and then we can visualize that 50 pixels' worth of spacing here and 50 pixels' worth of spacing here, 300 pixels' worth of spacing here, and that it's taking up a total of 400 pixels' inside of that outer div tag. Awesome! Now one of the elements that people really often forget about when they are calculating the width of their elements or factoring how much space something can take up within the layout are borders, but borders are part of the box model and they do calculate to an element's size.

So if I go down on the next line and I type in border, and let's say we do 2 pixels of border solid, and then black, all right, so I am going to save that, if I preview that in my page, that does increase the overall size of my element. Now borders show up on the outside edge of the padding edge, so essentially 2 pixels have been added to the outer edge of my element. But because it's inside of this outer div tag, it is inside that. It doesn't hang over or overlap. It stays inside the content edge of its parent.

So essentially, now we have added 2 pixels to the left and 2 pixels to the right, assuming now we have a total width if 404 pixels. Okay, so how do margins play into that equation? It's confusing. Sometimes you will hear people say margins don't calculate to the overall width of an element; sometimes you will hear people say they do, and it becomes a really confusing thing. So let's take a look at what margins add to our element and sort of how they play into the overall size of our element. So if I go back into my code, if I go down onto another line within the box selector and this time I am just going to apply a margin to one side.

I am going to do margin-left. And we'll go ahead and do 50 pixels there, okay. So I am going to save this and again I am going to refresh this in my browser. Okay, so here is essentially what's happening. The actual width of my element really hasn't changed, but the amount of space that it takes up has changed. So we are pushing our element away, 50 pixels from the edge of its containing element, so the amount of space it needs within the layout to do this are now 454 pixels, so 404 pixels for the total width of the element and then the 50 pixels over here on the left-hand side.

So let's clarify the relationship between margins and their elements, in terms of size. The best way to think about it are there are two sorts of blocks of calculations that are used for an element. You have the containing block and the content block. Now the content block are all of the border, padding, and width of an element. So the width property, any padding, and border, they all go together to make up the actual content block of an element. The containing block are the margin edges. So in this case the containing block is 454 pixels or the content block is 404 pixels. May not seem like a big deal.

We certainly need to understand how much space something is taking up within the layout, so understanding the margins is extremely important. However, certain things happen when an element is considered overconstrained, so what does overconstrained mean? Well, overconstrained means an element no longer fits inside of its parent. So what happens when we start increasing the values of the size of our element and increase it to a point to where it just doesn't fit inside this 600 pixels anymore? Well, when something is overconstrained, your browser has to make a decision, or the user agent has to make a decision, about what to do, and often it will sacrifice margins and padding in order to make things fit.

There are other properties that we can use too to control this sort of overflow, if you will, and we'll talk a little bit about that in just a moment. But let's go ahead and make this overconstrained and see what happens with our margins. All right! So I am going to go back into my file, and I am going to ahead and change the width of our box now to 400 pixels. So that gives us a total of 504 pixels of width. Now remember we are inside of a div tag whose width is set is 600. So for the moment we still fit; however, what I am going to do is I am going to take margin-left, I am going to get rid of left, and I am just going to apply that as margin. So if you think about this, the content block is going to fit, because the content block is 504 pixels.

The containing block will not fit. The containing block, with 50 pixels' worth of space on either side, is now 604 pixels, whereas our outer div tag is now 600 pixels. All right! Everybody still with me? Cool! So save this. And I am going to update this in my browser. Now at first glance, it looks like everything is okay. It fits, right? Not entirely. What happens is because we have 50 pixels' worth of margin on either side, it's sort of centering, or floating, if you will, in the middle of our div tag, and it gives us the illusion that it fits. But the problem is--well, it's not really a problem, but what our browser has done is it essentially has ignored certain margin values.

It simply just throws out the margin values that would overlap on either side. Now, if you don't believe me, let's take this to a little bit more of an extreme. So I am going to go back into box, and let's say, I go in and say, okay, on the margin-right, go ahead and add 100 pixels' worth of space of there on the right-hand side. So if I save this and test it. Nothing happens. And 100 pixels, it's just thrown away, because the browser is saying, "No, I can't push it that far. It won't fit anymore. So I am just going to sort of ignore that, okay." Now what about the content block, what happens when the content block no longer fits? All right! So what I am going to do now is I am going to go in and remove all those margins, and then I am going to take my padding and I am going to change my padding to 98 pixels. Why 90 pixels? Well, remember we have borders of 2 pixels on either side, so that it's essentially giving us a combination of padding and border of 100 on either side. So we have 100 pixels' worth of padding and border on the left-hand side, 100 pixels' worth of padding and border on the right-hand side, 400 pixels' worth of width, that's going to give us an equal value to our outer div tag of 600 pixels.

So if I save this, and preview it, it fits precisely. You can't see the outer div tag anymore, but I promise you, it fits precisely. Okay, so what does happen if I make it a little bit wider? So if I increase the width to say 500 pixels and save that, what happens? Well, it's hard to see what's happening right now. So let me do something really quick so you can visualize what's happening here. I am going to go ahead and take my height here and move my height to say 800 pixels so you can see this. All right! So let's save that and let's preview it. All right! So you can see what's happening.

The inner div tag is simply overlapping, it's sort of spilling out of the outer div tag. So what happens when a content block no longer fits is we have something that we call overflow. Now you can control overflow by using the overflow property. If I go back into my outer div tag, for example, and I am just going to type in overflow. And there are a couple of values you can use here. You can use hidden, and if I use hidden, it simply crops off any content block that would overflow the element. I can do visible.

I can do scroll. I can do auto. Now auto and scroll are very similar. Auto basically just says, if it does overflow, go ahead and give me scrollbars, and you can see that at the bottom in the element here I have scrollbars that let me scroll through this. Harder to see, because of the height of the element, but I think you get the idea. All right! So that's the overflow property. Now I am going to get rid of the overflow property here. And I want to show you one more thing, in terms of what happens when we have an overconstrained element, so we've got one more thing to take a look at here. All right! I am going to go back to my box and I am going to change the width of the box back to 400 pixels.

Now I am going to go up to my outer element here and I am going to get rid of this height property so that we are not confused by that, and what I am going to do is I am going to set my padding on the outer div tag to 50 pixels. So that's just affecting the overall width of that element as well. So now that the outer div tag is now 700 pixels wide, obviously. All right! Now with my box width being 400 pixels, of course I have 100 pixels on either side, so that's 600px, right? So if I save this and preview it, we can see there is the additional 50 pixels' worth of padding for our outer div tag.

Now keep an eye on that padding. If I come back in the box and I increase the total value of box here, let's say I increase its width to 450px and save that and preview it, notice what happens. It expands to fill that empty space. The padding on this side is basically sacrificed. So essentially, it fills this in and it says, "Okay, I have 50 pixels' worth of padding here and I have this huge total width over here," and this padding is basically sacrificed. It's actually still there and other elements would respond to it, but because this is now overflowing, it overflows that padding value.

Now the reason I'm mentioning this is because it's really important for you to understand what happens if you don't really plan your elements properly. If you are not carefully considering the dimensions of your outer elements and the dimensions of your inner elements, then your layout is either going to break or behave unexpectedly if elements become overconstrained. So obviously the more complex the page gets, the harder it can be to accurately calculate an element's width. This can cause layouts to break, spacing to be uneven, and content to overflow element boundaries. That's why it's so important not to lose sight of how even small changes to your elements, like increasing your border by 1 pixel, can make a big difference in your entire layout.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 42729 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 preferences from 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.

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.