Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we've gotten our page structure where we need it, maybe we'll apply some basic formatting to our page to begin our two-column layout. The first thing we are going to do is center the layout and apply a fixed width to it. That is something that's become rather standard today in CSS layout. The reason is monitor width and monitor resolution is so varied today from very small screens to very large screens with wide aspect ratios that having content aligned to the left or the right of a monitor isn't really ideal. So a lot of people are centering their content and from there they might use a fixed width or they might use a flexible layout and then we're going to do a fixed width layout. So the first thing that we're going to do is go ahead and center our content on the page and then we'll position them. So I am going to go to my CSS Styles panel because we need to create a new external style sheet and it's best to store any sort of global styles for a whole site in an external file, so we'll go ahead and do that now. And you can actually do that through the creation of a new rule, and if you don't have a style sheet already defined, Dreamweaver at any time allows you to create one as you define the rule. So, don't feel like you have to create them as a separate page. So we can create a new external style sheet just by defining a new rule. So I am going to go down to my New CSS Rule and click on that and that will bring up our New CSS Rule dialog box. And to review the selector types, class refers to classes and tags refer to any element selectors like paragraph or heading 1s. And the Advanced radio button basically is saying that you can just type in anything that you'd like, so ID selectors, pseudo-class selectors, descendant selectors, that type of thing. So we are going to use the Advanced here and we're going to do a selector that you may not be familiar with. I am going to go ahead and just type in a star or asterisk symbol, and we're going to say New Style Sheet File, so that it creates a new style sheet file. This is a wildcard selector and you can use it in conjunction with other selector. So you could say *h1 for example and you'd say every element outside of an h1 so you can use it as a descendant selector. A lot of people use *html to target Internet Explorer only browsers.
We're wanting to target every element on the page, so we're just saying star. I'll go ahead and click OK and we're going to save this in our Unit 3, Starting _css directory and we want to go ahead and name this layout.css. So it not only creates the external style sheet for us but it brings up the CSS Rule Definition dialog box. I am going to go to my Box properties and I am going to set the Padding to 0, Same For All, and Margin to 0, Same For All. Now because we are using a wildcard selector for this, the star selector, that basically means that every element on our page is going to be stripped of its margin and its padding. Now that's a really severe thing to do because our paragraphs are going to lose all their margins, their default margins, our headings are going to lose all that. But what that allows us to do is sort of strip all that bare and then have total control over all the elements on a page as we style them.
So I am going to go ahead and click OK. And the other thing that it does for us is it also makes sure that we are not going to be dealing with the default margins and padding differences that different browsers have. Safari and Firefox, Internet Explorer, Opera, they all have their own default margins and padding. Well. We have just basically told all of them ignore your own settings and use mine from here on out. So I'll go ahead and expand layout.css and the only selector in there right now is that little star, and if you click on that you can see there is our margin and there is our padding. It would be tempting to go ahead and keep creating rules this way, but once you get used to doing it, hand-coding these rules is actually a lot faster because Dreamweaver gives us coding hints for CSS, the same way it does when we are coding our XHTML.
So we're going to take advantage of that and maybe speed up the process of writing some of these selectors. So if you'll notice because we created a new cascading style sheet file, Dreamweaver has automatically opened that for you right beside your index_start file. So I'll click on the layout.css tab and you can see that we just have an encoding character set that Dreamweaver is passing along and we have our star selector. Now one thing that I like to do in any external style sheet is comment out that style sheet and talk about what it is that we are doing. Now remember we can go down to our Coding toolbar and we can just tell it, hey, I need a new comment. For a CSS, you use the /* */ multiline comment, and I'll go ahead and click on that to insert a comment line.
And generally I use comments to separate out areas of content. So I am just going to draw-- click a couple of dashes there and type in basic styling. And that helps identify that area of content. So you also want to write notes to yourself about what this particular selector is doing, if you think you need to remind yourself about later on. Well, I am going to go down to the line below our existing selector and if you want to put multiple lines in between them, you can, that sort of thing. Remember the concept that we learned about earlier, inheritance. I can go ahead and apply a lot of formatting to the body tag and know that everything on the page will inherit the style. So I am just going to type in body for the selector and open up one of my French braces, and I am going to go down the next line and I generally tab over so that I have the same indentation that they have. And I'll type in background and I can just use the coding that comes up to assign a background color. And the background color I am going to assign, I'll use the hash mark here, is going to be 6f5044 and we type in a semicolon after each rule and I'll hit Enter to go down to the next line. And I am going to type in text-align and I am going to set that property to center and type in another semicolon. Now that has everything to do with centering the content on the page and you might think, wow, that's a really bad way to center the content on the page. But we're really making up for a problem that Internet Explorer has and I'll talk more about that as we continue to write these selectors. So let's go down to the next line.
I am going to type in font-family and I notice that I get a pulldown menu of all these sort of preset font-families, and if you've ever wondered why you might define more than one font, well, we're relying on a client machine to show these fonts so if somebody doesn't have Georgia for example, it looks for the next font in the list. And speaking of that I'm just going to type in a G to jump down to Georgia and I'll hit Enter. And then I'll type in another semicolon. I'll hit Return and from here I'll do font-size and I'll type in 85%. I'll hit Return and then end my curly brace. Now by setting your font-size to a percentage you're telling any device whether it's a browser or hand-held device or whatever device it may be, you are telling that I want to display my fonts inside the body tag 85% of what your normal display size is or whatever its display size is currently set to. That allows you to be very flexible with your design and not have to worry so much about is it 16 pixels tall or 18 pixels tall.
I am just saying give me 85% of your normal font-size. The next thing I need to do in order to center my content is to create a Wrapper Selector and I am going to use #wrapper as the selector. So I am using the wrapper and open the curly brace. And on the next line I am going to type in position and I am going to set the value equal to relative. Now the reason we're using position:relative on the wrapper div tag is not so that we can move it over in the center by using relative positioning. It's actually more to control the elements inside of it. Because if you use absolute positioning on a nested element, it's relative to the top parent element that has a positioning attribute. In this case, it will be the wrapper tag. So if I wanted to take say the content area and use absolute positioning to move it around, it would go up and find the wrapper div tag and it would say oh! the wrapper div tag has a positioning element. Now the 00 position of the upper left-hand corner of the wrapper div tag would be used to position instead of the actual web page itself.
So we're just sort of resetting that 00 point if you will to the wrapper div tag. So we'll go down the next line and I am going to go ahead and give it a defined width. I am going to define a width of 900 pixels and since we are doing a fixed layout we definitely want to give it a specific size here; we could have given that a percentage too if we wanted it to be flexible. Now, I'll set my margins and here's where we do our centering. I am actually going to set margin to 0 and then auto. So for the first time here we are doing some shorthand. When you are doing margins and padding, you can pass along shorthand notation. By saying 0, the first value is the top and the left margin.
The second value, which is auto, is left and right. So by saying 0, we are aligning the wrapper up to the top edge of the page and by saying auto we're saying we've got 900 pixels for our wrapper and we want all the remaining pixels left over, no matter what size the monitor is, we want to distribute half of that on the left and half of that on the right because we're just saying automatically determine the margins there. That centers our content. So you might wonder, okay, if that works then why did you have to do text-align on the body and why are we now doing a text-align property left on the wrapper? Well, it deals with Internet Explorer and Internet Explorer doesn't really always calculate those auto margins correctly. So by using text-align center, Internet Explorer will go ahead and center everything in the middle of the page. Then by applying text-align left to the wrapper, everything inside the wrapper gets left aligned again so it sort of fixes that. So we'll go down to the next line and we'll type in a background-color for our wrapper. And our background-color is going to be just white, so #fff. So we're using shorthand notation there for the color.
And we'll close out the curly brace here. And the last thing we want to do for this sort of basic styling is to go ahead and style a clear id and for that we want to choose clear:both. This is a pretty common technique too when you are doing a float-based layout. You just go ahead and create either an id or class and you name it clear and then it's value is clear:both. And now anytime I need to clear an element I can go ahead and apply this id or apply this class or doing it via a class style and it will go ahead and clear my float.
Now that we've finished writing the styles that are going to center our content, we'll go back over to our Index page and we'll preview those. So I am going to click back on the tab for index_start now. I am on a pretty small screen resolution. So because of that I can't really see; it's 900 pixels wide. So I can't really see the edges. So I am going to preview that on my browsers to see them. But for those of you who are on a much higher screen resolution, you're probably seeing the content centered on the page and so the brown background color around it. So I am going to go ahead and preview that in the browser. Notice that Dreamweaver is giving us the message that some of the files necessary to preview this in the browser haven't been saved. So I am going to go ahead and click Yes. I could have prevented that by saving all of our files prior to previewing it. Now we'll preview that in our browser. And when it comes up, notice that we have 900 pixel wide content centered in the middle of the page and we have had that brown background color and let me go ahead and center this out. Now because we went ahead and zeroed out the margins and the padding for everything, notice that all of our content is butting up right against the edge of the container wrapper div tag and all of our content is kind of stacked right on top of each other. So even our paragraphs and our headings are really not pushing apart from each other at all anymore. So we've centered our content.
Now we need to focus on styling the content inside the nested containers.
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.