The <div> element is like a empty box. By itself, it does nothing special. But it is one of the most useful tags in HTML because you can put other elements inside it and style them all together.
What is a Div?
“Div” is short for division. It divides your page into sections. By default, a div is a block element. That means it takes up the full width available and adds line breaks before and after.
The div has no required attributes, but style, class, and id are very common. You will use classes and IDs a lot when you learn CSS.
Div as a Container
The most common use of a div is to group related elements together. Instead of styling a heading and two paragraphs separately, you put them inside a div and style the div once.
The div acts like a container that holds the heading and both paragraphs together. If you move the div, everything inside moves with it.
Multiple Divs on One Page
You can have as many divs as you want on a single page. They stack vertically by default because they are block elements.
Each city is inside its own div. They are separate containers, so you can style them differently.
Center Aligning a Div
If your div does not take up the full width (you set a specific width), you can center it using margin: auto;.
The margin: auto; trick only works when the div has a specific width set. Otherwise it just takes full width and centering does nothing.
Aligning Divs Side by Side
Sometimes you want divs to sit next to each other instead of stacking. There are several ways to do this. Here are the three most common methods used today.
Method 1: Flexbox (Recommended for most cases)
Flexbox is the modern way to align divs side by side. It is clean and easy to understand.
The parent div has display: flex;. The child divs then automatically line up horizontally.
Method 2: CSS Grid (Best for complex layouts)
Grid is great when you want more control over rows and columns.
grid-template-columns: 1fr 1fr 1fr; creates three equal columns.
Method 3: Inline-Block (Old way, still works)
You can also change divs from block to inline-block. This makes them sit next to each other, but can be tricky with spacing.
Quick Summary
- Div is a block level container. It takes full width and adds line breaks.
- Use divs to group related elements together.
- You can have many divs on one page.
- Use
margin: auto;to center a div that has a set width. - Use Flexbox, Grid, or Inline-Block to put divs side by side.
- Divs have no visual style by default. You add CSS to make them visible.
What Just Happened? Let Me Explain
- The div is the most used HTML element in web development. Almost every layout you see online uses divs.
- Divs are invisible by design. You add background colors, borders, or padding to see them while you work.
- Flexbox and Grid are modern CSS tools. They make layouts much easier than the old ways.
- Do not use tables for layout. Use divs with CSS instead. Tables are for data only.
- Give your divs meaningful class names like “header”, “sidebar”, “main-content”, or “footer”. This helps you and others understand your code.
HTML Div Reference
Here is the main tag we covered in this chapter:
| Tag | Description |
|---|---|
<div>
| Defines a section or container in an HTML document (block level) |