Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When people first learn how to control page layout with CSS, they're very eager to learn about things like floating, using positioning, and other details about page-layout techniques. While this is understandable, one of the unfortunate side effects of this is that people often don't pay enough attention to one of the most important page- layout concepts of them all, and that would be normal document flow. Normal document flow is exactly what happens to your page when you do nothing at all. It's the rules that all browsers use to control the appearance of elements on the page.
It's also more powerful than most people think, so let's take a closer look at normal document flow and how it relates to page layout. And to do that I've got the normal.htm file opened, and you can find that in the 01_06 folder in your exercise files. Now the first thing I want to do is just take this page and open it up in the browser so you can see what it looks like with absolutely no styling applied to it whatsoever. Okay, so this is the page with just the browser's default styling applied to it. We have a headline up top, we have a photo gallery with about six photos in it, and then we have little section down here where I expound a little bit on normal document flow and kind of what it is.
So essentially normal document flow says that elements are going to appear at the top of the page, they are going to read left to right and they are just going to stack one on top of another. So paragraphs are going to stack on top of each other. Headings, those block-level elements will occupy their own space within the layout. So here is the heading followed by another heading, followed by a paragraph. And then in-line elements, like these images for example, will appear on a line until they can no longer fit, and then they'll break down to the next line. So this is normal document flow in a nutshell. Now, I want to show you what we can make the page look like by just applying a few block-level properties that we already know how to control.
So I am going to flip over to the finished version of this, and we are going to do this in just a moment. And this is what you can do to the exact same content through just the use of CSS styles with no "positioning properties," only the box model properties. No floating, no positioning, nothing like that. Okay, so I am going to jump back in our code, and we'll go ahead and get started on this. All right, so the first thing I want you to do is to take off the comments. So I've got some code already commented out. If you look at about line 11, that forward slash star, that's the beginning of a CSS comment.
I am going to get rid of that. And then I have to go down to the very bottom of our styles and get rid of the star forward slash. So I've just gone ahead and uncommented those out. Now before we go back up and begin working on our styles, let's take a look at the structure of the code, so we'll know what it is that we are controlling. So we have a header and inside the header element we have a single h1. Then we have two articles. The first article has a class with attribute of gallery and inside that we have a heading, a paragraph. And then we have this div tag right here but with a class of photos, and that just has that series of images inside of it.
So we have six of those images. Notice they are each 250 pixels wide. And then when you're using the div tag as a wrapper to help us control that, so we are going to control exactly how many of those images fit on a line by controlling the size of the outer wrapper around it. Following that, we have another article. Its class attribute is normal and it just has some explanatory text that goes into normal document flow and is certainly worth reading through as you work on the exercise. Okay, so I am going to go back up into my code. Now a lot of it, I kind of already pre- populated to save us a little bit of time, so I am just going to start from the top and talk a little bit about what the existing code is doing.
Our first line is telling any browser that doesn't support the newer HTML5 elements how those only should be displayed and the fact we want them treated as if they are block-level elements, so we're telling the browsers to do just that. Most modern browsers you won't have to do that with, but just in case somebody is happening to use an older browser, that line of code comes in very handy. After that, we're stripping off default margins and padding for just a serious of elements, the div tags, headings, paragraphs, and image. This is something that's called a CSS reset and we are actually going to take a closer look at CSS resets in their own movie in just a little bit.
For the time being, just understand that we are sort of telling the browsers, hey, if you are already putting margins and padding on these elements, don't. We want to control them on our own. Following that, we do the same thing to the HTML tag, which is a root-level container of our file, and we're applying a background color to that. And for our body, we're applying a little bit of padding. Notice we are applying 25 pixels' worth of padding to the bottom. That's just to hold the content on the bottom away from the bottom just a little bit. Applying just a slight bit of typographic formatting here, where we are saying, hey! Go ahead and give us your default font size and we are going to use Georgia, and then we are going to give it a background color as well.
All right, well let's add to the body selector. So, on another line inside of our body selector I am going to go ahead and set the width of the body's content to 900 pixels. So doing that, we're basically constraining all of the content on the page, and we're saying we only want it to occupy 900 pixels wide. Now the padding gets added to that, if we had padding on the right and left sides, but we don't, so this is going to be constrained to just simply 900 pixels wide. Right now, on the next line, go ahead and apply a margin and here for margin we want to do zero for margin top and bottom, so no margin top and bottom, and then auto left and right.
Okay, so what this does for us is this is a very simple technique for centering content on the page. So by saying no margin top and bottom, we're just sort of making sure that the content adheres to the top of the page. And for when we say auto for left and right, we're saying okay, the content needs to be 900 pixels wide, but on the left side and on the right- hand side, if you have any space left over, go ahead and split that space and put it equally on the left-hand side and the right-hand side, and that has the result of centering the content on the page. Okay, I am going to keep going down. And for our header element, we're going to apply a few properties to that as well.
The first I want to do is just a little bit of padding, so I am going to type in padding. I'll 20 pixels' worth of padding for the top and the bottom, and then I'll do 25 pixels' worth of padding for the left and the right, so 20 pixels, space, 25 pixels. That will give me a little room above and below at the top and the bottom of the header, and it will give me a little room side to side as well. And then we are going to give it a background color, so I am going to type in background: rgb, and the color I am going to give this is 76, 67, 65. And it's sort of that dark sort of brown gray color, if you will.
I call it ash, but I don't really know what it is. So now we've given a background color to our header, and we've got a certain amount of padding for us here. Right now I am going to go down to my article. Now remember, we have two articles on the page: the top article has a photo gallery and then the bottom article just has the sort of informative text about the normal document flow. So this is going to be very generic styling to sort of style both of those regions. So the first thing I want to do is type in some padding, and I am just going to do zero padding top and bottom and 25 pixels' worth of padding left and right. So it kind of mirrors what we are doing with the heading, as far as the left and right padding goes, and assures that content will sort of lineup, but in the header em and with those articles.
And then one last thing we are going to do to article is we are going to do is a margin-top, and here we're going to do a top margin of 25 pixels. What that will do is it will create a little bit of space above each of the articles, 25 pixels' worth of space, to give separation between the headers and their articles and the two articles themselves, okay. So I am going to go ahead and save this and before we start working on our galleries, I want to preview it. So I am going to go back into my browser and I want to preview the changes we've made. Okay, so that's a lot of changes. Now I know a lot of the work was sort of already done for us, but look what we've done. We've centered our content on the page.
We've restricted it or restrained it, I should say, to 900 pixels wide from side to side. Here is the 25 pixels' worth of padding that we've placed already coming into play, and it's looking fairly decent. So the next thing we need to do is we need to work on our photo gallery. Now one of the things I want to do for our photo gallery is I want to make sure our photos are displaying in two columns of three, and right now they're doing that. And the only thing that's controlling that right now, there is no floating that's controlling that. There is nothing controlling that other than the constraints that we've already placed on the content. Remember, these images are 250 pixels wide.
Our page is 900 pixels wide. So there is a limit as to how many of these guys can fit on a single line. Because images are in-line elements, meaning they stack from right to left until they get to the end of a page or the end of the available room for content and then they go and form the next line, we don't really need to do anything fancy to get two rows of images; we merely need to restrict the space that they are in. Since they are already sort of restricted, what we are going to work on now is sort of styling that photo gallery so that it looks maybe a little more presentable than we have it right now.
Now one of the things that we can do for that is remember we have the div tag that wraps around those images, so by controlling that div tag, we can sort of create a defined visual region for those images. So I am going to go back in our code, and that is the first we are going to tackle is this photos. Remember, that's the div tag that surrounds those guys. So what I am going to do for that is I want to type in margin: 0 and then auto. So we are doing the same thing for the photos region that we did for the page itself; we are going to center it within its container. And the photos container is that body tag, so this should be centered within that single-column layout that we have going on.
The next thing I want to do is define a width for it as well. So I am going to choose a width of 822 pixels. Now why 822? It sounds kind of how weird. Bear with me. We'll get into that when we start talking about the widths of the actual images themselves. The next thing we want to do is we want to do some padding, and we are going to give it a padding of 10 pixels all the way around, and then we are going to give it a background. And the background we are going to give this is rgb (100, 98, 102), and that's kind of a gray, if you will.
All right, so I am going to save this and again sort of update my browser. You can see what this is doing for us. It's actually right around 842 total pixels of width, because of the padding on either side, but you can see that this is the region that we want our photos in. We've got padding, 10 pixels' worth of padding going around. And now what we want to do is we want to sort of space our images in this area so that they have a little bit of space between them and that they are centered as well. Now by defining the width the way we did, we've actually helped ourselves out a little bit. And I'll be honest with you.
When I plan this out and when you plan something like this out, you are going to approach it from the images first and then figure out how much space you need. So let me tell you how I did that. Okay, so our images are 250 pixels wide each. I want to get about 20 pixels' worth of space between each of them. So what I am going to do is I am going to 10 pixels of margin around the images. Now, vertical margins collapse unless you are dealing with in-line boxes the way we are already here with images, so I'm not actually having to worry about collapsing margins. I can just go ahead and put a 10-pixel margin around these images and I'll know that I am going to get 20 pixels' worth of spacing.
I have the padding going all the way around the interior of the div tag too, so the 10 pixels of margin on the outer edges of images will combine with that padding on the div tag to give me 20 pixels' worth of space there as well. I am also going to go ahead and put a two-pixel wide border on my images, but I can't ignore that two pixels when it comes to the total width of the images. So if you have a calculator handy, you can look at it this way. Each image is now 274 pixels wide. We've got 10 pixels of margin on each side and the two pixels of border on both sides of it.
If I multiply that times three, I get 822, and 822 with the 10 pixels' worth of padding on either side gives me the total width of 842. So math, maybe we should have paid more attention to it on our school, I don't know. All right, I am going to go back into my browser, and let's go ahead and set those values on our images and see what we get. All right, so the first thing I want to do with my image is just give it a margin of 10 pixels, and then the next thing I want to do is apply that border to it, so a border of two pixels, solid, and then white. I am going to save this. I'll preview it in my browser, and as if by magic, we get exactly the spacing, we are looking for.
We get our borders around it, which helps offset the photos a little bit. And because we carefully planned the width of that, we get equal spacing all the way around and we get a nice little container there with our div tag. That is very, very nice. Now, I know that this layout is very, very simple, but it is a perfectly good, single- column layout, with no styling required, outside of the simple box model properties that we've been controlling in order to make it work. You know, when I first started out controlling page layout through CSS, I'll be honest with you guys. I really, really overdid it. Now I was trying to float or position almost every single element on the page.
I thought that somehow if I didn't, I wasn't really controlling my layout through, you know, positioning. Oddly enough, the more experience I gained through doing page layout, the more I began to rely on normal document flow to do the majority of the work for me. You know, at this point, I try to do as much possible with normal document flow and then just merely tweak the layout using floats or positioning when required. Now this approach uses less code, it's more likely to be compatible across a wider array of devices and browser versions, and requires less work on my part.
So, I know the focus of this title is on page-layout techniques, but as we work, pay attention to how important normal document flow is to the majority of our layouts.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 65513 Viewers
61 Video lessons · 96305 Viewers
56 Video lessons · 110064 Viewers
82 Video lessons · 105055 Viewers
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.