Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.
In our last movie, we calculated the percentage values that we are going to need to create a two-column fluid layout. In this movie, we are going to put those values into practice by applying them to the appropriate elements. So I have the fluid.htm file open from the 06_03 directory, and if you remember from our fixed layout in the last chapter, we have a page structure that is pretty much almost exactly the same as we had before. I just want to preview this page in a browser really quickly to show you something about default styling. All right! So I have opened up our page in the browser, and you can see we don't really have any margins or padding goings on within the elements themselves. And as we look through this, obviously the elements are just sort of stacking one on top of each other, so we still need to position everything.
But as I resize this browser, I want you to notice something about layouts. By default, layouts are fluid. If you have a block-level element, that block-level element, if a width is not defined on it, it will go out and stretch to fill the parent container, meaning its width is set by default to 100%. Even if you don't do anything at all, your layout is more or less fluid. So I am going to go back into our code, and the first thing that I'm going to do is I'm going to scroll down into my styles to the body selector. And we need to define a width for this.
Now, unlike in the previous chapter when we were doing fixed layouts, instead of applying a fixed-target resolution to this, I'm simply just going to use a percentage value for the width. And in this case, I'm going to go ahead and give it a width of 80%. How wide you make the wrapper container, in this case the body selector, is entirely up to you. In some cases people like to do 90%, 95%, if you don't want a whole lot of space on the edges. It really boils down to how much space you want on the right and left side of this. If you're centering this at 80%, if I save my file and go back to the browser and refresh that, you can see it gives me a nice amount of space over here on either side, and based on the available browser space, it's going to give you nice column widths as well. Okay.
I am going to keep scrolling down, and one of the things that most people forget to do in fluid layouts is set padding. It happens all the time. You concentrate so much on the columns of your layout and the various elements that you sort of forget that every single one of those elements that needs to have padding that's going to line elements up, should have the same padding. So this header, we need to go in and change this padding here so that it's getting a percentage for its left padding. So where right now I have 32 pixels--and remember that was our old fixed layout-- I am going to highlight this and I am going to change that left padding to 2% and save that.
So now it has 32 pixels' worth of top padding and then 2% for the left. And if I go out to the browser and refresh that, you can see the spacing here in the headline up in the header changed a little bit. Okay. Now, we are going to go down and do our columns. Notice we have column1 floating to the left. We have column2 floating to the right. Now, they don't have any defined widths right now, so we're really not getting the effect we want to with those floats. So with column1, I am going to go down in that selector, I am going to give it a width of 60%.
Now, we remember those from our previous exercise. If you still have your notes you have taken, if you were sketching that in a sketchpad, you should be able to see those percentages and just plug those right in. Underneath that I am going to do padding to the left and again, I am going to do a padding-left of 2%, and will save that. Of course that means I am going to go down to column2 and do something very similar. In this case, column2's width is going to be 34% and its padding, this time to the right and not the left, is also going to be 2%. You will notice because of the layout method that I use of floating one to the left and floating the other one to the right, I don't need to specify any type of margins between the two of them. Their widths will create an absence of space in between them, which gives me the spacing, and I don't have to actually declare that, which, for me, is nicer. Okay.
I am going to save this. Once again, I will go out to the browser, refresh the page, and there is our two-column layout. Again, if I resize the browser, you can see that this is now flexible. I am getting a flexible amount of space and a flexible amount of padding for both of those guys. Now, down a little bit lower, we still have these two elements, these two interior elements. So we're going to go in and set those percentage values on the interior elements that we were calculating in the last exercise as well. So going back into the code, I can scroll down and I can find those two news items right there.
So in the main news one, what I am going to do here is set its width to 44.5%. Remember, they are both going to be exactly the same. So I am going to give them both the same width. And then for padding, well, I want to set padding all the way around, so I am just going to do 1.75% for the padding. I also need to set some spacing between these two of them, because I'm not floating one to left and one to the right. So I need to do margin-right on these, and the margin between them, if you remember from the last exercise, is going to be 4%. And then I have this other selector right down below that that is stripping the margin-right value from the second news, so we don't get an extra bit of spacing there on the right-hand side of that.
So if I save this, go back out to the browser and refresh my page, now we get our interior spacing. And what I really want you to focus on is take a look at the space between these two columns, these two elements, and this element. And they're very close. Now, we had to give them all different percentage values. This is 2%, this is 4% based on where it's located, and this of course we are letting the actual columns create that space. But we are seeing sort of a consistent spacing between them because we took the time to plan that out and really calculate what those various percentages are going to be based upon their parent elements.
So there you have it. It is a basic two-column fluid layout. Now, your designs are probably, over time, going to be a lot more complex than this, but as long as you remember the rules behind calculating percentage values, and especially how it pertains to margins and padding, you should be fine. Now, next up, we are going to explore how page assets like images and other assets can affect fluid layouts and how to make those assets flexible.
There are currently no FAQs about CSS: Page Layouts.
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.