Live Preview
A simple styled div container:
This content is wrapped in a div with a light blue background.

By the end of this tutorial, you’ll use the <div> element confidently for layout and grouping in real projects.
Write valid <div> containers with correct opening, closing, and nesting rules.
Target divs with CSS classes and unique ids for styling and JavaScript hooks.
Understand how div differs from span and semantic elements.
Build cards, page regions, and flexbox grids with nested div containers.
Apply flexbox, grid, spacing, and colors through external stylesheets.
Prefer semantic HTML, avoid div soup, and keep accessibility in mind.
<div> Tag?The <div> element (short for “division”) is a generic block-level container. It groups content for styling and layout but carries no semantic meaning on its own. Browsers display div as a block—it starts on a new line and stretches to the available width by default.
Use <div> when no semantic element (header, main, section, article) fits your content, or as an extra wrapper for CSS layout hooks.
The div tag is one of HTML’s most-used elements—it is the building block behind cards, grids, sidebars, and component wrappers in virtually every modern website.
Wrap grouped content between opening and closing div tags:
<div>
<p>Your grouped content here.</p>
</div><div> is a block-level element—it starts on a new line and spans full width by default.<div /> is not valid HTML.class or id attributes to target divs with CSS and JavaScript.| Feature | Syntax / Rule | Notes |
|---|---|---|
| Tag-specific attrs | None | Global attributes only |
| Display | Block | New line, full width |
| Common attrs | class, id | For CSS and JavaScript |
| vs span | Block vs inline | span flows inside text |
| Semantics | None | Prefer header, main, section when they fit |
| Use cases | Layout, cards, grids | Wrappers, flexbox, grid containers |
<div> vs Semantic ElementsPrefer semantic elements when they describe your content. Use div as a generic fallback:
| Element | Meaning | When to use |
|---|---|---|
<div> | Generic container | No semantic role; layout wrapper only |
<header> | Page or section header | Introductory content, logos, nav bars |
<main> | Primary content | Central page content (one per page) |
<section> | Thematic grouping | Grouped topic blocks with a heading |
<article> | Self-contained content | Blog posts, cards, news items |
<div> vs <span>Both are generic containers, but they differ in display behavior:
| Element | Display | Typical use |
|---|---|---|
<div> | Block-level | Page regions, cards, columns, layout grids |
<span> | Inline | Highlight words or phrases inside a sentence |
| Layout | Flexbox / grid | Divs are the primary layout building blocks |
| Text styling | Inline hooks | Spans wrap small inline text portions |
The <div> tag has no tag-specific attributes. Use global attributes such as class, id, and style:
class GlobalCSS hook for styling groups of divs. Use meaningful, reusable class names.
class="card highlight-box"id GlobalUnique identifier for a single div. Use for fragment links and JavaScript targeting.
id="welcome"style GlobalInline CSS. Fine for quick demos—prefer external stylesheets in production.
style="padding: 1rem;"role A11yARIA role when div acts as a widget (e.g. role="alert"). Use semantic HTML first.
role="alert"data-* GlobalCustom data attributes for JavaScript hooks without affecting styling.
data-tab="settings"hidden GlobalHides the element from display and accessibility tree when not relevant.
hiddenUse meaningful class names for CSS. Reserve id for unique elements. Prefer external stylesheets over large inline style attributes.
Basic containers, class/id targeting, page regions, and flexbox layouts with copy-ready code and live previews.
A simple styled div container:
This content is wrapped in a div with a light blue background.
Group content in a simple div wrapper.
<div class="card">
<p>This paragraph is grouped inside a div.</p>
</div>Use <div> to group page sections, build card layouts, create CSS grid and flexbox structures, wrap components for JavaScript, and organize content when no semantic element is the right fit.
Target divs with CSS classes and unique ids.
<div id="notice" class="highlight-box">
<p>This div uses class for styling and id for targeting.</p>
</div>Wrap header, main, and footer regions with div containers.
<div class="page-header">
<h1>Your Website Title</h1>
</div>
<div class="page-main">
<p>Your main content goes here.</p>
</div>
<div class="page-footer">
<p>© 2026 Your Company</p>
</div>Use nested divs with CSS flexbox for responsive card layouts.
<div class="container">
<div class="box">Card 1</div>
<div class="box">Card 2</div>
<div class="box">Card 3</div>
</div>Divs have no default styling beyond block display. Use CSS for layout, spacing, colors, and responsive design:
display Block by defaultflexbox Row and column layoutsgrid Two-dimensional layoutspadding Spacing inside containers/* Card container */
.card {
padding: 1rem 1.25rem;
background: #fff;
border: 1px solid #e2e8f0;
border-radius: 8px;
}
/* Flexbox layout */
.container {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}Live styled div container
A div styled with padding, border, and border-radius.
Because div has no semantic role, accessibility depends on how you use it:
header, main, nav help screen readers.role="alert" for dynamic messages.h1–h6 for titles.Group related elements inside a div container.
The div starts on a new line and spans available width.
Classes and ids hook into flexbox, grid, colors, and spacing.
Organized, styleable content blocks for any layout.
The <div> element is fully supported in all browsers, including legacy Internet Explorer.
From legacy Internet Explorer to the latest mobile browsers — the div element is fully supported everywhere for grouping and layout.
Bottom line: Ship div-based layouts with confidence. The <div> element is safe to use in every production environment today.
The <div> tag is the workhorse container of HTML—generic, block-level, and styleable with CSS. Use it for layout and grouping, but reach for semantic elements first when they describe your content.
class names<div>Bookmark these before you ship — they’ll keep your layout markup clean and maintainable.
<div> groups content with no built-in semantic meaning.
Starts on a new line and spans full width by default.
DisplayTarget divs with CSS classes and unique ids.
AttributesPrimary building blocks for modern CSS layouts.
LayoutPrefer header, main, section when they describe content.
Best practiceWorks in every browser, including legacy IE.
Compatibilitydiv is block-level (new line, full width). span is inline and flows within text.header, main, nav, section, and article when they match your content structure.class, id, style, and data-* apply.class or id and write CSS rules. Flexbox and grid are common for multi-column layouts.Practice grouping, styling, and flexbox layouts in the interactive HTML editor.
6 people found this page helpful