HTML Β· Chapter 21 of 45

HTML Div Element

The <div> tag is a generic block-level container used to group other elements together, primarily for styling and layout purposes with CSS or JavaScript.

By itself, a <div> has no semantic meaning β€” it's a plain box. Modern HTML5 encourages using semantic elements like <section> or <article> where they fit better, reserving <div> for purely structural grouping.

Syntax
<div class="box">content</div>

Grouping content

Divs are commonly used to wrap sections of a page (like a sidebar or card) so CSS can style or position the whole group at once.

Div vs semantic tags

Where possible, prefer <header>, <nav>, <main>, <section>, or <article> over <div> since they add meaning for accessibility and SEO.

Example 1 (html)
<div class="card">
  <h2>Title</h2>
  <p>Description</p>
</div>
Output
Title
Description

A div groups a heading and paragraph so they can be styled together as one 'card'.

Example 2 (html)
<div style="display:flex; gap:10px;">
  <div>Box A</div>
  <div>Box B</div>
</div>
Output
Box A  Box B

Nested divs with flexbox create a simple horizontal layout.

Key points

  • <div> is a generic, non-semantic block container.
  • Used to group elements for CSS styling or JS targeting.
  • Prefer semantic tags like <section> when meaning matters.
  • Divs are extremely common in real-world layouts.
πŸ’‘ Note: Overusing divs for everything is sometimes called 'divitis' β€” use semantic tags where appropriate.

πŸ“ Quick Quiz

1. What kind of container is <div>?

2. What is 'divitis'?

3. Which is a more semantic alternative to div for an article?