Little Web Hut

HTML 4.01 and XHTML 1.0 Reference: <div> Tag

HTML <div> Tag - Division
Syntax This element uses separate opening and closing tags.
<div>...</div>
Usage The <div> tag can be used to divide an HTML document into sections. Styles can then be applied to whole sections at the same time. The <div> tag is a block-level element that can contain other block-level elements, inline elements, and text. The <span> tag is similar to the <div> tag except that it is an inline element that can contain other inline elements and text.

HTML <div> tag example using styles.
<html>

<head>
 <title>HTML Test</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <style type="text/css">
  .mainsection {color:#990000; text-align:center}
 </style>
</head>

<body>

<p>This text is not contained in a div element.</p>
<div class="mainsection">
 This text is contained in a div element and styled.<br>
 The class attribute is used to identify which style to apply.<br>
 The style is defined in the head section.
</div>

</body>
</html>
Test It HTML Style

XHTML <div> tag example using styles.
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
 <title>HTML Test</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <style type="text/css">
  .mainsection {color:#990000; text-align:center}
 </style>
</head>

<body>

<p>This text is not contained in a div element.</p>
<div class="mainsection">
 This text is contained in a div element and styled.<br />
 The class attribute is used to identify which style to apply.<br />
 The style is defined in the head section.
</div>

</body>
</html>
Test It XHTML Style

Content Model The <div> element can contain the following tags, and Text, between its opening and closing tags.Notes:
1. The <div> element can contain the <iframe> tag when using DTD Transitional and Frameset documents but not Strict documents.
2. The <div> element can contain the <noframes> tag when using DTD Transitional documents but not Strict or Frameset documents.
3. Red tags have been deprecated and are allowed in DTD Transitional and Frameset documents but not Strict documents.
Video Tutorial

This video demonstrates how to use the HTML / XHTML div tag and how to style it with CSS. The examples will cover the basic usage for the div tag and demonstrate the CSS color, text align, background, float, and width properties.


Red attributes have been deprecated
<div> Tag Attribute Summary
Required Tag Specific Core Focus Events Language
  align id class style title   onclick ondblclick onkeydown onkeypress onkeyup onmousedown onmousemove onmouseout onmouseover onmouseup dir lang xml:lang1
Notes: 1. XHTML only.

Red attributes have been deprecated
<div> Tag Attributes
Attribute Description
alignThe align attribute specifies the horizontal alignment. Possible values are "center", "justify", "left", and "right".

HTML <div> tag example using the align attribute.
<div align="right">
 <h1>This text and image are aligned to the right.</h1>
 <img src="eightball.gif" alt="eight ball">
</div>
Test It HTML Style

XHTML <div> tag example using the align attribute.
<div align="right">
 <h1>This text and image are aligned to the right.</h1>
 <img src="eightball.gif" alt="eight ball" />
</div>
Test It XHTML Style


Note: This attribute has been deprecated.
idThe id attribute assigns a unique name to a tag. This allows style sheets or scripts to reference the tag. See id Example
classThe class attribute assigns a class name to a tag. The class name does not need to be unique. More than one tag can have the same class name. This allows style sheets or scripts to reference multiple tags with a single class name. See class Example
styleThe style attribute specifies styles for the tag. For Cascading Style Sheets (CSS), the syntax is name:value. Each name:value pair is separated by semicolons. See style Example
titleThe title attribute specifies additional information about the tag. It is common for browsers to display the title when the pointing device stops over the object. See title Example
onclickThe onclick attribute specifies a script to be run when the object is clicked with a mouse or other pointing device. See onclick Example
ondblclickThe ondblclick attribute specifies a script to be run when the object is double clicked with a mouse or other pointing device. See ondblclick Example
onkeydownThe onkeydown attribute specifies a script to be run when a key is pressed down. See onkeydown Example
onkeypressThe onkeypress attribute specifies a script to be run when a key is pressed and released. See onkeypress Example
onkeyupThe onkeyup attribute specifies a script to be run when a key is released. See onkeyup Example
onmousedownThe onmousedown attribute specifies a script to be run when the mouse button, or other pointing device button, is pressed while over the object. See onmousedown Example
onmousemoveThe onmousemove attribute specifies a script to be run when the mouse, or other pointing device, is moved while it is over the object. See onmousemove Example
onmouseoutThe onmouseout attribute specifies a script to be run when the mouse, or other pointing device, is moved away from an object after being over it. See onmouseout Example
onmouseoverThe onmouseover attribute specifies a script to be run when the mouse, or other pointing device, is moved onto the object. See onmouseover Example
onmouseupThe onmouseup attribute specifies a script to be run when the mouse button, or other pointing device button, is released while over the object. See onmouseup Example
dirThe dir attribute tells the browser whether the text should be displayed from left-to-right or right-to-left. It does not reverse the direction of the characters, like the <bdo> tag does, but it can help the browser to determine if the text should be aligned on the left side or the right side. See dir Example
langThe lang attribute specifies a language. This attribute can help the browser to correctly display text. This attribute can also be useful for braille translation software, speech synthesizers, dictionary definitions, etc. See lang Example
xml:langThe xml:lang attribute specifies a language for XHTML documents. This attribute can help the browser to correctly display text. This attribute can also be useful for braille translation software, speech synthesizers, dictionary definitions, etc. See xml:lang Example

Note: XHTML only.

HTML <div> Tag Examples
Test It HTML StyleTest It HTML Style<div> tag example using styles.
Test It HTML StyleTest It HTML Style<div> tag example using the align attribute.