HTML <div> Tag

The HTML <div> tag is a non-semantic tag that is used to define division in an HTML document. For example,

<div>
    <h2>This is a heading in a div element</h2>
    <p>This is some text in a div element.</p>
</div>

Browser Output

HTML div element  with some elements inside

Here, you can see the <div> tag is used like a container for HTML elements.


Usage of HTML <div>

The HTML <div> tag is generally used to group content and provide CSS styles using class or id attributes. For example,

<div class="div1">
    <h2>This is heading.</h2>
    <p>This is a paragraph</p>
</div>

<div class="div2">
    <h2>This is another heading.</h2>
    <p>This is another paragraph</p>
</div>

<style>
    .div1 {
        color: red;
    }

    .div2 {
        color: green;
    }
</style>

Browser Output

HTML div element styled with css

Here, the styling for the first two <div> comes from selector .div1 and .div2 respectively.

Table of Contents

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community