Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
CSS gives Web designers control over the appearance of their web sites by separating the visual presentation from the content. It lets them easily make minor changes to a site or perform a complete overhaul of the design. In CSS Web Site Design, instructor and leading industry expert Eric Meyer reviews the essentials of CSS, including selectors, the cascade, and inheritance. The training also covers how to build effective navigation, how to lay out pages, and how to work with typography, colors, backgrounds, and white space. Using a project-based approach, Eric walks through the process of creating a Web page, while teaching the essentials of CSS along the way. By the end of the training, viewers will have the tools to master professional site design. Exercise files accompany the training videos.
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.
There are currently no FAQs about CSS Web Site Design.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.