CSS: Core Concepts
Illustration by Richard Downs

Understanding the box model


CSS: Core Concepts

with James Williamson

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Understanding the box model

In this chapter we're going to talk about styling container elements. One of the most basic concepts of CSS is taking parent elements and then styling them as page regions for their content. In order for us to do that, we'll need to understand the Box model. So we'll start there. Now the Box model is a term used to describe the rules and properties surrounding the physical properties of all HTML elements on the page. It's easier if you think about it like this. All elements on your web page are considered to be a rectangular box.
Expand all | Collapse all
  1. 4m 54s
    1. Welcome
    2. Using the exercise files
      4m 2s
  2. 1h 8m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 35s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 44m 54s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      8m 26s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 19s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 50s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 11s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 56s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

please wait ...
Watch the Online Video Course CSS: Core Concepts
8h 52m Beginner Nov 22, 2011

Viewers: in countries Watching now:

In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.

Topics include:
  • Exploring default styling
  • Writing a selector
  • Setting properties
  • Working with common units of measurement, including ems and pixels
  • Structuring HTML correctly
  • Understanding the cascade and inheritance
  • Setting a font family, font size, text color, and more
  • Understanding the box model
  • Styling container elements
  • Working with RGB vs. HSL values
  • Styling drop shadows
James Williamson

Understanding the box model

In this chapter we're going to talk about styling container elements. One of the most basic concepts of CSS is taking parent elements and then styling them as page regions for their content. In order for us to do that, we'll need to understand the Box model. So we'll start there. Now the Box model is a term used to describe the rules and properties surrounding the physical properties of all HTML elements on the page. It's easier if you think about it like this. All elements on your web page are considered to be a rectangular box.

The properties of this box; its width, height, padding, and margin define not only the size of the element, but also how it relates to the elements around it. Without these properties we wouldn't be able to control layout at all. The first thing I want to do is just introduce the different parts of the Box model visually by adding some styles to a couple of elements that we're going to have on a page. After this exercise, we'll explore each of those properties in a little bit more detail. So I've opened up the box-model.htm file and you can find that in the 05_01 folder.

In this chapter of course we're going to be working out of the Chapter_05 folder as well. Very simple page structure, if I scroll down, we can see that we simply have a div tag with a paragraph inside of that and that is followed directly by another div tag with a paragraph inside of it. The reason that we're going to use the paragraph inside of it is because I want to be able to visualize all of this Box model properties, including padding, content, margins, and sometimes in order to do that you have to stack elements on top of one another. So I am going to go up to our styles and I am just going to go ahead and create a new selector for our div tags, and inside of that I am going to just apply a background color initially.

So I am just going to say background: #3ff. Now, I am using hexadecimal shorthand notation here. We're going to study this in the next chapter and we're going to be talking about the background property as well in more detail a little bit later on in this chapter. So if I Save this file and Preview this in one of my browsers, I can see that sort of blue color that I just set to my divs. Now, the page actually has a gray background color, so we're going to use it to help us differentiate between the elements and the page itself. All right.

Another thing you might notice, there's a space between the divs, I went ahead and zeroed out the margins for both the divs and the paragraphs. So as we add those margins back in, we'll be able to visualize those a little bit better. Okay. Now, going back into the div tag, one of the things that we can do is we can control the size of the element by also controlling its width and height. So on a new line within the div rule, I am going to go ahead and set the width property to 300 pixels, and then I am going to set the height property to 300 pixels as well. So go ahead and Save that, Preview that in your browser.

And now we see quite a bit of a change. Notice that now the div tags are 300 pixels wide, and they are 300 pixels taller. That's very hard to see because there's no space between them. This white space that you're seeing out here to the right-hand side, the body tag is being styled through a body selector and it has been given a width of 600 pixels. So each of these div tags is exactly now half of the width of our body tag. Padding is a property that often causes a good amount of confusion when it's paired with the width property. A lot of people just assume that this width that we just defined for the div tag is the total width of the div tag, it is not.

One of the things that you need to really think about when you're doing width and height is that really you could almost refer to those properties as content width and content height, because that's what they're defining. They are not defining the overall width of the element. As we're going to see in just a moment, the overall width of the element is actually determined by the borders, the padding, and the width and height. So I am going to go down to the next line and I am going to add the padding property. So we're just going to say padding, and I want to go ahead and do 10 pixels worth of padding. Adding the padding, as we did this way using the syntax, it goes ahead and adds padding equally to each side.

So top and the bottom have 10 pixels of padding and the right and the left have 10 pixels of padding. Now, if I Save this, go back into Safari and Refresh my page, I can see that the width and the height of these has increased a little bit. It's really hard to visualize this again, because I don't have really anything that's showing me exactly where the padding is versus the content width. So I am going to go ahead and do that now by taking advantage of the paragraph inside of the div tag. So I am going to go back to my CSS, just below the div tag, I am going to create a selector for my paragraph, and the only thing I am going to do here initially is just give it a background color as well.

