Check out a free preview of the full Introduction to CSS course:
The "The Box Model" Lesson is part of the full, Introduction to CSS course featured in this preview video. Here's what you'd learn in this lesson's course:

Jen gives an overview of the box model and explains how margin, border, and padding come together within it.

Get Unlimited Access Now

Transcript from the "The Box Model" Lesson

>> Jen Kramer: What we're gonna talk about today we're gonna talk about box models, we're gonna talk about the display property. We're gonna talk about how to make your navigation bar so that they go horizontal, vertical, look pretty, we're gonna talk about floats and we're gonna talk about flex box.

That is about five weeks of material we're gonna cover, [LAUGH], which is why we need to go a little bit long today. And then I have a project for you at the end of all this, that's gonna combine everything that I've talked to you about today. And you'll be able to put that together into one document it's gonna be a blog website that you're gonna lay out, sound good?

So that is the roadmap for today. So we are gonna rely heavily on our website here at be sure to click on the third link for layout and we're gonna be working from this today. And there's links in here, a lot of links to CodePens that we're gonna work with rather than doing so much in the VS Code, we're gonna do more with CodePen today.

So let's go ahead and get started then, we are taking a look at that website, and the very first thing that I have here for you is a diagram, okay, and this diagram is a diagram of the so-called box model in CSS. Every single HTML element anywhere on your webpage has a box around it, that's all your website is, it's boxes and boxes and boxes and boxes.

And the boxes are everywhere and you can never get away from them, okay? There are no circles, there are no triangles, there are only boxes in CSS and every box around every element has three properties, that we're going to discuss in great detail today, so these three properties are the margin the border and padding.

So border is the easy one, right? We did a little bit of work with border yesterday that puts the pretty line around whatever it is that we're working with, so that one's pretty straightforward. Border can be put together in a whole bunch of different ways, you can have just one border in which case that would make what?

If you only had a border on one side, that would be a?
>> Speaker 2: Line.
>> Jen Kramer: A line, [LAUGH] okay, so you can put a border just on one side and make a line with it. You can put it on two sides and make parallel lines maybe, yeah? You can put it two going this way.

You can put it on three sides. So we'll talk about all the ways to do that. There is the distance between the actual content, whatever you have, whether it's words or pictures, the distance between that content and the border is called padding, okay, that's padding. So if you don't want your border smashed up against your picture, you want a little breathing space.

You don't want your border smashed up against your text, you want a little breathing space. You do that with padding and once again, padding can be on one side, two sides, all the sides, we'll talk about how to do that too. And then finally, margin is the space between the border and the rest of your web page, so that's the space outside.

So some of you yesterday wanted to put space, wanted to have one big box here and I wanna have a space, and I wanna have a box here. Margin is gonna be responsible for that space in between those boxes, all right? So that's how we define margin, padding, and border and I will show you here on the webpage just so that you have this for reference.

We've talked about all of these properties here in great detail, you can scroll through all this wonderful stuff about padding, margin width, read about it in detail. And so with that I'm gonna go ahead and start with the CodePen, so back at the top of that, there is a CodePen here, just above that graphic, go ahead and click that link.

>> Jen Kramer: And it doesn't open in a new window, so I'm gonna open mine in a new window, okay, [COUGH] and it's a very simple little CodePen that we have going on here.