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

The Box Model

From: Dreamweaver CS5 Essential Training

Video: The Box Model

Any discussion on CSS-based layouts should start with the box model. Understanding the box model is crucial to controlling page layout. Every element in your document is contained within a box, even the inline level elements. Browsers use an element's box properties to determine how much space it takes up on your page. Controlling these values gives us control over much of our layout. The box model is made up of five basic properties: an element's width, height, padding, border, and margins.

The Box Model

Any discussion on CSS-based layouts should start with the box model. Understanding the box model is crucial to controlling page layout. Every element in your document is contained within a box, even the inline level elements. Browsers use an element's box properties to determine how much space it takes up on your page. Controlling these values gives us control over much of our layout. The box model is made up of five basic properties: an element's width, height, padding, border, and margins.

Backgrounds do take advantage of the box model. They display inside an element all the way to the element's borders, but they're not technically part of the box model itself. Not understanding the box model clearly can have serious consequences to your layout. Without understand the box model, you might think that an element's total width and height are determined by the width and height property. In fact, they're determined by a combination of the width, height, padding, and borders. Starting inside an element and working on our way out, we start with an element's width and height properties. Next is padding.

The padding keeps the element's contents away from the edge of the element itself. Then we have borders. Now, borders are optional. You can have different styles of borders, such as solid or dashed or dotted, but borders also have a width and a color. So the width of a border, an element's padding, and the width and height of that element all calculate together to form the total width and height of the element. Now what about margins, where do they come in? Well, margins don't technically calculate to the width and height of an element. However, they do help define the relationship between elements.

So, they're used to determine the amount of space between one element and another. Now, the best way to truly understand the box model is to go ahead and control those properties for yourself. So, let's dive in and set some values. So, here I have the box_model.htm file open. You can find this in the 10_02 folder. Before we start modify some properties, I want to take a moment to discuss the structure of the page. Currently, we have a paragraph, which is within a div tag with an ID of container. That's also found within the body tag.

We have some very basic CSS going on our page right now. So let's take a moment and see what we have. So if I go over to the CSS Styles panel, I can see that I have a rule right here targeting the paragraph inside the container. All I've got is a background color. It's that light tan background color that we're seeing here. Now the container div tag itself also has a background color. It's sort of a darker teal blue color, but we can't see that right now. The reason that we can't see it is because if you have a block-level element, it's going to expand to fill its entire container element.

So right now our inner paragraph, since there is no padding or borders or anything holding it away from the edge of the div tag, is going all the way to the edge of the div tag. Let's start setting some box model properties and see how it affects these two elements. The first thing I'm going to do is select the #container p selector. I'm going to go ahead and add a width and a height to it. So I'm going to add a property. I'm going to add width. I'm going to give it a width of 300 pixels. Then I'm going to add another property. I'm going to add height and give it a height of 300 pixels as well.

Now, while I'm typing this in, you can see that I'm just sort of typing these values. But when you define a new property, anytime you say Add Property-- let me hit Enter so I get my height. But anytime you hit Add Property, you get a pulldown menu. So if you don't feel like typing these properties in that we're talking about, you can add them that way as well. Okay, so we have a height of 300 pixels. We have a width of 300 pixels. Now, that has created a dramatic change in our paragraph and our container. Within our paragraph now, we see the 300x300 size of it.

We now begin to see some of the background color of the containing div tag. So, why don't we see all of it? Well, your paragraph is lining up to the top left-hand corner of the div tag itself. That's pretty standard in HTML. Child elements will align top left-hand corner to their parent elements, unless you do something to move them away from that position. So, our paragraph is just butting right up against the top left edge. However, we've defined a width for our paragraph, but we haven't defined a width for our containing div tag.

Because of that, the containing div tag is now expanding to fill its parent container, which in this case is the body tag. So whereas we've defined the width and height for the paragraph tag, we haven't defined one yet for the container. Its height is being determined by its contents, in this case, the paragraph. Its width is being defined by the body tag. Now let's add some box properties to our containing element and see how it affects our layout. So I'm going to go up to the #container ID selector. I'm going to add a property here. I want to go ahead and define a width for it as well.

So I'm going to go ahead and add a width. I'm going to give it a width of 300 pixels. That's exactly the same width we gave the paragraph. When I hit Return, you'll notice that our parent container disappears. Or does it? Well, we don't see the width anymore, but within Dreamweaver here, we are seeing something going on top and bottom. What's going on there? Well, not maybe what you think. I tell you what, go up to the document toolbar and turn on Live View. When we turn on Live View, oh, that's what we expect. We expected to see nothing, because now the parent container is exactly the same size as our paragraph.

