Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When I first started doing CSS-based layouts, I created a lot more work for myself than I needed by attempting to position everything, and I'm talking every single element on the page. It's a common mistake that many people make when they first attempt to let a CSS handle their layout. By comparison, I find that the majority of my layouts now contain only a handful of positioned or floated elements. In the discussion of strategies for CSS -based layouts, often left out of the conversation entirely is normal document flow. Most people don't discuss it because they feel that everyone inherently understands it.
Since one of the best layout strategies is to let normal document flow do most of the work for you, I think it's worth taking a closer look at XHTML's normal document flow. Stated plainly, most documents will read left to right, top to bottom. Block level elements such as paragraphs or headers occupy their own space within the document flow resulting in them stacking vertically on top of each other. Some block level elements can be nested inside of one another such as Div tags, but for the most part, they create new lines above and below them within the document.
Inline level elements are typically contained within a block level element and they don't stack vertically, but display from side to side and left to right. Spans and EM tags and images are examples of inline elements. A page that relies on normal document flow can be modified by changing element box model properties such as margins and padding to change how elements interact with each other. By careful consideration and planning, the bulk of your layout can be achieved without a single positioning attribute. So, here we have the 10_03 file open and what this is it's a display of some of the prints that we have available for sale on the Desolve.org site.
I'd like to lay these out in a little bit more of an attractive fashion and arrange the prints in ordered rows. We have a pretty basic page structure. We have a header region. We have an area defined for prints. These are both being done with Div tags. Then we have a wrapper div tag that wraps both of those elements as well. That's going to allow us to do define some width and center the design on the page, all sorts of fun things that we normally like to do with CSS-based layouts. So, the first thing we're going to do is start by adding a background or two to help define these regions.
So, I'm going to go over to the CSS Styles panel and we already have some of our styles in here and I'm going to select the header selector and you can see that one is empty. We don't have any properties in that yet. So, what I'm going to do is add a background property and we're going to give this a background color of #4f4872. So now we've got sort of a purple background color for our header. Now, you'll notice that the elements are butting right up against the edge of the header, they may be a little hard to read.
So, let's go in and add a couple of more properties to this. First thing we're going to do is add some padding, and that's going to keep the elements inside of it away from the edge of the header. So, we're going to go ahead and give that a padding of 1em and you can see that makes our header taller, keeps the elements away from it and it makes it look a lot more like a defined region. Now, the next thing that we're going to do is we're going to add a little bit of margin to the bottom to help push the header and these prints away from each other. So, I'm going to add another property and this time we're going to be explicit about the margin we're adding. We're going to add margin-bottom and we're going to pass 1em worth of margin underneath that.
And you can see it just pushes our prints down. So, now we have a defined header region and it's separated from the other content on the page and it's got some padding in it which helps make it look more like a singular region, and the text doesn't bud up right against the edge, cool! Now, we need to modify the content inside of it so that it reads a little easier. So, what I'm going to do is find my #header h1 and I'm going to find this color right here and you can just literally click that color chip and change that to white. I'm going to select the header of paragraph rule right underneath that and I'm going to change this color to #CCC, which is a gray.
Okay, so our header region is styled, positioned and placed exactly where we need it. It's the first element on the page, so it's going to appear on the top of the page, just as you would normally expect headers to do. Now, in our print section, we're going to need a little bit more work here. So, we have a Div tag that surrounds all of our images and that Div tag has an ID of prints. You can see we do have a print selector and currently the only thing that it's doing is assigning a background color of #333. You can see that gray background showing through here, where the prints don't really take up any additional room.
So, one of the first things we're going to do is define a width for the prints. Each one of our images is 125 pixels wide. Based upon the amount of spacing we want between these images and based on how many we want to display in a single row, we could calculate that value and set it as the value of the width for our prints. So, we're going to go ahead and do that. Grab your print selector and let's go ahead and add a width property. We're going to go ahead and give it a width of 675 pixels. Now, as soon as we do that, notice that the number of prints that it can display in a single row, changes.
We now have 5 prints to a row and that's exactly what we want because we have a total of 15. Now, we don't have any spacing between the prints right now, but notice that the defined width allows some extra spacing. So, once we start pushing our prints away from each other, they'll fill that up and we'll space them evenly inside that region. Now, I'd like the prints to be centered within our layout, and currently they're not. You can achieve centering really easily by using some margins. So, what I'm going to do is I'm going to add another property and I'm going to type in margin. I'm going to add 0 margin top and bottom and then a space and then auto-margin for left and right.
Now, we've done that centering technique before, but since we're in a chapter on layout, it really bears talking about here. Setting the right and left margin to auto is basically telling the container element, whatever space you have left, evenly divide it between the right side and the left side of this element. Because we have a defined width on the element, we know that there is going to be a little bit of extra space left over. And when we hit Enter, you'll see that it evenly distributes that from the right side to the left side, centering the element. Now, this works in almost every single browser. Some older versions of Internet Explorer seem to have a little bit trouble with it.
But as you'll see later on, when we tackle page layout, there are ways to get around that as well. Now, I'm going to add a little bit of padding. So, I'm going to add another property and we're going to add some padding and we're just going to add 5 pixels worth of padding. That's going to help us achieve even spacing and make sure that our images don't bud up right against the edge of that, which is going to give us a little bit of framing all the way around our images. Well, now we need to tackle the spacing of the prints themselves and so what we're going to do is create a brand-new selector targeting the images inside the prints. So, you can just go ahead and select one of those if you'd like and choose a new CSS rule.
If you don't have compound selector chosen in the pull-down menu, go ahead and grab it and you should see this result, #wrapper #prints image. So, I'm going to make that less specific so that the selector just reads #prints image. So, you're targeting any image inside of an element with an ID of prints. We're going to define it in this document only and I'm going to go ahead and click OK. Now, this is going to be a very simple selector. We're just going to go to our box model and we're going to apply 5 pixels worth of margins all the way around it. I'm going to go ahead and click OK and now our prints are spacing evenly.
So, because we have placed 5 pixels worth of margin all the way around our images, we're getting consistent spacing. Now remember our 5 pixels worth of padding on the outer edge is too. So, if I do a Save All and preview that in my browser, you can see that we're getting a nice, even amount of spacing all the way around our prints. Okay, so really the only thing we have left to tackle is the footer and maybe giving a defined page size. Right now, the header element is stretching all the way across the page, for example, maybe we don't want that. So, let's tackle our footer first. I'm going to go to the #footer selector and I'm going to add a couple of more properties.
So, I'm going to close my Files panel here so I have a little bit more room. I'm going to choose Add Property and I'm going to add the background property. We're going to give this a background of #666, which is a gray, there we go. I'm going to add padding property and give it 10 pixels worth of padding so that the text doesn't bud right up against the edge of that. We're going to go ahead and give it a margin to the top, so the top margin, margin-top of 10 pixels. That's going to push the footer away from the prints and you can kind of see that occurring right here.
And then finally, we're going to change the color to white, so you can just click the color chip and change it to white and you can see our text is now reversing out. Of course, the nice thing is the padding. It is giving us enough spacing and actually driving the height of our footer. So, our footer height right now is calculated based upon the height of the text and the padding on the top and the bottom's edges. We could also center the footer text if that's what we wanted. So, I'm going to do a Save All and preview that in my browser and our layout is really coming together. The only thing that we don't have going on right now is it feels a little weird having the header stretch all the way to the edge of the page and the footer stretch all the way to the edge and having the prints sort of in here in the middle.
So, I'd like to restrict the width of these by controlling my wrapper div tag. Now, this is a very common strategy in CSS layouts. You're going to have a Div tag which can be sometimes referred to as container or wrapper or outer wrapper. And what it does is it wraps the entire content just inside the body text. We can now use this to set a defined width, background colors, background images, we can center the layout on the page this way. So, it's a really good practice and it's a practice that you'll see consistently across multiple CSS layouts.
So, what I'm going to do is add a new selector and this is just going to be #wrapper. So, if you haven't selected it beforehand, you can just select ID and type in #wrapper or, chances are, if you had it highlighted like I did, Dreamweaver will go ahead and populate that for you. Okay, I'm going to go ahead and click OK and we're going to define the width of our page at 800 pixels. So, I had to take a few things into consideration, for example, the width of my print region, I want to make sure that there is enough width for it to fit in there and for the prints to still kind of float in the middle. I'm basically determining how much spacing I want on the right and the left side of my prints now.
I'm going to go ahead and give it a margin right and so I'm going to de-select Same for all. Margin right of auto and a left margin of auto and as we know that's going to center it within the page. I'm going to apply a background color to it. So, I'm going to go to my Background and I'm going to give it a background color of #fff. That's going to be white and it's going to give our content region a white background while the page is still going to have this sort of cream background color. So, I'm going to click OK, save the file and preview that in our browser, cool! Now, this layout probably isn't going to be winning any design contest, but it does demonstrate that normal document flow can be counted on for consistent cross-browser compliant performance.
This layout is going to work in whatever browser you open it up in and you don't have to worry about any CSS hacks or tricks. That's something that any CSS designer will tell you is worth its weight in gold. So make sure that as you're going through this chapter and really focusing on laying out your pages with CSS, that you pay proper attention to normal document flow. Many times, it can take a lot of the work away from you and make the positioning that you need to do to achieve your final layout extremely minimal.
Get unlimited access to all courses for just $25/month.Become a member
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.