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

Box model essentials

From: CSS Web Site Design

Video: Box model essentials

In this movie we're going to take a moment and talk about the CSS box model. This is the model that CSS house for drawing element boxes. Every element generates a box whether or not it's a continuous box or not. So, this is sort of the basis of everything in XHTML or everything in CSS, generates a rectangle. So, what I've done here is actually, I have with a little bit of trickery created a way in which we can actually see the margins. Margins typically are completely transparent. In fact, not just typically, but always completely transparent, but what I've done is I've wrapped another element around the paragraph, put a background image on that wrapping element so that that shows through the margin. So anywhere you see this crosshatch that's actually a margin on the paragraph. We can see that in the source, the wrapping div is what's actually used to visualize the margins on the paragraph.

Box model essentials

In this movie we're going to take a moment and talk about the CSS box model. This is the model that CSS house for drawing element boxes. Every element generates a box whether or not it's a continuous box or not. So, this is sort of the basis of everything in XHTML or everything in CSS, generates a rectangle. So, what I've done here is actually, I have with a little bit of trickery created a way in which we can actually see the margins. Margins typically are completely transparent. In fact, not just typically, but always completely transparent, but what I've done is I've wrapped another element around the paragraph, put a background image on that wrapping element so that that shows through the margin. So anywhere you see this crosshatch that's actually a margin on the paragraph. We can see that in the source, the wrapping div is what's actually used to visualize the margins on the paragraph.

So if we were to make this margin really huge, like 50 pixels on top and bottom then, there you go. That's 50 pixels a margin on the top and bottom and again we're seeing this pattern through the margins, they are not actually being applied to the margins. You can never make margins directly visible. You can only sort of make them implicitly visible like I have here. So, if in the future you think to yourself, wow, it would be really cool if I could put something in the background of the margin, just the margin of this element, that's not really possible.

But what this can show is if we just put 1em of margin all the way around the edge of this paragraph, then you actually get the margins on all four sides. Now, 1em and em is the unit of measure in CSS, well is related to the font size. In this case, a margin of 1em means exactly the same as the font size for the paragraph. If I made the font size of the paragraph temporarily 10 pixels, which is too small to comfortably read, but it makes it easy. Now, if I make it 10 pixels, then the 1em margins will calculate to be 10 pixels wide, so that's 10 pixels of top, right, bottom and left margin, all the way around, whereas if I take the font size up to 20 pixels, 20 pixels of margin all the way around the edges.

So, I'm not going to actually set direct font sizes, but there is margin, there's border, and you can see there's a border on the inside there and that border goes around the content, but inside the margin of an element and there's also padding, padding going between the margin, or going between the border and the content.

Now, I'm actually going to change the margin and padding to the pixel based for a moment, and you can see that there you have pixels instead of ems. I tend to like ems, because they scale with changes in font size. If I, in my web browser here, hit Cmd + and make the font size bigger, you can see that the margins are actually growing and if I bring it down, then they shrink. This is one of the reasons why I really like to use em based measures for margins and padding because then they will scale along with any change in the text size.

I've been known to do em-based borders, but borders, honestly, if you're using them are almost always going to be pixel based, either one or two pixels in my experience, although they do occasionally go larger. This isn't always the case, there are times when I'll do like a thick top border on an element, I can say border top of a third of an em, and then that border will also scale with changes in text size.

Those are infrequent at best, so what we have there is how the box model works. Now, there's one more thing that should be pointed out, and this is when I'm going to go back to pixel sizes. Suppose I also set a width for my paragraph. I'm going to give it a 300 pixel width and I'm going to hit Reload. Now this is actually very useful to see. What's happening here, if you were to count the pixels from one border to the other, it would actually be greater than 300 pixels. Setting a width of 300 pixels, actually means that you're setting the width of the content area of the elements. In this case, the content of the paragraph is 300 pixels wide, and then there's 10 pixels of padding outside of that. So that's now a total of 320 pixels and then the two side borders each add up pixels, so you have 322 pixels from outer border edge to outer border edge, going horizontally and then there's this 10-pixel margin on the left. On the right, you might think, well there's another 10 pixel margin and then there's just sort of an filled space, but actually, technically the way CSS is written, what happens is, if you have more space on the right side than your margin would ordinarily fill, then the margin is automatically increased to fill that entire space.

The end result is basically the same, but it's something worth keeping in mind. This is true for elements and what is known as the normal flow, which is to say just plain old elements that have text and they're not floating their position or otherwise have had interesting things like that done to them, that they're just sort of normal text. Elements which are floated, which will we talking about here, in an upcoming video, don't have this sort of magic margin expansion. So it's like I say, it's a subtle point but it's one that that's actually worth mentioning given what we're seeing here.