So, how come Design View shows us a little bit of space up top and a little bit of space on the bottom? What Design View is doing is it's taking into consideration the default margins for your paragraph. Every element in a browser has default margins. With a paragraph, we get about 16 pixels worth of space above it, 23 pixels worth of space below it. So how come we wouldn't see that when a browser renders it? Well, that is a curious little property called vertical margin collapse.

When a vertical margin touches another vertical margin, they collapse to the highest of the two values. So if you have a paragraph with 20 pixels worth of margin below it, followed by another paragraph with 10 pixels worth of margin above it, you wouldn't get 30 pixels between the two of them. You'd only get 20, the higher of the two values. The reason for that is we don't want all of our elements to be double-spaced. We want them to be single-spaced. That's why vertical margins collapse. Now, it even gets more curious when you evolve a nested tag with the parent tag like we have here.

When you have a nested tag, if its margin touches the edge of the parent container with nothing in the way, no other content, no other padding, no other margins, then you get this margin collapse. That is something that has caused problems in more than one layout, I promise you that. Let's add a little bit something more to our container and see if it deals with this vertical margin collapse. I'm going to go up to the containing element. Let's add a little bit of padding to this. We're going to go ahead and add 20 pixels worth of padding all the way around.

Now when we switch over in Live View, I see 20 pixels worth of space to the left and to the right of the paragraph. At the top and bottom, well, now I'm seeing more than 20 pixels. Okay, what's going on there? Well, remember what I said earlier, vertical margins collapse when a nested element's margins go all the way up and touches the edge of its parent container. It can't do that anymore. Now we have 20 pixels of padding right here, which means the margin also touches that padding. margins and padding would add together.

Now we're getting probably about 36 to 40 pixels worth of space between the top of the paragraph and the top of its parent container. Layout can sometimes be a little tricky. So these are things that you have to understand when you're starting you use box model properties to control your elements. Okay, let's take this a little bit further. Let's examine how we can use our box model properties to achieve a very common technique in CSS-based layouts, and that is the centering of one element inside of another one. I'm going to make the container a tad bit wider. Let's see.

I'm going to go to my container and I'm going to go ahead and make these 600 pixels wide. So let's say we want to center that paragraph inside of its parent container. All we have to do to do that is to take advantage of margins. If I go back into my paragraph selector, I can add another property. The property I'm going to add here is margin. This time I'm going to use a little shorthand notation. A shorthand notation allows us to add just one margin value instead of having to add separate values for the top, bottom, right, and left.

So what I'm going to do is 0 and then a space. Then I'm going to type in the word "auto." Let's talk about what that's going to do. The zero, the first value, is going to represent the top and the bottom. So when you use margin and use two values like we're doing here, the first value applies to the top and the bottom of the element. The second value applies to the right and left. So what we're doing is we're taking that default margin that's causing that little bit of extra space up top and we're going to zero that out. Now the next thing we're doing is auto-margin for the left and right. Now what auto-margin basically does is it says okay, calculate the width of the parent, split it in two, put half of it on the right, half of it on the left.

What's that going to do for us? Well, let me hit Return. That's going to center the paragraph right in the middle of the containing element. Later on, we're going to use that technique to center our page layout on our page. Notice also what happened to the spacing above our paragraph. No longer are we seeing that extra spacing. We're just seeing those 20 pixels worth of padding that we put in. By zeroing out that top margin, we've eliminated that extra spacing that was causing that little awkward gap that we had up there. All right, let's add one more box model property. I'm going to go right down through our paragraph.

Let's add a border. So I'm going to add a property to this. I'm going to type in border. Once again, we'll do a shorthand notation here. By hitting Tab, that will move me over to the next dialog box. I'm going to type in 5 pixels. So we're going to do a fairly thick border here, solid, #fff, so white. So 5 pixels, solid, and white, and hit Return. So now we have a 5-pixel border all the way around our paragraph. We have auto-margins right and left. We're getting our space up top from the padding of the parent containing element.

Now, I don't like the way the text is butting right up against the paragraph. So let's go ahead and fix that while we're at it. So I'm going to go ahead and add a property to our paragraph. We'll add some padding. Let's add 20 pixels worth of padding all the way around our inner paragraph as well. So now we get the layout that we're looking for, sort of. We have 20 pixels worth of padding all the way around the interior of our paragraph. We have 5 pixels worth of a border. Then we have some auto-margins going over here on the right and the left-hand inside. So when we're factoring up the total width of our parent containing element, we have to take a lot into consideration here.

