Extensible HyperText Markup Language

div

Definition

The div element offers a generic way of grouping areas of content.

Example

The div element can be used to create Web page layout. This example illustrates the grouping nature of the div element:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4. <head>
  5. <title>Employee Profile</title>
  6. <style type="text/css">
  7. #navigation {width: 200px; float: left;}
  8. #content {margin-left: 205px;}
  9. #footer {clear: both;}
  10. </style>
  11. </head>
  12. <body>
  13. <div id="header">
  14. ... [banner/logo goes here] ...
  15. </div>
  16. <div id="navigation">
  17. ... [navigation menu goes here] ...
  18. </div>
  19. <div id="content">
  20. ... [page content goes here] ...
  21. </div>
  22. <div id="footer">
  23. ... [copyright info goes here] ...
  24. </div>
  25. </body>
  26. </html>

Best practice

The div element contains no semantic meaning. It is simply a generic way of grouping together areas of content. So, even if the XHTML specification permits the div element to contain text and inline elements, it is better practice to enclose text and inline elements in meaningful block elements such as p, ol, ul, dl, h1 to h6, blockquote, etc. The following example shows text inside a div element:

  1. <div>
  2. Breaking News
  3. </div>

Compare this to a better approach where text can be given semantic meaning by using instead appropriate block elements. For example:

  1. <div>
  2. <h1>Breaking News</h1>
  3. </div>

Attributes

Common core attributes

class
(NameTokens) This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or set of class names. Multiple class names must be separated by white space characters. Class names are typically used to apply CSS formatting rules to an element.
id
(ID) This attribute assigns an ID to an element. This ID must be unique in a document. This ID can be used by client-side scripts (such as JavaScript) to select elements, apply CSS formatting rules, or to build relationships between elements.
title
(Text) This attribute offers advisory information. Some Web browsers will display this information as tooltips. Assistive technologies may make this information available to users as additional information about the element.

Common internationalization attributes

xml:lang
(NameToken) This attribute specifies the base language of an element's attribute values and text content.
dir

This attribute specifies the base direction of text. Possible values:

  • ltr: Left-to-right
  • rtl: Right-to-left

Common event attributes

onclick
(Script) A client-side script event that occurs when a pointing device button is clicked over an element.
ondblclick
(Script) A client-side script event that occurs when a pointing device button is double-clicked over an element.
onmousedown
(Script) A client-side script event that occurs when a pointing device button is pressed down over an element.
onmouseup
(Script) A client-side script event that occurs when a pointing device button is released over an element.
onmouseover
(Script) A client-side script event that occurs when a pointing device is moved onto an element.
onmousemove
(Script) A client-side script event that occurs when a pointing device is moved within an element.
onmouseout
(Script) A client-side script event that occurs when a pointing device is moved away from an element.
onkeypress
(Script) A client-side script event that occurs when a key is pressed down over an element then released.
onkeydown
(Script) A client-side script event that occurs when a key is pressed down over an element.
onkeyup
(Script) A client-side script event that occurs when a key is released over an element.

Common style attribute

style
(Text) This attribute specifies formatting style information for the current element. The content of this attribute is called inline CSS. The style attribute is deprecated (considered outdated), because it fuses together content and formatting.

Contains