Menu
HTML DIV Basics

An html div is a block-level element that defines a horizontal section of a webpage. A block level element has an embedded carriage return before and after the element. A <div> is similar to a paragraph <p> element, except that paragraph elements have a semantic meaning that they should be used to create blocks of text, while a div is a generic box that is commonly used for layout purposes.

In the code shown below, I've defined two paragraphs above two divs. Note that the default margin for paragraphs is the height of the font, while the default margin for the divs is zero.

<p style="border-style:solid;">Being able to factor numbers is an important skill,
one that will be necessary later on to work with fractions and polynomials.</p>

<p style="border-style:solid;">Being able to factor numbers is an important skill,
one that will be necessary later on to work with fractions and polynomials.</p>

<div style="border-style:solid;">Being able to factor numbers is an important skill,
one that will be necessary later on to work with fractions and polynomials.</div>

<div style="border-style:solid;">Being able to factor numbers is an important skill,
one that will be necessary later on to work with fractions and polynomials.</div>

Being able to factor numbers is an important skill, one that will be necessary later on to work with fractions and polynomials.

Being able to factor numbers is an important skill, one that will be necessary later on to work with fractions and polynomials.

Being able to factor numbers is an important skill, one that will be necessary later on to work with fractions and polynomials.
Being able to factor numbers is an important skill, one that will be necessary later on to work with fractions and polynomials.

The div element has few html attributes. The align attribute specifies the alignment of the content inside a div element. Possible values are; left, right, center, or justify. However, this attribute is deprecated and styles should be used instead.

Divs make heavy use of styles, so Without knowledge of CSS, you'll have have a difficult time with all but the most basic use of divs. Even with good knowledge of CSS, because box model styles are inconsistent between browsers and browser versions, you'll have a difficult time using divs for page layout. It requires many work-arounds to get them to display constantly.

However, using styles, a div can be given a specific width and height and changed to an inline box which makes it the best element to use for complex webpage layouts. Especially useful for webpage layout is the ability to nest divs as in the code shown below.

<div style="border-style:solid; border-color:red; width:450px; padding:4px;">
<div style="border-style:solid; border-color:blue; width:200px; display:inline;">
Nested block</div>
<div style="border-style:solid; border-color:green; width:200px; display:inline;">
Nested block</div>
</div>
Nested block
Nested block

Another important styles concept to understand is that vertical margins collapse between adjacent elements. In other words, for adjacent vertical block-level elements, only the margin of the element with the largest margin value will be displayed. The margin of the element with the smaller margin value will be collapsed to zero. In order to become more skilled in the use of divs you should study the CSS box model.


Learn more at amazon.com

More HTML Code:
• HTML Select List Basics
• Adding Space Around an Image
• Setting the Number of Items Visible in a Select List
• Image Map Basics
• Wrapping Text Around Images
• How to Use a Meta Redirect
• Use HTML Target Attribute to Specify Where to Open Document
• Webpage DOCTYPE Declarations Explained
• HTML Horizontal Rule
• How to Debug HTML