Skip to main content

How to Group Elements with div in HTML

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<div>
<elements>
</div>
PartWhat 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:

index.html
<!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:

The page in Chromium: Ana and Designer, then Ben and Developer, as headings and paragraphs, with nothing showing where the div elements are.

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:

index.html, the same page with a style element in its head: a rule for div with a 1-pixel light grey border, 8 pixels of padding and 8 pixels of bottom margin, and the two div elements holding Ana, Designer and Ben, Developer.

index.html
<!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 page in Chromium: two boxes with a light grey border, one holding the heading Ana and the line Designer, the other the heading Ben and the line Developer.

The screenshots come from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Each div became a visible box around its name and job title. The CSS rule styled the div, and the elements inside it moved with it.
  • The boxes are stacked and as wide as the page, because a div is a block element: Chromium reported its display as block.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting a div to show something on its ownNothing changes on the page, as Step 2 showsStyle it with CSS, or use an element that has a meaning
Putting a div inside a pThe browser ends the paragraph before the div, so the paragraph is cut in twoPut 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'sClose every div you open

Troubleshooting​

SymptomLikely causeFix
The div doesn't showIt has no styleGive it a border, background or spacing with CSS
One box holds the next group tooA </div> is missingAdd 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​