So I am going to say background and this will be #c90. So if I save this and go back out to my browser and Refresh, you can see that now we actually get to see the padding in our div tag. Now, the paragraph doesn't extend all the way down to the 300 pixels worth of height. So just notice the tops, the left, and the right-hand side, you can see that sort of 10 pixels worth of padding all the way around that. The width of the paragraph is stretching all the way to the edge of the content width of its parent element.

So that padding is actually holding the contents of the div tag away from the edge of the div tag, that's what padding does for us. Now, what happens if you don't define say a width and a height? I am going to go ahead and remove both of those, and one of the things that I want to talk about is the behavior of what we call block-level elements; a div tag, a paragraph, headings, these are all block-level elements. Essentially, block-level elements sort of occupy their own space within the normal document flow, and when left to their own devices, they will stretch to fit the width of their parent element.

Let's go ahead and observe that behavior. So I'm going to go ahead and Save this, go back out to my browser and Refresh it, and you can see exactly what's happening now. The div tag is extending all the way out to fill the body tag, which is at 600 pixels wide, and then the paragraph is extending out as far as it can. Now, it can only go as far as the edge of the padding of the div tags, because again, padding sort of keeps the content away from the edge of the element. So the padding inside the div tag is keeping the paragraph content away from its edge.

So the blue that you're seeing is the padding of the div tags and that sort of ocher color that you're seeing there is the actual content of the div tag itself, which in this case is the paragraph. Let's visualize this a little bit more and see how height and width are affected through the use of borders, which is another property of the Box model. If I go back into my styles, I am going to first go into the div tag selector and I am just going to go ahead and give it a border, so we're just going to do border: 1px solid #300.

So make sure you have white space between these properties, we're using border shorthand notation here. So we're just creating a 1-pixel solid border that has this sort of maroon color applied to it. And then in the paragraph we're going to go ahead and do the same thing. Here I am just going to do border: 1px solid #000, which will be black. So if I Save this, and once again, I am going to go ahead and Preview this. You may or may not have caught that change, but essentially what has happened now is the border also needs to fit within the 600 pixels, and we've added 1 pixel border to the left-hand side, we've added a 1-pixel border to the right-hand side, and then we've added 1 pixel border inside the paragraph, and 1 pixel border to the right side of the paragraph.

So what that means is that essentially on either side we've had to reduce the content by a certain number of pixels. So essentially what happens is borders, padding, and the content width of an element all sort of add up to make its total width. If you change one of those and you don't have the other ones defined, the other ones will basically shrink or expand based upon the available space that you're giving it. I am just going to go back into my code. If I take this to an extreme and take this sort of outer div tag border and cranked it up to 10 pixels, watch what happens to the inside content here.

Notice that the height gets much taller and the width of the paragraph gets a lot smaller, because we still need that 10 pixels of padding. We now have 10 pixels of borders on either side and we have much less room for the content now. So anytime that you modify one of those properties, whether it be a border or whether it be padding, if you don't have an explicit width set for the element, you're going to lose some of that width, or if you reduce those amounts, you're going to gain some width, so you'll always have to keep that in mind. I am going to go back in and I am just going to reset that border to 1, because that looks a little ungainly.

So far we've talked about content, we've talked about the width and height in terms of relating to the content, we've talked about padding and we've talked about borders. What we haven't talked about yet are margins. So let's go ahead and see what margins can do for us as well. I am going to go right over here to the div element sector and I am just going to type in margin, much the same way we did padding, and with margin I am just going to type in 10 pixels as well. So this should apply 10 pixels worth of margin all the way around the outside edges of our div tag; so top, bottom, left, and right.

Let's see what this does for us. So if I Save my file and go into Safari and test this again, I can see that what's happening now is we're getting spacing between these div tags, they're not butting right up against each other anymore, which is nice. We're also getting 10 pixels worth of spacing on the outer edges of it as well. Now, this is not increasing the size of the body element. The body element is still at 600 pixels, it's not going to flex or move when we give it an explicit value like that. So what happens is this content has to flex a little bit.

So again, what happens, we lose content width right in through here when it's not predefined. It allows that content width to sort of flex based on these other values. One of the things that you might have noticed if you're a real keen observer here, you might have noticed that we gave 10 pixels worth of margin all the way around these. We're seeing it on the edges, but we're not seeing it on the bottom, we're not seeing it on the top, and we're really not seeing it between it, because if we were seeing 10 pixels worth of margins for both of those, they would be 20 pixels apart, right? Well, what you're seeing on the top between them and on the bottom here is what we call vertical margin collapse.

Later on in this title when we have the movie on margins, we're going to talk more about vertical margin collapse and why it happens. I want to take a moment to reinforce the concept of how you calculate the total width of an element and how you know how much space is left for the width of the content. So I am just going to go down to a new line here and we're going to do a little bit of math. This is not CSS syntax, this is just to help me visualize this, because I am not very good at math. All right. So we have 600 pixels worth of space available to us and we're getting that from the body selector up here.

