Skip to main content

HTML Tags

HTML <div> Tag

Written by Updated HTML <div> Tag exercises

The div tag is a plain box. It means nothing by itself; it groups other elements so you can style them or lay them out together. It is the most used tag on the web, and often used where a more meaningful tag would be better.

That is the rule of thumb: if the box is a header, a navigation menu, an article or a footer, use those semantic tags. Use <div> for everything else, like wrappers for layout and styling.

Syntax

html

<div>
    Content goes here...
</div>

There are no special attributes, only the global ones. class is the one you will use most, to connect the div to CSS.

Example

html

<!DOCTYPE html>
<html>
<head>
    <title>DIV Example</title>
    <style>
        .box {
            width: 200px;
            padding: 20px;
            background-color: lightblue;
            border: 2px solid #333;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>

    <h2>DIV Example</h2>

    <div class="box">
        This is a content box inside a DIV.
    </div>

    <div class="box">
        Another DIV with the same CSS class.
    </div>

</body>
</html>

Two light blue boxes with a border appear, one below the other. Both use the same box class, so they share the same look.

Where divs belong

  • Wrappers for layout: a container with Flexbox or Grid inside.
  • Cards and boxes that have no more specific meaning.
  • Groups that JavaScript needs to show, hide or move together.

Comparing it with its inline cousin is covered in HTML Div & Span.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • A div is a block: it starts on a new line and takes the full width, unless CSS changes that.
  • Do not make a clickable div instead of a button. It cannot be reached with the keyboard or used by screen readers. Use <button>.
  • A div inside a <p> or <span> is invalid; the browser moves it.

HTML <div> Tag exercises

Group for styling

Make a container with the class panel that holds two paragraphs: One and Two.

Result

Check your answer to see it here.