Computers and Technology
CSS Lesson 6: Intro to Divs
Alright, alright. So I skipped all that fuss with horizontal navbars but that stuff is easy so I’ll create a short article on it later.
Let’s start with the <div> tag. It’s what I’ll be covering. Making use of div’s with CSS. You’ll need to know the following before we start the lesson:
- CSS Lesson 2
- CSS Lesson 3
- as well as basic HTML knowledge
If you don’t how to add CSS to your website, check out my CSS Intro tutorial and CSS Lesson 1.
What are “divs”?
A div is basically a section, a chunk of HTML that you can apply the same CSS to. It works similarly to <span> but on larger areas and more complicated HTML. Also, it supports nesting.
A single DIVision can be used, for example, to make a header, another for the navbar. Divs are usually defined using the CSS properties: margin, padding, border, height, width, and float.
So what can you do with Divs? Well, that’s the best part. Using divs, you can create multi column websites, you can make the website aligned in the center, instead to the side. You can create rules for large chunks of stuff including text and pictures.
Divs largely replaced the use of tables for page layout, which is great.
Recognizing Divs
Now, let’s look at how divs should be structured, I’ll be re-making a template I did in PSD a while ago, you can find in Exan’s Corner.
Right as I’m looking at it, I can already distinguish four different sections in the template. The first is the header that contains the words “A Chiropractic Group”. The second section is the nav bar and the third is the content on the right. The fourth would be the footer. But when you’re thinking of divs, you have to think differently. This page will contain at least five different divs, if that. I’d probably cut it up to six or seven different ones. Why? Well let me show you.
NOTE: These are only examples, I am not actually recreating the template above.
Way too confusing, eh? Haha, there are just so many sections. Let me break it down for you and explain why it is this way. Also note that I did not add the “footer” div which would be number 7 on the bottom. It works the same way as the top div.
1. Header – Just a standard div that you’ll put at the beginning of your <div> tag. It will always be on top unless you set another div for absolute position. First thing you want to do while setting this division up is giving it an id or a class like this: <div id=”header”> </div>. Great thing about divs is that you don’t NEED to put anything in there so let’s set up the rest of the properties. Second step is to set up the width and height using CSS:
<div id="header"></div>
#header {
width: 100%;
height: 150px;
margin: 0;
background: url(header.png) repeat-x;
}
This is basically how we set up all of the divs. Using the “repeat-x”, we don’t need to slice large portions of the header (thus adding to the size of the images needed to be downloaded) and we create a continuous header that will show up the same on all web-browsers/monitors. You’ll need to separately define the classes and ids for the logo as well as the text inside the header to match the PSD template, you might also have to create another separate div for the logo and one for the text, just in case.
2. Wrapper – I’m sure you’ve encountered that term before. Whenever you need two or more divs to show up next to each other, you need to “wrap” them in another div. In this case, it’ll be the content area, the content footer, and the sidebar that will be wrapped together. With the wrapper, we can also set it up so that the content shows up in the middle using the property “margin: 0 auto;” which will automatically center the content. Here, though, we just need to set up the background and the width of the wrapper using CSS. Once again, assign a class or an id to the div.
<div id="wrapper">
<div id="sidebar"></div>
<div id="contentwrap></div>
</div>
#wrapper {
width: 900px;
height: 1000px;
}
It is also possible to set the height to “auto” but if we’re to do that, let’s do that as the last thing in the process.
3. Sidebar – As you can see, I created a div for the sidebar. Now, if you wanted to create a dynamic sidebar, ie one that changes, and you wish to still use the rounded corners, you will need to include three more divs in the sidebar division. This way, the sidebar itself will serve as another wrapper inside a wrapper.
You’d have the top div where you would use the first five or six pixels to create the corners, the bottom div for the same purpose. Also, use only PNG because JPEG does not support transparency. The middle would use a repeating background again. Here’s how you’d set it up:
<div id="sidebar">
<div id="sidebartop"></div>
<div id="sidebarmiddle"></div>
<div id="sidebarbottom"></div>
<div class="clear"></div>
</div>
#sidebar {
height: 600px;
width: 200px;
float: left;
}
#sidebartop{
height: 10px;
background: url(topside.png) no-repeat;
width: auto;
}
#sidebarmiddle {
height: auto;
background: url(sidebar.png) repeat-y;
width: auto;
}
#sidebarbottom {
height: 10px;
background: url(bottomside.png) no-repeat;
width: auto;
}
Now you’ve set up the sidebar. Do you get the idea yet? All you do with divs is set up portions of the page. The problem with this is that the sidebar height will be based on the content inside. For template-making purposes, set the height and change the CSS afterward if needed.
TIP: Create a special class called “clear” with these attributes: .clear { clear: both; height: 1px; } and add it at the end of every div that has the property “float”
4. Content Wrap: Here’s another tricky part. You see how the content has two different sections? One above with articles and one below with a pseudo footer? They’re aligned together so that means, we need another wrapper. This one will how 5. Content and 6. Pseudo-footer. The set up is easy.
<div id="contentwrap">
<div id="content"></div>
<div id="psfooter"></div>
<div class="clear"></div>
</div>
#contentwrap {
width: 700px;
height: auto;
float:left;
}
#content {
width: 698px;
height: auto;
}
#psfooter {
width: 698px;
height: auto;
}
7. The Footer – This is the easiest part and probably the most common on all websites.
<div id="footer"></div>
#footer {
height: 20px;
width: auto;
margin: 0 auto;
text-align: center;
}
That’s it! This is how you create a website out of a PSD template, basically. And this is how you use divs. It’s all about setting up the height, width, padding, margins. Really, there is no magic to it.
Altogether, here’s the basic layout in divs:
<body> <div id="header"></div> <div id="wrapper"> <div id="sidebar"> <div id="sidebartop"></div> <div id="sidebarmiddle"></div> <div id="sidebarbottom"></div> <div class="clear"></div> </div> <div id="contentwrap"> <div id="content"></div> <div id="psfooter"></div> <div class="clear"></div> </div> </div> <div id="footer"></div> </body>
Using the CSS, you can define the rest of the properties as shown at the beginning (ie. width, height, float, margins, background, border etc).
In my next tutorial, I’ll be covering the correct way to slice a PSD template for HTML/CSS use. Remember to use the same measurements in photoshop as in CSS and HTML!
Overview
A <div> or division, is a section of a webpage that shares the same properties. They’re easily recognizable, there is a div for a sidebar, for the content, the header, and the footer.
If you want multiple divs next to each other, create a “wrapper” div that will group the multiple divs together. Next, make the divs “float:left” to ensure they’ll be next to each other. After every “float”ing div, employ the “clear” div with the properties “clear:both; height: 1px”.
If you want to have rounded corners on your webpage and still retain a variable height of the division, create three separate divs nested in the original. One for the top, one for the middle, and one for the bottom. The top and bottom will contain the part of the background with the rounded corners. USE PNG!
<div>s may look confusing at first but once you start working with them, you’ll appreciate the options they provide you with.
See you next time!
| Print article | This entry was posted by Admin on October 25, 2009 at 17:36, and is filed under HTML/CSS, Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







about 2 years ago
Easy and simple way for designer, to design their themplate.
about 2 years ago
Hey man,
Thanks for the tutorial. I’m just starting to use CSS. I’ve been doing pretty well, but I can’t seem to get my sidebar div to align correctly next my content div. What the best and cleanest way to do that?
about 2 years ago
The main issue when aligning the divs is the CSS used. Try different widths. Let’s say you have a wrapper that’s 600px wide. The content and sidebar should amount up to about 598-6 px. So, you can have a content div 398 px and sidebar 200px. That should work. Don’t forget to clear both divs and float them both to one side.
If you have any further trouble, e-mail me and I’ll explain the process more thoroughly. alexander@davepcguy.com