So if we go down and really calculate what we have here, we know that we have 10 pixels worth of padding on either side of our div tag. So that's 10 pixels plus 10 pixels. Now, I know, because I am at least that good at math, that that's 20. So we have 20 pixels worth of space being taken up by the padding. Then we have 10 pixels of margin on either side, so we know that that is 20 as well. So we have 20 pixels based on the padding, we have 20 pixels based on the margins. Then, and this often gets overlooked, we do have 1 pixel worth of border on the left and right sides.

So that's going to give us an additional 2 pixels to our total element width, and you can't forget the border of our paragraph, can we? It's 1 pixel as well. This is kind of tricky, because the border is applied to the paragraph and the paragraph is expanding to fill the content width of the div tag. So it doesn't really add up to the content width of the div tag, what it does is it limits the content width of the paragraph. So we're going to leave that off for just the moment. So we have 20 pixels worth of padding, 20 pixels worth of margin to content width and 2 pixels worth of border.

So that's going to equal, obviously, 42 pixels worth of defined space that we currently have on this div tag. So what does that leave us in terms of the actual content width that we have to deal with? So if you add that up, that's going to give us a total of 558 pixels to deal with. So we have now 558 pixels worth of content width for this div tag and that's the available space for our paragraph to fit into. Now, I mentioned this is not CSS syntax, this is just helping me visualize this, so I want to go ahead and get rid of this, but I do want to put that theory to test.

So here's what I am going to do, I am going to go into the div selector and I am just going to say, okay, fine, what if I made the width something bigger than that? What if I said 565 pixels, what if I did that? So if I Save this, go back to Safari and Preview it, you can see what happens. We don't break anything per se, but our element width no longer fits, our content width no longer fits into the space that we're giving it, it no longer fits in that available space. What browsers are instructed to do when this happens is that if there's still room inside this parent element, in this case the body selector, it's instructed to ignore the margins.

So margins, while they do calculate in terms of where an element fits into the layout, one thing that you have to remember about margins is that they will be sacrificed in order to help an element stay within its parent container. So in this case, the right margin is being just sort of thrown away, and that's one of the reasons why I tell people margins are important for planning document layout, but you should not use them to calculate the total width of an element, because they can be discarded. You certainly need to take them into consideration and know how far your elements are going to be apart, but if you're looking for the total container box of your element, margins really don't come into play for that.

So what happens if we increase the width to a level that you simply don't have anymore margins to sacrifice? Well, let's find out what happens there. So I am going to go back into my code and I am going to show you a very common mistake that a lot of new designers make. They're going to go in and declare like a width of 600 pixels, because they say, okay, I've got 600 pixels worth of space to deal with, so I need my div tag to be 600 pixels wide as well. They're ignoring the fact that they have the padding, the border, and the margin. So what happens? So if I Save this, go back into my browser and test it, what happens is we end up with overflow.

So right now, these div tags are overflowing their parent container, in this case, the body selector. Now, browsers are instructed in terms of how they should deal with overflow and the default overflow instruction is to make that content visible. They error on the side of showing it rather than not showing it. But there are some things that you can do. For example, if I come up to the HTML tag right here, I can come up and I can say overflow: hidden Overflow is the property that we use to tell browsers how we want to manage the overflow of our content.

So if I save this and test this in the browser, nothing really happens. Well, remember, this is on the body element, this is on the body tag, not so much the HTML. So if I come back into the body element and come in and say overflow: hidden, Save that and test it. Boom! Now that content is actually being clipped off. We can't see to the right of that content anymore, because it's gone and there's actually no way to scroll to it, no way to get to it, it really does hide it, it clips it right off.

Now, there are other things that we can do. For example, we could come in and just say scroll, and if we say scroll, we're going to be given scrollbars that allow us to scroll around this element. Now, different browsers render this a little bit differently. You'll notice that in Safari, it's giving me a bottom scrollbar, but it's also giving me the Chrome for the vertical scrollbar as well, which in this case we don't need. So maybe a better choice instead of scroll would be auto. If I Save that and test it, I can see that auto only gives me the scrollbars that I need, in this case, I am getting scrollbars on the bottom hand side, and this allows me to kind of go back and forth here on this element.

Now, that's actually a really useful thing to know, because it's quite a common technique actually to create a parent container that's not wide enough to contain the children of it and then add scrollbars so that people can scroll through it. It's very reminiscent of what people used to do with frames and you can create some neat little widgets that way as well. I know that was a lot, but really those are just the basic elements of the Box model. Once again, width, height, padding, borders and margins. Now, we're going to explore every single one of those properties and how they work with each other throughout this chapter.

We're also going to explore other properties, like the background property, which isn't technically a Box model property, but it's still very important when styling container elements.

There are currently no FAQs about CSS: Core Concepts.

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

* Estimated file size

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


You have completed CSS: Core Concepts.

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

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 ?

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.