There's a way actually if you had a situation like this where you wanted to quote, unquote, right align your element, what you do instead of saying margin just 10 pixels all way around is set your top right and bottom margins to be 10 pixels or whatever it is you want and make the left margin auto for automatic. That means that the left margin is the one the will automatically size to fill in whatever space is available. So, that's a way to push a box over to the right side, if that's what you want to do and then this actually also means that if you make both your right and your left margins auto, that they'll split the difference and center the box. So, a few little things about the box model there and, and margins.

But in the end, the important thing to remember here is that if you set a width, if you set an explicit width of some type for an element you're only setting the width of the content area and any padding our borders or margin beyond that are added to that web so we don't have a box here that's 300 pixels wide, we have a box that's actually 322 pixels wide, from the outer border edge to the outer border edge. So that's a look at the box model and in the next videos we're going to see how to make use of that in laying out the Javaco design and create columns, for example.

Show transcript

This video is part of

Image for CSS Web Site Design
CSS Web Site Design

73 video lessons · 61489 viewers

Eric Meyer
Author

 
Expand all | Collapse all
  1. 14m 34s
    1. Welcome
      28s
    2. What is CSS?
      5m 34s
    3. Design tour
      2m 38s
    4. Using the exercise files
      1m 20s
    5. Installing the Web Developer toolbar
      4m 34s
  2. 25m 56s
    1. XHTML essentials
      3m 55s
    2. CSS essentials
      5m 17s
    3. Embedded style sheets
      2m 20s
    4. Linking a style sheet
      2m 19s
    5. Linking to multiple style sheets
      2m 20s
    6. Using linked and embedded style sheets together
      4m 21s
    7. Using imported style sheets
      5m 24s
  3. 57m 48s
    1. ID selector essentials
      6m 38s
    2. Class selector essentials
      4m 9s
    3. Best practices for classes
      4m 52s
    4. Grouped selection
      4m 2s
    5. Descendant selectors
      6m 44s
    6. The sources of style
      6m 38s
    7. Specificity
      8m 21s
    8. Making things important
      4m 32s
    9. Inheritance essentials
      5m 12s
    10. Making things really unstyled
      4m 2s
    11. User style sheets
      2m 38s
  4. 39m 3s
    1. Box model essentials
      7m 35s
    2. Simple floating
      5m 3s
    3. Using float for layout
      5m 5s
    4. Fixing column drop
      5m 35s
    5. Clearing essentials
      4m 20s
    6. Float containment
      6m 35s
    7. Creating a navbar from a list
      4m 50s
  5. 38m 3s
    1. Coloring text
      4m 13s
    2. Defining color in CSS
      8m 12s
    3. Coloring backgrounds
      6m 35s
    4. Applying background images
      4m 19s
    5. Manipulating the direction of background images
      2m 52s
    6. Positioning backgrounds
      7m 23s
    7. Background shorthand
      4m 29s
  6. 58m 28s
    1. Altering line height
      7m 32s
    2. Font style and weight
      5m 45s
    3. Sizing fonts
      9m 59s
    4. Using font families
      10m 38s
    5. Font shorthand
      6m 5s
    6. Justifying text
      4m 56s
    7. Vertically aligning text
      4m 22s
    8. Transforming text
      3m 49s
    9. Text decoration
      5m 22s
  7. 44m 40s
    1. Margin essentials
      14m 21s
    2. Adding borders
      6m 52s
    3. Padding
      9m 17s
    4. Using negative margins
      7m 19s
    5. Margin collapsing
      6m 51s
  8. 20m 38s
    1. Styling tables and captions
      5m 23s
    2. Styling table cells
      6m 30s
    3. Styling a column with classes
      4m 51s
    4. Styling links inside table cells
      3m 54s
  9. 30m 40s
    1. Styling for specific mediums
      4m 3s
    2. Creating a print style sheet
      6m 35s
    3. Hiding layout for print
      4m 11s
    4. Styling for print
      6m 34s
    5. Complex styling for print
      4m 37s
    6. Creating a footer
      4m 40s
  10. 37m 23s
    1. Getting started
      1m 11s
    2. Setting global styles
      4m 4s
    3. Defining masthead and navbar colors
      3m 27s
    4. Layout of the navlink bar
      3m 38s
    5. Using columns
      4m 49s
    6. Setting content styles
      1m 53s
    7. Creating the sidebar boxes
      5m 42s
    8. Creating the sidebar form
      3m 23s
    9. Completing the sidebar
      3m 29s
    10. Making a table
      3m 12s
    11. Creating a footer
      2m 35s
  11. 1m 28s
    1. Goodbye
      1m 28s

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 Web Site Design.

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.