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

Box model review

From: CSS: Page Layouts

Video: Box model review

Before we start exploring the individual techniques of page layout, I want to begin with a general overview of CSS page-layout concepts. At the core of almost all page-layout techniques is the box model, so that makes reviewing it a logical starting point. Before we get into that, however, I feel like I need to mention again that this course is a continuation of my CSSL Core Concepts course. That means that in this course we are going to be building on the material that covered in the CSS: Core Concepts course. There will be some repetition, designed to reinforce important concepts, but for the most part, if you're new to CSS and are starting with this title, I actually recommend that you go back and start with the Core Concepts title and then come back to this on when you finish.

Box model review

Before we start exploring the individual techniques of page layout, I want to begin with a general overview of CSS page-layout concepts. At the core of almost all page-layout techniques is the box model, so that makes reviewing it a logical starting point. Before we get into that, however, I feel like I need to mention again that this course is a continuation of my CSSL Core Concepts course. That means that in this course we are going to be building on the material that covered in the CSS: Core Concepts course. There will be some repetition, designed to reinforce important concepts, but for the most part, if you're new to CSS and are starting with this title, I actually recommend that you go back and start with the Core Concepts title and then come back to this on when you finish.

I had pretty much said the exact same thing in the Welcome movie, but I know that a lot of you guys skip those, so I wanted to mention it again here before got too far into the title. Okay, cool! Now the box model is the term that we use to describe the rules and properties surrounding the physical properties of all HTML elements on the page, and basically every single element on your web page is considered to be in a rectangular box. The properties of this box--its width, height, padding, borders and margins-- define not only the size of the element, but also how it relates to all of the elements around it.

Without these properties, we would not be able to control layout at all. So to visualize these properties a little bit better, we are going to go ahead and modify the properties of a sample element. Okay, so here I am, in Aptana Studio again. Feel free to use whatever code editor you want. It does not matter, in terms of which tool you use. You just want to be able to access and modify the code. That is it. You can even use just a basic text editor if you want. All right! I have the box-model.htm file opened, and you can find that in the Chapter_01 folder, 01_01 directory.

Just a brief overview of the structure of the page here. You can see we have some default styles in the head of the document and if I scroll down, very simple HTML file. We have two div tags, one wrapping the other one. The outer div has a class of outer applied to it, the inner one has a class of box, and inside that we have a paragraph. If we look at the styles, we have some preexisting styles for outer that are giving it a little bit of definition. We have a background color for our paragraph that's going to show you what makes it a paragraph itself, and then we have a blank selector, an empty selector here for box, and that's of course what we are going to be experimenting a little bit.

Now before we do that, let's just preview this in a browser and kind of see what it looks like. So I am going to go right up here in Aptana Studio to my configurations. Now one more Aptana Studio note, if you are using it along with me: if I click on this little pulldown menu for Configurations, I can see all of my browsers here. You may not see that list. You may have those browsers installed, but Aptana might not pre-populate that list. So if I go right here to Run Configurations, I can go down to my web browser configurations and choose a new one and then just point it to the different browser executable files that I want, and you will have to do that for each browser that you want to load in, all right! So I am going to go ahead and preview this, say, in Firefox.

I can see the blue color here is the background color of the outer div tag; the orange color is the background color of the paragraph, okay. So I am going to go back into my code, and let's start adding a few things to our box element. Now the first thing I am going to add is a background to this. And I am just going to go ahead and give this a color of #f90. Now, the background property is not considered part of the box mode; however, it does help us define this element, and you will often see it included in discussions of the box model. But in terms of whether it is a part of the box model or not, it's generally not considered that. All right! So if I save this file and go back into my browser and reload the page, I can see that it doesn't look like really anything has changed.

Well, that has everything to do with sort of the stacking order, if you will. This paragraph is sort of stacked on top of the div tag that it's inside of, which is in turn stacked on top of the div tag that it is inside of. So the color is there, but because the inner div tag doesn't really have any properties assigned to it, it's sort of shrinkwrapping to our paragraph, and that's why we don't see the background color. All right! I am going to go back into my code. Now let's go ahead and add some definition to our box. Let's go ahead and give it a width of 300 pixels and let's give it a height of 300 pixels as well.

