How to Group Elements with div in HTML
A <div> is a box with no meaning and no look of its own. Put elements inside it to group them, and you can style the whole group at once, for example with a border, or reach it from a script.
Syntax
<div>
<elements>
</div>
| Part | What it does |
|---|---|
<div> | Starts the group. |
<elements> | The elements in the group: headings, paragraphs, lists or other div elements. |
</div> | Ends the group. |
MDN's div page describes it as a generic container that has no effect until it's styled.
The Example
The example page shows two team members, Ana and Ben. Each one's name and job title are grouped in a div. First you see the page as it is, then you add one CSS rule that turns each group into a box.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Team</title>
</head>
<body>
<div>
<h2>Ana</h2>
<p>Designer</p>
</div>
<div>
<h2>Ben</h2>
<p>Developer</p>
</div>
</body>
</html>
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

Nothing shows where the div elements are: a div has no border, background or spacing of its own.
Step 3: Give the Groups a Border
Add a style element with a rule for div to head, so the file looks like this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Team</title>
<style>
div {
border: 1px solid #d1d1d1;
padding: 8px;
margin-bottom: 8px;
}
</style>
</head>
<body>
<div>
<h2>Ana</h2>
<p>Designer</p>
</div>
<div>
<h2>Ben</h2>
<p>Developer</p>
</div>
</body>
</html>
The rule gives every div a light grey border, 8 pixels of space inside the border (padding) and 8 pixels of space below it (margin-bottom). Save the file and reload the page:

The screenshots come from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- Each
divbecame a visible box around its name and job title. The CSS rule styled thediv, and the elements inside it moved with it. - The boxes are stacked and as wide as the page, because a
divis a block element: Chromium reported its display asblock.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting a div to show something on its own | Nothing changes on the page, as Step 2 shows | Style it with CSS, or use an element that has a meaning |
Putting a div inside a p | The browser ends the paragraph before the div, so the paragraph is cut in two | Put the div outside the paragraph, or use span |
Forgetting a </div> | The elements that follow end up inside the group: with Ana's </div> missing, Ben's box was drawn inside Ana's | Close every div you open |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The div doesn't show | It has no style | Give it a border, background or spacing with CSS |
| One box holds the next group too | A </div> is missing | Add the closing tag |
FAQ
What's the difference between div and span?
A div is a block: it starts on a new line and is as wide as its container. A span sits inside a line of text: Chromium reported inline as the display of a span. Use div to group blocks, and span to mark a few words.
Should I use div for everything?
No. When an element with a meaning fits, such as p for a paragraph or ul for a list, use it: it tells browsers and screen readers what the content is. Use div only to group things.
Can I put a div inside a div?
Yes. A div can hold any number of other div elements.
Conclusion
A div groups elements so CSS and scripts can treat them as one. On its own it shows nothing, and once it's styled, the whole group follows.
Additional Resources
- div: The Content Division element, on MDN.
- span: The Content Span element, on MDN.
