Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › HTML Snippets › Reference › <div>

HTML Reference

<div> — The Container Element

The most-used HTML element. A generic block container with no meaning — and that's exactly the point.

Last updated: October 2026

Definition: The <div> element is a generic block-level container with no semantic meaning. It is used to group content for styling or scripting when no other semantic element fits.

🎬 Live Example

Three divs styled as a card layout. This is the classic use case — pure structure for CSS.

Card Title
This outer div groups the title and description. No semantic meaning — just a wrapper for styling.
Card Title
Same structure. The div is doing nothing except letting CSS target these groups.

💡 Every card above is a <div>. If this content were a blog post, we would use <article> instead.

📝

Basic Syntax

<div>
    <p>Any content here</p>
</div>

The <div> has no attributes of its own (only global attributes). It is a wrapper.

⚙️ Global Attributes

The <div> has no specific attributes. It uses the standard global attributes available to every HTML element:

Attribute Purpose
idUnique identifier for CSS or JavaScript.
classOne or more class names for CSS.
styleInline CSS.
data-*Custom data attributes for JavaScript.
titleTooltip on hover.
hiddenHides the element.
roleARIA role for accessibility.
aria-*ARIA attributes for accessibility.
1

The Wrapper Pattern

Group content to apply layout styles. This is the most common use of <div>.

<div class="card">
    <h3>Card Title</h3>
    <p>Card description.</p>
</div>
2

The Grid Container Pattern

A div that wraps other divs to form a grid or flex layout.

<div class="grid grid-cols-3 gap-4">
    <div>Item 1</div>
    <div>Item 2</div>
    <div>Item 3</div>
</div>
3

JavaScript Hook Pattern

A div used as a target for JavaScript selection or dynamic rendering.

<div id="app-root"></div>

<script>
    const root = document.getElementById("app-root");
    root.innerHTML = "<h1>Rendered by JS</h1>";
</script>
4

data-* Attributes for Interactivity

Store custom data on a div that JavaScript can read later.

<div class="product" data-id="42" data-price="29.99">
    <h3>Widget</h3>
</div>

<script>
    const product = document.querySelector(".product");
    console.log(product.dataset.id);    // "42"
    console.log(product.dataset.price); // "29.99"
</script>

🎯 div vs Semantic Elements

The single most important decision when using <div> — is there a semantic element that fits?

If the content is... Use this instead
The top of a page or section<header>
A set of navigation links<nav>
The main content of the page<main>
A self-contained piece (blog post, product)<article>
A thematic grouping with a heading<section>
Tangential content (sidebar, related posts)<aside>
The bottom of a page or section<footer>
A clickable action<button>
A paragraph of text<p>

💡 Rule of thumb: If you're wrapping content only because CSS needs a target, use <div>. If the content has meaning (a header, an article, a navigation list), use the semantic element.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the HTML <div> tag?

The <div> tag is a generic block-level container with no semantic meaning. It is used to group elements for styling with CSS or for JavaScript manipulation. It is the most common HTML element.

When should I use a <div>?

Use a <div> when you need a container for styling or scripting and no semantic element fits. For example, a wrapper for a grid, a flex container, or a grouping for JavaScript hooks.

When should I NOT use a <div>?

Avoid using <div> when a semantic element fits. Use <header>, <nav>, <main>, <article>, <section>, <aside>, or <footer> when the content has meaning. Use <button> for buttons, not <div> with onclick.

What is the difference between <div> and <section>?

<div> has no semantic meaning — it is purely for grouping. <section> represents a thematic grouping of content and usually has a heading. Use <section> when the group has meaning; use <div> when it is just for styling.

Can I nest <div> elements?

Yes. Divs can be nested as deeply as needed. This is common in CSS layouts where outer divs define the grid and inner divs define individual components.

🎯 What's Next?

← All HTML Reference