All right, so I am going to save that, preview that in the browser, and now we can see a little of a change here. We see that the paragraph looks like it's a lot wider, and it's a lot taller, but again, we are not seeing the background color. Now the reason for that has everything to do with how block-level elements react when they are inside of another element. Now block-level sort of are elements that occupy through their own space within the normal flow of the document. Paragraphs are block-level elements, div tags are, headings, or things like that. Well, a block-level element is told basically that if it doesn't have a defined width and height, it should just go ahead and expand to fill the container element.

So in this case, it's expanding to fill the div tag that we just made a little bit larger. Okay! The next thing we want to do is let's throw some padding on here. So I am going to come into our box properties. I am just going to type in padding, and I am going to type in 25px. Now this is padding's shorthand notation. We could also say padding-top, padding- bottom, padding-right, padding-left, that sort of thing. By saying just padding and passing one value into it, we are going to get the same amount of padding all the way around our element. So remember, padding is the space inside the element. It separates the content from the border edge of the element itself, so we should expect to see a little bit of cushion between the paragraph now and the edges of this inner box.

So we save this and preview it, and indeed that is exactly what we get. Notice that the color that we defined for the inner box is a slightly darker orange. So what we are seeing on the inside, that's the paragraph's dimensions, and now on the outside, we're seeing the box for the actual box dimensions for the inner box. Now one thing to note here is that even though we've defined its width and height at 300px by 300px, the 25px that we've just added all way around it have actually added to the overall width and height.

So now we have 25px on this side, 300px here, and then 25px on this side. So now the total width of our element is now 350px. Okay, so what about borders? Borders are also considered part of the box model, so right after padding, I am going to type in border and then we are going to do 2px solid black. So again, this is shorthand notation. This is us telling the user agent that we want a border all the way around of our element. We want that border to be solid, we want the color to be black, and we want it to be 2px in width.

So again, if I save this, preview it in my browser, I can see that indeed our inner box now has that 2-pixel black border. If you're wondering, yes, that is cumulative as well. So we've added an additional 2 pixels on either side of this, so now instead of being just 350 pixels wide and 350 pixels tall, it is now 354 pixels wide and 354 pixels tall. All right! One last property to take a look at and that is margins. So if I come back into my code and I go down to the next line and type in margin, and we'll just go ahead and do 25 pixels of margin there as well.

Just like padding, we are using shorthand notation here for margin. Now margin doesn't always calculate to the total width of an element. It basically controls the spacing from one element to another. And almost always it does calculate in terms of the space it's going to take up within the layout, but there are instances, for example, when it's inside of a container that's too small to contain it, where sometimes the margins will basically be ignored, or in the case of a vertical margin, sometimes that is going to collapse. That actually is such a big deal that we are going to discuss that in a separate movie in just a little bit, when we talk about calculating element dimensions. All right! So I am going to go ahead and save that, preview that in the browser. And it doesn't look like a whole lot has changed, except for it looks like our box has shifted position.

Essentially, what's happening here is now this margin is pushing the top of the box away from the top edge of the containing element, and it's pushing the edges away. So basically, it's sort of creating its own space inside the containing element at this point. Now what about total width and height? Well, remember we have 300 pixels here; we have 25 here and 25 here, so it's 350. If you take a look at the margins, that's another 25 on either side, so that's 400 pixels wide. But the border, remember 2 pixels and 2 pixels, so that'd be 404 pixels wide, or at least it's taking up 404 pixels' worth of space.

It may not be that physically wide, but it's at least taking up that amount of space. So just remember that width, height, padding, borders, and margin, all those properties combine to make up the box model. Now when we plan our layouts, you are going to need to understand how all those properties combine to create the overall width and the height of an element. Like I had mentioned just a second ago, that is so important we are going to take a closer look at calculating element dimensions in our next movie.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

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

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.