Divisions or “div” are a ‘block level element’, meaning they have the “block” attribute already inherited. Divs are used to display content in a block format much like tables but also used to define sections of a web page.
Divs are the best way to layout information on a web page because of the properties that can be applied using the simplest and cleanliest code. It is always better to use divs over tables whenever possible.
The best way to organize a web page using divs is to create specific sections for each part of the page. For example: a header, navigation, content and footer. For each of the different sections, an ID can be used to identify and style each div. It is always better to have each section or div inside one main div, normally this div is called the “container”, this “container” div will allow for more options, which will be described later.
Below is how to organize the container and its inner sections or divs.
<div id="header">Header Content</div>
<div id="navigation">Navigation Content</div>
<div id="content">Main Content</div>
<div id="footer">Footer Content</div>
Below is a picture of a box model of the four sections inside the container, which also shows the style that applied to each section.
The example shows the navigation being used on the left hand side. You can set the “navigation” and “content” divs side-by-side using the “float” property. The “navigation” div will have the “float” property along with a specified width. The “content” div will have the “margin-left” property for the same value as the navigation width. Using the same value will allow the content to only flow to the right of the navigation.
Below is an example of the style applied to each section:
By using a “container” div, you can set the width of the overall content. By not setting a “width” property, the content will fill the browser window-no matter what size. To set a maximum width for the overall content, you would do so by adding a “width” property to the container. The default alignment for the container is left, but you can also center the “container” div. You can see how to center the div below.
Unlike tables, divs do not have the “align” property, so you cannot center a div in the same way. The way to center a div element is to use the margin property. Below is how to absolutely center a div:
The value to use for the margin properties is “auto”. This will automatically calculate the distance from each side, left and right, and position the div in the center.
<div onclick="location.href='http://www.example.com';" style="cursor:pointer;"></div>
The only property that is applied, via css, is the “cursor” property, this is so the visitor knows the div is a link.