HTML Div
In this page:
<div class="classname">
Content
</div>
The Generic div Container
The div tag defines a generic block-level division or container. It does not carry any semantic meaning, but it is highly useful for grouping elements, separating sections, and applying CSS layout rules.
Example: The Generic div Container
<div>Grouped content with no semantic meaning</div>
Styling Divisions with CSS
Because div is a generic block container, it is the perfect element for applying CSS classes to create custom visual cards, boxes, borders, and margins on your page.
Example: Styling Divisions with CSS
<div class="card">Styled card content</div>
Flexible Column Grids with Flexbox
You can use div tags to build modern, flexible column grids. By setting a parent div's display property to flex, you can arrange its child divs side-by-side cleanly and control their alignment.
Example: Flexible Column Grids with Flexbox
<div style="display: flex;">
<div>Column 1</div>
<div>Column 2</div>
</div>
Consistent Indentation and Nesting
Nesting multiple divs inside each other is the key to building complex web pages. To keep your code clean, always use clear indentation to show which divs are nested inside others.
Example: Consistent Indentation and Nesting
<div class="outer">
<div class="inner">
Nested content
</div>
</div>
Div Accessibility and ARIA
Because div is a completely generic, non-semantic element, assistive devices like screen readers will ignore it entirely.
If you use a div to build an interactive component (like a custom button or modal), you must add standard ARIA roles to keep it accessible.
Example: Div Accessibility and ARIA
<div role="button" tabindex="0" aria-label="Close dialog">X</div>
- Using plain, generic div elements for everything instead of semantic layout tags.
- Forgetting to write a closing div tag, which causes subsequent elements to merge and breaks layouts.
- Using styled div containers as interactive buttons without adding necessary ARIA roles or keyboard focus support.
- The div tag defines a generic block-level container that carries zero semantic meaning.
- Use divs to group elements, separate sections, and apply CSS styling or Flexbox layouts cleanly.
- Always write clear indentation and closing tags to keep your layouts organized and easy to maintain.
Standard div elements and block-level styling rules are supported natively by all web browsers.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: