A <div>, quite simply and literally, is a box.
In its default state, a div is transparent and expands/contracts to fit it's content.
Div's have many uses and a myriad of style attributes can be applied to them. For example, all of the code and comment boxes that you've encountered in these tutorials are done with div's that take their attributes from my stylesheet. This is what the CSS looks like for the code boxes you've been seeing...
When I implement them it looks something like this...
<div class="somecode">
<p>
The code that you see is in here.
</p>
</div>
Notice the use of the "nesting" thing again in the second set of declarations (.somecode p). The paragraph tag is only effected by those style rules when it's inside of the CSS class "somecode". Any paragraphs outside of the <div> are not affected. I had a few reasons for why I did it that way, but they aren't important to know at this point.
Don't get intimidated by the many declarations. If you look at them, most of them are pretty self-explanatory and all of them will be explained in other sections. To the contrary, you should be excited in having that much control! Once you learn what you can do, you can make the page look EXACTLY like you want instead of settling for the closest thing that the WYSIWYG editor will give you!
While you could apply your style declarations to the div selector like this...
div {border-width: 1px;}, every time the browser encountered <div> ALL of them would behave the same way. Chances are that you'll have several different div's, and so you'll want to create a separate class for each one.
Let's create an outlined box for one of the paragraphs on the first page (index.html). But first, we need to create a link back to index.html from pagetwo.html...
Save/refresh, then click the link to go back to your home page. Then, in your style sheet, add this new class...
Now open index.html with Notepad so that you can edit it. Implement the new class "redborderwhtbg" by surrounding the second paragraph with div tags like this...
Save/refresh. Notice that all div's, by default, are 100% of the width of whatever they're contained in (in this case the <body>) unless another width is specified. The height defaults to the height of the div's contents (plus some padding) unless another height is specified.
You may be thinking that if you wanted to, you could have just applied your new class to the second paragraph by adding <p class="redborderwhtbg"> and you'd be 100% correct, because paragraphs are also "box" elements. But suppose you wanted to have multiple paragraphs surrounded by the red border? Putting them inside the <div></div> tags is the best solution.
As you'll learn in more advanced tutorials, you can do all sorts of things to div's including setting positions for them on the page, layering them on top of each other, nesting them inside of each other, and on, and on. But, for right now I'm going to show you a more traditional (and controversial) way to get boxes on your page...
Basic HTML Tables Tutorial... →