First, we have to take in the width of the containing element itself. Currently, that width is 600 pixels wide, but we also have to take a look at the padding inside of it as well. We have 20 pixels worth of padding and 600 pixels worth of width. So, 20 pixels on one side, and 20 pixels on the other side. That gives us a total width of 640 pixels. For the most part, your interior content won't disrupt that width unless it's going to equal more than that. If we were trying to calculate the size of our inner paragraph, we would see it's 300 pixels wide.

We have padding of 20 pixels, which would be 20 on the left, 20 on the right, so 340, total. Then our 5 pixels worth of border would equal 350. So in this case, the interior paragraph really isn't affecting the outer div tag. But those are things that you have to keep your eye on when you're doing and planning your layout. Often you're going to find yourself subtracting certain width values or adding certain values to it, just so you can achieve the proper layout. I want to illustrate what that can do to your layout here for just a moment. Let's say I go back up to our containing element and I tell it instead of being 600 pixels to go ahead and be 300 pixels. What happens? Well, that's what happens.

Notice that the inner paragraph no longer will fit. So even though the inner paragraph is told to be 300 pixels wide, it no longer fits because of the padding and the border that we have on it as well. So what happens is our elements overflow each other. That's really not what we want. So if we're going to try to figure out how to have exactly the same amount of space all the way around this, let's say 20 pixels all the way around our outer paragraph, we've got to factor out what the width of the paragraph is, including its padding and its borders, and add the proper amount of padding around our containing element.

Let me show you what I mean. So our nested paragraph is 300 pixels wide. We have 40 pixels worth of padding when you add up both sides. So that's 340 pixels. Then finally, we have 5 pixels worth of border. So it's another 10 pixels. So now we're up to 350 pixels. So we have 350 pixels from the side- to-side, but we also want 20 pixels on either side of it. So if I go up to my containing element, I can say all right, let's go ahead and be 350 pixels wide. And we have 20 pixels worth of padding on either side.

And we get exactly what we're looking for. So, now that we have a handle on our margins, borders, padding, height, and width, and how they can affect overall dimensions and relationships with other elements on the page, we can move on to talking about more layout-specific topics. Next up, we're going to be using the CSS float property for layout.

Show transcript

This video is part of

Image for Dreamweaver CS5 Essential Training
Dreamweaver CS5 Essential Training

135 video lessons · 89053 viewers

James Williamson

