HTML <div> Tag
The <div> tag is a container used to define a division or a section. It is a block-level element that does not inherently affect the content, but is primarily used to group HTML elements to be styled with CSS or manipulated with scripts.

Positioning Blocks Defined by <div> Tag
TIP
We recommend using the <div> tag only when no other semantic elements introduced in HTML5 (such as <nav>, <main>, or <article>) are appropriate.
Since <div> is a block-level element, a line break is placed before and after it.
It is possible to place any HTML element within a <div> tag, including another <div>.
DANGER
Block-level elements like <div> cannot be nested inside <p> tags, as the paragraph will be broken at the point where the <div> tag is placed.
To apply styles inside a paragraph use <span> tag, which is used with inline elements.
Syntax
The <div> tag comes in pairs. The content is written between the opening (<div>) and closing (</div>) tags.
Example of the HTML <div> tag:
Example of HTML <div> tag
<!DOCTYPE html>
<html>
<head>
<title>The <div> Tag</title>
</head>
<body>
<h1> The <div> Tag </h1>
<div style="background-color:#8ebf42">
<p>We use the <div> tag to group two paragraphs for applying a background to the text, and to add color to this
<span style="color:#1c87c9">word</span> we place it within <span> tag.</p>
<p> Pay attention, that the <div> tag is a block-level element, so a line break is placed before and after
it.</p>
</div>
</body>
</html>When we build layouts, we deal with multiple blocks defined by the <div> tag. The positioning of these blocks is at the heart of layout: placing elements in the correct relative positions across all screen sizes is one of the most important tasks.
Depending on the content and the goals of the page, we can use different techniques (or their combinations) to determine the place of each block. Let’s learn more about these techniques.
Flexbox ¶
In modern websites float-based layouts are being replaced with Flexbox. The main idea behind Flexbox is that with it, you can control the alignment, direction, order, and size of the items inside the container.
Example of a Flexbox with HTML <div> tag:
An example of HTML <div> tag with Flexbox
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.flex-container {
display: flex;
align-items: center; /* Use another value to see the result */
width: 100%;
height: 200px;
background-color: #1faadb;
}
.flex-container > div {
width: 25%;
height: 60px;
margin: 5px;
border-radius: 3px;
background-color: #8ebf42;
}
</style>
</head>
<body>
<div class="flex-container">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>CSS Float property ¶
CSS float property, or "floats" allows elements to appear next to, or apart from, one another, which lets us create different types of layouts, including multi-column pages, sidebars, grids, etc.
Example of the HTML <div> tag with the CSS float property:
Example of HTML <div> tag with CSS Float property
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.content {
overflow: auto;
border: 3px solid #666;
}
.content div {
padding: 10px;
}
.content a {
color: darkblue;
}
.blue {
float: right;
width: 45%;
background-color: #1faadb;
}
.green {
float: left;
width: 35%;
background-color: #8ebf42;
}
</style>
</head>
<body>
<div class="content">
<div class="blue">
<p>This is some paragraph inside div tag.</p>
<a href="#">This is some hyperlink inside div tag.</a>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
</div>
<div class="green">
<p>This is some paragraph inside div tag.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
</ol>
</div>
</div>
</body>
</html>Result

Negative Margins ¶
Negative margins can be applied to both static or floated elements.
Example of negative margins:
Example of HTML <div> tag with negative margins
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.content div {
padding: 2%;
}
.content a {
color: darkblue;
}
.main-content {
width: 30%;
margin-left: 32%;
}
.blue {
width: 25%;
margin-top: -10%;
background-color: #1faadb;
}
.green {
width: 20%;
margin: -35% auto auto 70%;
background-color: #8ebf42;
}
</style>
</head>
<body>
<div class="content">
<div class="main-content">
<a href="#">This is some hyperlink inside div tag.</a>
</div>
<div class="blue">
<p>This is some paragraph inside div tag.</p>
<a href="#">This is some hyperlink inside div tag.</a>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
</div>
<div class="green">
<p>This is some paragraph inside div tag.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
</ol>
</div>
</div>
</body>
</html>Relative+absolute positioning ¶
If you want to position div relative to particular element you can use a combination of position: relative and position: absolute.
Example of relative+absolute positioning of HTML <div> tag:
An example of HTML <div> tag with CSS position property
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.content {
position: relative;
height: 400px;
border: 1px solid #666;
}
.content div {
position: absolute;
width: 35%;
padding: 10px;
}
.blue {
right: 20px;
bottom: 0;
background-color: #1faadb;
}
.green {
top: 10px;
left: 15px;
background-color: #8ebf42;
}
</style>
</head>
<body>
<div class="content">
<div class="blue">
<p>This is some paragraph inside div tag.</p>
</div>
<div class="green">
<p>This is some paragraph inside div tag.</p>
</div>
</div>
</body>
</html>DANGER
position: relative does not affect the positioning of elements in normal flow unless you add offsets.
Attributes ¶
| Attribute | Value | Description |
|---|---|---|
| align | left right center justify | Was used to align the content inside a <div> tag. This attribute is not supported in HTML5. CSS text-align property is used instead. |
The <div> tag also supports the Global attributes and the Event Attributes.
How to style an HTML <div> tag
{
"tag_name": "div"
}Practice
What is the primary function of the HTML <div> tag?