Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
- Reviewing the box model
- Calculating em and percentage values
- Controlling how elements display
- Creating fixed, fluid, and responsive layouts
- Structuring content with HTML5
- Floating elements
- Using relative, absolute, or fixed positioning
- Defining column spacing
- Creating grid-based assets and layouts
- Considering mobile-design-specific issues
- Working with multi-column text
- Enhancing page design CSS Sprites
Skill Level Beginner
Now that we've gotten the hard part out of the way--and trust me, the planning is definitely the hard part-- it's time to apply our grid to our layout using CSS. While we'll be using techniques like floats that we've already covered in previous chapters, we're going to see how our planning really pays off in making our CSS easier to write and how our typographic styles will tie in to our layout. So I've got the grid.htm file open here, and you can find that in the 05_04 directory.
And there's just a couple of things that I need to do right off the bat. One is just sort of give you an overview of the structure of the page. If I scroll down past the styles here, you can see that we have a header, followed by a section with a class of col1. That has a lot of text in it. And as I mentioned before, this layout is very text focused. After that we have an aside with a class of col2, and at the very bottom we have a footer. So basic structure. And of course if I previewed the page in the browser right now, you can see design-wise, it is no great shakes, but the structure is there, and we know the CSS that we need to write for this. Okay.
I am going to scroll back up into my styles, and I've got on about line 28 here a little comment that says layout, and we are going to write the layout styles directly underneath that. Before we do that, we're going to modify the body tag, because remember, step number 1 in our planning process was to define a target resolution for our page and then come up with a value for that. So in the body selector what I am going to do is I am going to assign a width to the page of 960 pixels. Remember, that's our target size.
And then below that I am going to go ahead and center this content on the page. I am going to give it a margin of 0 for top, auto for left and right, and 2ems of margin on the bottom. Now, the only reason I am doing 2ems on the bottom is I kind of always like my pages to sort of float above the bottom edge instead of be snugged to it, in my opinion. It lets the reader know, hey, this is it, the scene and the content, nothing is being clipped off. So I am going to go down into our layout, and I am going to start coding. Now, as I mentioned before, we already know the measurements that we are going to be using, and I am going to flip back to the planning document that we were using in our previous movies, and here are all the measurements that we need for the elements that we are going to be styling on our page.
So we'll come back here, grab the measurements, and go back into the code. So the first thing we are going to style is our header, so I am just going to write a selector for our header. And on the first line here I am going to give it a background color. Now, this is a decision that was made off-site, so we didn't see this one being planned out. If you don't like the colors I am using, please feel free to substitute your own. So rgb value of 76, 67, 65, and then remember the semicolon. Then I am going to go down to the next line and I am going to give this a margin-bottom.
Now, that's going to separate the header from the content beneath it, and I am going to go ahead and give that a margin-bottom of 16 pixels. Since we are basing this off font- size, I could certainly give it a margin-bottom 1em, but again, I am just sort of reinforcing the fact that this is based off of 16 pixels, in terms of our grid. Now, for height, I am going to give it 96 pixels. And if you were thinking to yourself, hey, wait a minute, in the planning document, didn't you say 128? well, you've got to remember that padding plus height and/or width equals total height and total width.
So back in our CSS, 96 is for the height, and that tells us that we're also going to add some padding. And the padding for this is going to be 32 pixels top padding, and then 0 pixels for the right, 0 pixels for the bottom, 32 pixels of padding on the left-hand side. So the 32 and the 96 give us the 128 pixels total height that we need, and then the 32 pixels of padding on the left- hand side matches the 32 pixels' worth of padding that we want everywhere. So now we've defined our header and we're going to move on to our next element, which is our first column.
So I am going to write a selector for col1. Now, that's the class name that we used for this column. Since we only really have a section and an aside, I could use those elements that they are inside of as well, but since we have those class names available to us, I am going to take advantage of them. So this is col1, and I am going to float this to the left. Now, just like we learned in our last chapter, this is going to create a two-column layout, one column on the left-hand side, another column on our right-hand side. So this one is going to be floated to the left. I am going to give it a padding to the left of, you guessed it, 32 pixels and, again, if I am looking for our width, I can go back to the mockup and it tells me 512 pixels.
So on the next line I am just going to type in width and I am going to do 512 pixels and save that. I am going to scroll down a little bit more so you guys can see this a little bit better, and then we are going to go ahead and create the styles for our second column. So that one is .col2. That's a class selector for the second column. This one is going to be floated to the right. So again, this is going to help us create our two-column layout: one is going to be on the left-hand side, one is going to be on the right-hand side. Now, if I go back to our mockup, I can see that I need 32 pixels of padding on the right here and I need 352 pixels for its width.
So if I go back into my code, I go down to the next line, and I'll just go ahead and give it that width of 352 pixels. I'll give it my padding-right of 32 pixels, and then I am just going to do a couple of things here for typography. I am going to give it a font-size of 90%. I want the text in the sidebar of the aside there to be a little bit smaller than our body copy text. And I'm going to give it a line-height of 1.6, so there's a good amount of vertical separation between the text there. All right! I am going to go ahead and save that, and then we have one more element left to do and that is the footer.
So I am going to write a selector for my footer. I am going to scroll a little bit so you guys can see this a little bit better. Again, for our footer, we'll do a background color. Again, feel free to change this if you don't like the color that we are working with here. I am just going to do rgb. The value for this is going to be 100, 98, 102. Don't forget the semicolon. And then on the next line, we are going to go ahead and give that a height of 80 pixels. Now, I know we didn't put this in the mockup but, again, that 80 pixels, the height there is based off of that 16 pixels.
We don't have any content in our footer right now. Later on, if we put content in it, we would probably add a little bit of padding, and then we would have to adjust that height value. And then, because we're doing a two- column layout with floats, what do we need to do to the footer that we learned in the last chapter? That's exactly right. We need to go ahead and clear that footer, so I am just going to do clear: both and save that. So I am going to go ahead and preview this in my browser again, and when I refresh the page, I can see my two-column layout. The layout looks okay, but it doesn't look particularly tied together, does it? And the reason for that is that even though these elements are based off of that 16-pixel grid that we were working with, the text right now isn't.
So there's really nothing to sort of tie it together visually, and even though the columns are spaced the way we want them to, we're really not getting that sensation that this is all tied together. So it's not just about defining element widths; you really have to think about the layout as a whole. I am going to go back into my code and I am going to uncomment out these typographic styles. So there's a little beginning comment right beside the h1 there, and then if I scroll down, there's an ending comment at the bottom of it. Now, if I save these and go back into my browser and refresh, you can see that now that looks a lot better, a lot more put together.
It looks a lot more tied together, and that's because all of the typographic styles that I just enabled are also based off of that sort of 1em 16-pixel grid, so everything sort of ties together. Now, if you want to examine that in more detail, just go back into the code and go through all the styles that I created here. There's really not that many. There are just four styles. But go through, take a look at their font-size, take a look at the margins that we're using here, take a look at line-height and the spacing, run these calculations and sort of figure out how they tie in to the same 16-pixel grid.
So the majority of our layout is now finished, and let me ask you: How much easier is it to have that out of the way during the planning stages, so that you just come in here and you just plug in your values and you know your layout is going to work? That's why it's so important to spend the time that you need in the planning stage to make sure that everything with your layout is working as you intend it to within that grid that you defined. So for the most part our layout is done, but even though our layout is primarily focused on text, it doesn't mean that we won't occasionally have images or other types of assets that we're going to need to add to this page context.
So in our next movie, we're going to take a look at how to make sure our page assets are working with our grid that we've established as well.