Expand all | Collapse all
  1. 2m 57s
    1. Welcome
      1m 8s
    2. Using the exercise files
      1m 49s
  2. 7m 50s
    1. What is Dreamweaver?
      3m 16s
    2. Learning web design
      2m 22s
    3. Current web standards
      2m 12s
  3. 43m 9s
    1. The Welcome screen
      4m 5s
    2. Windows and Mac interface differences
      2m 23s
    3. The Application toolbar
      4m 7s
    4. The Document toolbar
      4m 40s
    5. Arranging panels
      8m 19s
    6. Managing workspaces
      7m 32s
    7. The Properties Inspector
      5m 54s
    8. The Insert panel
      6m 9s
  4. 25m 45s
    1. Basic site structure
      3m 11s
    2. File naming conventions
      1m 49s
    3. Defining a new site
      4m 35s
    4. Managing sites
      4m 51s
    5. Managing files and folders
      6m 36s
    6. Working with browsers
      4m 43s
  5. 27m 21s
    1. Creating new documents
      5m 16s
    2. New document preferences
      3m 6s
    3. Setting accessibility preferences
      4m 56s
    4. Working with starter pages
      3m 46s
    5. Managing starter pages
      10m 17s
  6. 30m 2s
    1. Basic tag structure
      2m 15s
    2. Adding structure to text
      8m 20s
    3. Creating lists
      9m 59s
    4. Getting text into Dreamweaver
      5m 59s
    5. Importing Word documents
      3m 29s
  7. 1h 17m
    1. Understanding style sheets
      2m 16s
    2. The anatomy of a CSS rule
      1m 48s
    3. Setting CSS preferences
      6m 36s
    4. The CSS Styles panel
      10m 2s
    5. Controlling CSS through the Properties Inspector
      5m 14s
    6. Using the Code Navigator
      7m 21s
    7. Using CSS Enable
      6m 45s
    8. Understanding element selectors
      8m 11s
    9. Understanding class selectors
      8m 49s
    10. Understanding ID selectors
      5m 59s
    11. Understanding descendant selectors
      6m 51s
    12. Attaching external style sheets
      7m 44s
  8. 1h 47m
    1. Working with units of measurement
      7m 11s
    2. Declaring font families
      9m 39s
    3. Controlling font sizing
      9m 9s
    4. Controlling weight and style
      8m 0s
    5. Controlling line height
      8m 29s
    6. Controlling vertical spacing with margins
      12m 3s
    7. Controlling spacing with padding
      5m 39s
    8. Aligning text
      8m 26s
    9. Transforming text
      5m 36s
    10. Writing global styles
      15m 42s
    11. Writing targeted styles
      17m 37s
  9. 1h 32m
    1. Understanding image types
      5m 3s
    2. Managing assets in Dreamweaver
      12m 51s
    3. Setting image accessibility preferences
      4m 20s
    4. Setting external image editing preferences
      3m 52s
    5. Placing images on the page
      7m 37s
    6. Photoshop integration
      5m 54s
    7. Modifying Smart Objects
      5m 51s
    8. Alternate Photoshop workflows
      8m 8s
    9. Modifying image properties
      11m 14s
    10. Styling images with CSS
      7m 11s
    11. Using background graphics
      9m 3s
    12. Positioning background graphics
      11m 6s
  10. 55m 16s
    1. Link basics
      3m 37s
    2. Setting site linking preferences
      2m 14s
    3. Creating links in Dreamweaver
      11m 1s
    4. Absolute links
      5m 8s
    5. Using named anchors
      11m 19s
    6. Linking to named anchors in external files
      2m 44s
    7. Creating an email link
      5m 24s
    8. Creating CSS-based rollovers
      13m 49s
  11. 1h 34m
    1. CSS structuring basics
      2m 56s
    2. The Box Model
      13m 21s
    3. Understanding floats
      6m 53s
    4. Clearing and containing floats
      8m 56s
    5. Using relative positioning
      4m 8s
    6. Using absolute positioning
      7m 18s
    7. Creating structure with div tags
      12m 7s
    8. Styling basic structure
      10m 34s
    9. Creating a two-column layout
      10m 37s
    10. Using Live View and CSS Inspect
      7m 51s
    11. Using Browser Lab
      9m 39s
  12. 56m 22s
    1. Reviewing table structure
      7m 41s
    2. Importing tabular data
      5m 13s
    3. Creating accessible tables
      9m 56s
    4. Using thead and tbody tags
      4m 0s
    5. Basic table styling
      8m 45s
    6. Styling table headers
      7m 52s
    7. Styling column groups
      4m 22s
    8. Creating custom table borders
      5m 1s
    9. Styling table captions
      3m 32s
  13. 1h 43m
    1. How forms work
      3m 0s
    2. Reviewing form design
      3m 2s
    3. Creating accessible forms
      7m 33s
    4. Setting form properties
      4m 6s
    5. The fieldset and legend tags
      4m 32s
    6. Inserting text fields
      5m 58s
    7. Inserting list menu items
      5m 26s
    8. Inserting checkboxes
      7m 50s
    9. Inserting radio button groups
      6m 22s
    10. Inserting text areas
      4m 12s
    11. Inserting submit buttons
      3m 37s
    12. Basic form styling
      12m 0s
    13. Form element styling
      8m 52s
    14. Styling form layout
      11m 49s
    15. Adding form interactivity
      2m 47s
    16. Using Spry validation widgets
      12m 49s
  14. 1h 23m
    1. Planning for templates
      10m 51s
    2. Creating a new template
      10m 37s
    3. Using editable attributes
      13m 43s
    4. Creating optional regions
      6m 23s
    5. Creating new pages from a template
      9m 17s
    6. Applying templates to existing pages
      6m 9s
    7. Working with nested templates
      7m 56s
    8. Working with repeating regions
      12m 58s
    9. Modifying templates
      5m 41s
  15. 40m 14s
    1. Behaviors overview
      3m 47s
    2. Hiding and showing elements
      9m 18s
    3. Spry overview
      4m 4s
    4. Using Spry widgets
      11m 36s
    5. Adding Spry effects
      3m 6s
    6. Using the Widget Browser
      8m 23s
  16. 28m 18s
    1. Inserting Flash files
      5m 4s
    2. Setting properties for Flash
      6m 27s
    3. Dreamweaver and Flash integration
      6m 6s
    4. Encoding Flash video
      6m 10s
    5. Adding Flash video
      4m 31s
  17. 45m 28s
    1. Running site-wide reports
      6m 33s
    2. Checking for broken links
      5m 41s
    3. Checking for browser compatibility
      8m 3s
    4. Adding remote servers
      8m 0s
    5. Uploading files
      7m 20s
    6. Managing remote sites
      9m 51s
  18. 34s
    1. Goodbye

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


You have completed Dreamweaver CS5 Essential Training.

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?

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?


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.