Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
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.
Before we start exploring the individual techniques of page layout, I want to begin with a general overview of CSS page-layout concepts. At the core of almost all page-layout techniques is the box model, so that makes reviewing it a logical starting point. Before we get into that, however, I feel like I need to mention again that this course is a continuation of my CSSL Core Concepts course. That means that in this course we are going to be building on the material that covered in the CSS: Core Concepts course. There will be some repetition, designed to reinforce important concepts, but for the most part, if you're new to CSS and are starting with this title, I actually recommend that you go back and start with the Core Concepts title and then come back to this on when you finish.
I had pretty much said the exact same thing in the Welcome movie, but I know that a lot of you guys skip those, so I wanted to mention it again here before got too far into the title. Okay, cool! Now the box model is the term that we use to describe the rules and properties surrounding the physical properties of all HTML elements on the page, and basically every single element on your web page is considered to be in a rectangular box. The properties of this box--its width, height, padding, borders and margins-- define not only the size of the element, but also how it relates to all of the elements around it.
Without these properties, we would not be able to control layout at all. So to visualize these properties a little bit better, we are going to go ahead and modify the properties of a sample element. Okay, so here I am, in Aptana Studio again. Feel free to use whatever code editor you want. It does not matter, in terms of which tool you use. You just want to be able to access and modify the code. That is it. You can even use just a basic text editor if you want. All right! I have the box-model.htm file opened, and you can find that in the Chapter_01 folder, 01_01 directory.
Just a brief overview of the structure of the page here. You can see we have some default styles in the head of the document and if I scroll down, very simple HTML file. We have two div tags, one wrapping the other one. The outer div has a class of outer applied to it, the inner one has a class of box, and inside that we have a paragraph. If we look at the styles, we have some preexisting styles for outer that are giving it a little bit of definition. We have a background color for our paragraph that's going to show you what makes it a paragraph itself, and then we have a blank selector, an empty selector here for box, and that's of course what we are going to be experimenting a little bit.
Now before we do that, let's just preview this in a browser and kind of see what it looks like. So I am going to go right up here in Aptana Studio to my configurations. Now one more Aptana Studio note, if you are using it along with me: if I click on this little pulldown menu for Configurations, I can see all of my browsers here. You may not see that list. You may have those browsers installed, but Aptana might not pre-populate that list. So if I go right here to Run Configurations, I can go down to my web browser configurations and choose a new one and then just point it to the different browser executable files that I want, and you will have to do that for each browser that you want to load in, all right! So I am going to go ahead and preview this, say, in Firefox.
I can see the blue color here is the background color of the outer div tag; the orange color is the background color of the paragraph, okay. So I am going to go back into my code, and let's start adding a few things to our box element. Now the first thing I am going to add is a background to this. And I am just going to go ahead and give this a color of #f90. Now, the background property is not considered part of the box mode; however, it does help us define this element, and you will often see it included in discussions of the box model. But in terms of whether it is a part of the box model or not, it's generally not considered that. All right! So if I save this file and go back into my browser and reload the page, I can see that it doesn't look like really anything has changed.
Well, that has everything to do with sort of the stacking order, if you will. This paragraph is sort of stacked on top of the div tag that it's inside of, which is in turn stacked on top of the div tag that it is inside of. So the color is there, but because the inner div tag doesn't really have any properties assigned to it, it's sort of shrinkwrapping to our paragraph, and that's why we don't see the background color. All right! I am going to go back into my code. Now let's go ahead and add some definition to our box. Let's go ahead and give it a width of 300 pixels and let's give it a height of 300 pixels as well.
All right, so I am going to save that, preview that in the browser, and now we can see a little of a change here. We see that the paragraph looks like it's a lot wider, and it's a lot taller, but again, we are not seeing the background color. Now the reason for that has everything to do with how block-level elements react when they are inside of another element. Now block-level sort of are elements that occupy through their own space within the normal flow of the document. Paragraphs are block-level elements, div tags are, headings, or things like that. Well, a block-level element is told basically that if it doesn't have a defined width and height, it should just go ahead and expand to fill the container element.
So in this case, it's expanding to fill the div tag that we just made a little bit larger. Okay! The next thing we want to do is let's throw some padding on here. So I am going to come into our box properties. I am just going to type in padding, and I am going to type in 25px. Now this is padding's shorthand notation. We could also say padding-top, padding- bottom, padding-right, padding-left, that sort of thing. By saying just padding and passing one value into it, we are going to get the same amount of padding all the way around our element. So remember, padding is the space inside the element. It separates the content from the border edge of the element itself, so we should expect to see a little bit of cushion between the paragraph now and the edges of this inner box.
So we save this and preview it, and indeed that is exactly what we get. Notice that the color that we defined for the inner box is a slightly darker orange. So what we are seeing on the inside, that's the paragraph's dimensions, and now on the outside, we're seeing the box for the actual box dimensions for the inner box. Now one thing to note here is that even though we've defined its width and height at 300px by 300px, the 25px that we've just added all way around it have actually added to the overall width and height.
So now we have 25px on this side, 300px here, and then 25px on this side. So now the total width of our element is now 350px. Okay, so what about borders? Borders are also considered part of the box model, so right after padding, I am going to type in border and then we are going to do 2px solid black. So again, this is shorthand notation. This is us telling the user agent that we want a border all the way around of our element. We want that border to be solid, we want the color to be black, and we want it to be 2px in width.
So again, if I save this, preview it in my browser, I can see that indeed our inner box now has that 2-pixel black border. If you're wondering, yes, that is cumulative as well. So we've added an additional 2 pixels on either side of this, so now instead of being just 350 pixels wide and 350 pixels tall, it is now 354 pixels wide and 354 pixels tall. All right! One last property to take a look at and that is margins. So if I come back into my code and I go down to the next line and type in margin, and we'll just go ahead and do 25 pixels of margin there as well.
Just like padding, we are using shorthand notation here for margin. Now margin doesn't always calculate to the total width of an element. It basically controls the spacing from one element to another. And almost always it does calculate in terms of the space it's going to take up within the layout, but there are instances, for example, when it's inside of a container that's too small to contain it, where sometimes the margins will basically be ignored, or in the case of a vertical margin, sometimes that is going to collapse. That actually is such a big deal that we are going to discuss that in a separate movie in just a little bit, when we talk about calculating element dimensions. All right! So I am going to go ahead and save that, preview that in the browser. And it doesn't look like a whole lot has changed, except for it looks like our box has shifted position.
Essentially, what's happening here is now this margin is pushing the top of the box away from the top edge of the containing element, and it's pushing the edges away. So basically, it's sort of creating its own space inside the containing element at this point. Now what about total width and height? Well, remember we have 300 pixels here; we have 25 here and 25 here, so it's 350. If you take a look at the margins, that's another 25 on either side, so that's 400 pixels wide. But the border, remember 2 pixels and 2 pixels, so that'd be 404 pixels wide, or at least it's taking up 404 pixels' worth of space.
It may not be that physically wide, but it's at least taking up that amount of space. So just remember that width, height, padding, borders, and margin, all those properties combine to make up the box model. Now when we plan our layouts, you are going to need to understand how all those properties combine to create the overall width and the height of an element. Like I had mentioned just a second ago, that is so important we are going to take a closer look at calculating element dimensions in our next movie.
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.