HTML Div
The <div> element ("division") is the most general-purpose container in HTML. On its own it has no meaning and no visible style — it simply groups other elements together so you can style them, position them or target them with JavaScript.
A container for other elements
A <div> is a block-level element, so it starts on a new line and fills the available width. Inside it you can put anything: text, headings, images, lists, forms, even other divs.
<style>
.card {
border: 1px solid #ddd;
border-radius: 10px;
padding: 16px;
max-width: 280px;
font-family: sans-serif;
box-shadow: 0 2px 6px rgba(0,0,0,0.12);
}
.card h3 { margin-top: 0; color: #e34c26; }
</style>
<div class="card">
<h3>🗼 Paris</h3>
<p>Capital of France, famous for art, food and a very tall iron tower.</p>
<ul>
<li>Population: about 2.1 million</li>
<li>River: Seine</li>
</ul>
</div>Without the <div> there would be no single box to put the border and shadow around.
Centering a div
A frequently asked question! For a block with a set width, margin: 0 auto centers it horizontally. To center its contents both ways, Flexbox is the modern approach.
<style>
.narrow {
width: 220px;
margin: 0 auto;
background: #e8f0fe;
padding: 10px;
text-align: center;
}
.stage {
display: flex;
justify-content: center; /* horizontal */
align-items: center; /* vertical */
height: 140px;
margin-top: 16px;
background: #fff4e5;
}
.stage div { background: #e34c26; color: white; padding: 12px 20px; border-radius: 8px; }
</style>
<div class="narrow">Centered with margin: 0 auto</div>
<div class="stage">
<div>Centered both ways with flexbox</div>
</div>Divs side by side
Since divs are blocks, they stack vertically by default. To place them in a row, make their parent a flex container.
<style>
.row { display: flex; gap: 12px; font-family: sans-serif; }
.col { flex: 1; padding: 14px; border-radius: 8px; color: white; }
.a { background: #3b82f6; }
.b { background: #10b981; }
.c { background: #f59e0b; }
</style>
<div class="row">
<div class="col a">Column one</div>
<div class="col b">Column two has a bit more text, but the columns stay equal width.</div>
<div class="col c">Column three</div>
</div>Other ways to arrange divs:
| Technique | CSS on the parent | Good for |
|---|---|---|
| Flexbox | display: flex |
Rows or columns of items, alignment |
| Grid | display: grid; grid-template-columns: ... |
Two-dimensional layouts |
| Inline-block | display: inline-block on children |
Simple rows of fixed-size boxes |
| Float | float: left on children |
Older technique; wrapping text around boxes |
div vs. span
<div> |
<span> |
|
|---|---|---|
| Display | Block | Inline |
| Typical use | Grouping sections of a page | Marking a few words inside text |
| Example | A card, a column, a wrapper | Highlighting a word, styling a price |
<div class="price-box">
Total: <span class="price">€24.99</span>
</div>div vs. semantic elements
HTML5 introduced elements with meaning: <header>, <nav>, <main>, <section>, <article>, <aside>, <footer>. They behave just like divs visually but tell browsers, search engines and screen readers what each part of the page is.
<!-- Works, but says nothing about the content -->
<div class="header">…</div>
<div class="nav">…</div>
<!-- Better: the structure is self-describing -->
<header>…</header>
<nav>…</nav>Tip: Reach for a semantic element first. Use
<div>when no other element fits — for example, a wrapper that exists only so you can apply a layout style.
Note: Avoid "div soup" — deep nests of divs with no purpose. Each extra wrapper makes your HTML harder to read and your CSS harder to write.
Test yourself
What is the default display type of <div>?
- Inline
- Block
- Inline-block
- Flex
Which CSS on the parent places child divs in a row?
display: blockdisplay: flexposition: rowfloat: none
When should you prefer <nav> over a <div class="nav">?
- Never — they are identical
- When the block contains the main navigation links
- Only in tables
- Only when using JavaScript