HTML <div> Tag

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

The <div> element is the generic container for flow content. Alone it does not change meaning or layout. Style it with CSS, or place it inside a Flexbox/Grid parent, when you need a hook for grouping.

Use <div> only when no semantic element fits — for example <section>, <article>, <nav>, or <main>. For inline text wrappers, prefer <span>.

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015 (and long before as a core HTML container).

Obsolete: align.

Do not use align on <div>. Align and lay out boxes with CSS (Flexbox, Grid, text-align, margin, and so on).

HTML Syntax

syntax.html
<div> flow content</div>

Both start and end tags are required. Permitted content is flow content (paragraphs, lists, headings, nested <div>s, and more).

Attributes

<div> has no element-specific attributes. These globals are the ones you will use most:

AttributeDescriptionTry it
classOne or more class names for CSS and scripting hooksTry it
idUnique identifier for one element (anchors, scripts, CSS)Try it
langLanguage of this block when it differs from the parentTry it
alignObsolete. Do not use. Align with CSS insteadAvoid

Global Attributes

The <div> tag also supports the Global Attributes in HTML.

Live Preview

Change the class on a real <div> and see how CSS hooks change its look.

A bare <div> is invisible until CSS (or a layout model) styles it.

Container

Grouped content inside a div.

class="tut-div-live-box"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Grouping

Wrap related flow content in a <div>. Without CSS it looks like normal blocks stacked.

HTML
<div>
  <h2>Welcome</h2>
  <p>This heading and paragraph share one container.</p>
</div>
Try It Yourself

How It Works

  1. Open <div> where you need a generic group.
  2. Put flow content inside (headings, paragraphs, lists, and so on).
  3. Close with </div>. Both tags are required.
  4. Expect no visual change until you add CSS or a layout model.

Example 2 — Style Hook with class

Give the container a class, then target that class in CSS.

HTML
<div class="card">
  <p>A note grouped for styling.</p>
</div>

<style>
  .card {
    border: 1px solid #94a3b8;
    padding: 0.75rem 1rem;
  }
</style>
Try It Yourself

How It Works

  1. Add class="card" on the <div>.
  2. Write a CSS rule for .card (border, padding, and so on).
  3. Reuse the class on other similar groups when the look should match.
  4. Keep styles in a stylesheet for real projects; the inline <style> here is only for a compact demo.

Example 3 — Unique Target with id

Use id for one unique box you may link to or select in script.

HTML
<p><a href="#intro">Jump to intro</a></p>

<div id="intro">
  <p>You landed on the intro block.</p>
</div>
Try It Yourself

How It Works

  1. Set a unique id on the target <div> (one per page).
  2. Link with a fragment such as href="#intro".
  3. Activate the link to scroll to that container.
  4. Prefer semantic targets (<section id="…">) when the region has meaning.

Example 4 — Prefer a Semantic Element

When content is a thematic grouping, use <section> instead of a bare <div>.

HTML
<!-- Prefer this when it is a real section -->
<section>
  <h2>Pricing</h2>
  <p>Plans start at $0 for learning projects.</p>
</section>

<!-- Use div only when no semantic tag fits -->
<div class="layout-row">
  <p>Generic layout wrapper.</p>
</div>
Try It Yourself

How It Works

  1. Ask what the content is (section, article, nav, aside, and so on).
  2. Pick the matching semantic tag when one fits.
  3. Fall back to <div> for pure layout or styling hooks with no extra meaning.
  4. Keep accessibility in mind: <div> is role generic, not a landmark.

Edge Cases & Pitfalls

Most div problems come from overusing it or treating it as a landmark.

semantics

Avoid “div soup”

Nested empty wrappers hurt readability. Prefer semantic tags and only as many <div>s as layout needs.

a11y

Not a landmark

Implicit role is generic. Use <main>, <nav>, or labeled regions for structure screen readers can find.

inline

Do not wrap words in div

For a phrase inside a sentence, use <span>. A <div> breaks to a new line by default.

obsolete

Skip align

Presentational alignment belongs in CSS. The align attribute on <div> is obsolete.

aria

Be careful with ARIA on div

You may assign many roles, but prefer native elements. A role on <div> still needs correct keyboard and naming support.

Browser Support

The <div> element is core HTML in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<div>

Widely available since about July 2015 (and long before as a generic container). Chrome, Firefox, Safari, Edge, and Opera all support it fully.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<div> Widely supported

Bottom line: Safe to use everywhere as a generic container. Prefer semantic elements when they describe the content better.

Key Takeaways

  • Role: <div> groups flow content; it has no meaning by itself.
  • Hooks: style and script with class and id; layout with CSS Flexbox or Grid.
  • Semantics first: choose section, article, nav, and friends when they fit.
  • Avoid: obsolete align, and wrapping inline text in div instead of span.

One line: use <div> as a last-resort generic box, not as a substitute for meaning.

Default CSS Settings

User-agent stylesheets treat div as a block box with no extra chrome:

default.css
div {
  display: block;
}

Margins, padding, borders, and Flexbox/Grid come from your CSS (or from a parent layout), not from the tag itself.

Frequently Asked Questions

The div element is a generic container for flow content. It has no meaning by itself. Use it to group content for styling, scripting, or layout when no semantic element fits better.
No element-specific attributes. Use global attributes such as class, id, lang, and title. The old align attribute is obsolete — use CSS instead.
div is a block-level (flow) container that starts on a new line by default. span is an inline container for text-level phrasing content.
No. Prefer semantic elements like header, nav, main, section, article, or aside when they match the content. Reach for div only when you need a generic wrapper.
div has an implicit ARIA role of generic. It does not announce a landmark. Do not rely on bare divs for page structure that assistive tech should recognize.
Add class or id, then apply CSS (including Flexbox or Grid on the div or its parent). Avoid obsolete align and heavy inline styles for large projects.

Next: dl

Mark up description lists with terms and definitions.

dl tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful