HTML <div> Tag

The HTML <div> tag represents a generic section within an HTML document. The <div> tag enables you to group sections of HTML elements together and format them with CSS.

The <div> element is similar to the <span> element, however the <span> element is used with inline elements, whereas the <div> element is used with block-level elements.

The <div> element accepts "flow content", which refers to most elements that can appear inside a document's body.

The <div> should only be used as a last resort when there isn't another suitable HTML element to use. HTML5 has introduced a number of new elements that can (and should) be used in place of the <div>. These include <article>, <aside>, <header>, and <footer> and others. See below under "Differences Between HTML 4 & HTML 5" for more information on this.

Syntax

The <div> tag is written as <div></div> with the generic content inserted between the start and end tags.

Like this:

Copy to Clipboard
<div>
Generic content here...
</div>

Examples

Basic tag usage

As you can see, simply placing content inside a <div> tag doesn't really do anything or change the content's appearance. It simply acts as a container that holds the enclosed content.

CodeResult

Generic content here...

Applying Styles

One of the most common reasons for using the <div> element is to apply styles. You can apply styles to the element itself, or to any element enclosed within it.

Inline Styles

You can style an element using inline styles, where you apply the styles via the style attribute.

CodeResult

Generic content...

Embedded & External Styles

You can also style an element using either embedded styles or an external style sheet.

Here's an example of using embedded styles and applying them via the class attribute of the <div> tag:

CodeResult

Generic content...

  • Item 1
  • Item 2
  • Item 3

Nested <div> Elements

You can nest <div> elements inside each other. Each of these could have different styles applied.

CodeResult

Generic content...

Nested div content...

Differences Between HTML 4 & HTML 5

In HTML 4.01 documents, the <div> tag was often used for specifying the various navigational sections of the HTML document (such as the header, footer, content area, side bars, etc).

The HTML 5 specification has introduced a number of new elements that can (and should) be used instead of the <div> element. Examples of these new elements include <article>, <aside>, <header>, and <footer>, as well as others.

Therefore, the <div> element should generally be used as an extension mechanism which is used only if there isn't another suitable HTML element to use.

To see more detail on the two versions see HTML5 <div> Tag and HTML4 <div> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <div> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

Note that the <div> element does not actually have any local attributes (i.e. attributes that are specific to the element), but the following global attributes and event handlers are available to the element (and all other HTML elements).

For more information on attributes for this tag, see HTML5 <div> Tag and HTML4 <div> Tag.

Copy to Clipboard
<div 
 accesskey=""
 class=""
 contenteditable=""
 contextmenu=""
 dir=""
 draggable=""
 dropzone=""
 hidden=""
 id=""
 itemid=""
 itemprop=""
 itemref=""
 itemscope=""
 itemtype=""
 lang=""
 spellcheck=""
 style=""
 tabindex=""
 title=""
 translate=""

 onabort=""
 onautocomplete=""
 onautocompleteerror=""
 onblur=""
 oncancel=""
 oncanplay=""
 oncanplaythrough=""
 onchange=""
 onclick=""
 onclose=""
 oncontextmenu=""
 oncuechange=""
 ondblclick=""
 ondrag=""
 ondragend=""
 ondragenter=""
 ondragexit=""
 ondragleave=""
 ondragover=""
 ondragstart=""
 ondrop=""
 ondurationchange=""
 onemptied=""
 onended=""
 onerror=""
 onfocus=""
 oninput=""
 oninvalid=""
 onkeydown=""
 onkeypress=""
 onkeyup=""
 onload=""
 onloadeddata=""
 onloadedmetadata=""
 onloadstart=""
 onmousedown=""
 onmouseenter=""
 onmouseleave=""
 onmousemove=""
 onmouseout=""
 onmouseover=""
 onmouseup=""
 onmousewheel=""
 onpause=""
 onplay=""
 onplaying=""
 onprogress=""
 onratechange=""
 onreset=""
 onresize=""
 onscroll=""
 onseeked=""
 onseeking=""
 onselect=""
 onshow=""
 onsort=""
 onstalled=""
 onsubmit=""
 onsuspend=""
 ontimeupdate=""
 ontoggle=""
 onvolumechange=""
 onwaiting=""
	>
</div>

Tag Details

For more details about the <div> tag, see HTML5 <div> Tag and HTML4 <div> Tag.

Specifications

Here are the official specifications for the <div> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.

Enjoy this page?