Menu

HTML Div Element


Previous


Next

The <div> element is used as a container for other HTML elements.

The <div> Element

The <div> element is by default a block element, meaning that it takes all available width, and comes with line breaks before and after.

 

Example

A <div> element takes up all available width:

<!DOCTYPE html>
<html>
<style>
div {
background-color: #FFF4A3;
}
</style>
<body>

<h1>HTML DIV Example</h1>

Lorem Ipsum <div>I am a div</div> dolor sit amet.

<p>The yellow background is added to demonstrate the footprint of the DIV element.</p>

</body>
</html>

The <div> element has no required attributes, but styleclass and id are common.

<div> as a container

The <div> element is often used to group sections of a web page together.

 

Example

A <div> element with HTML elements:

<!DOCTYPE html>
<html>
<style>
div {
background-color: #FFF4A3;
}
</style>
<body>

<h1>HTML DIV Example</h1>

<div>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

<p>The yellow background is added to demonstrate the footprint of the DIV element.</p>

</body>
</html>

Center align a <div> element

If you have a <div> element that is not 100% wide, and you want to center-align it, set the CSS margin property to auto.

Example

<!DOCTYPE html>
<html>
<style>
div {
width: 300px;
margin: auto;
background-color: #FFF4A3;
}
</style>
<body>

<h1>Center align a DIV element</h1>

<div>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

</body>
</html>

 

 

Multiple <div> elements

You can have many <div> containers on the same page.

 

Example

<!DOCTYPE html>
<html>
<body>

<h1>Multiple DIV Elements</h1>

<div style=”background-color:#FFF4A3;”>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

<div style=”background-color:#FFC0C7;”>
<h2>Oslo</h2>
<p>Oslo is the capital city of Norway.</p>
<p>Oslo has over 600.000 inhabitants.</p>
</div>

<div style=”background-color:#D9EEE1;”>
<h2>Rome</h2>
<p>Rome is the capital city of Italy.</p>
<p>Rome has almost 3 million inhabitants.</p>
</div>

<p>CSS styles are added to make it easier to separate the divs, and to meake them more pretty:)</p>

</body>
</html>

 

 

Aligning <div> elements side by side

When building web pages, you often want to have two or more <div> elements side by side, like this:

 

There are different methods for aligning elements side by side, all include some CSS styling. We will look at the most common methods:

Float

The CSS float property was not originally meant to align <div> elements side-by-side, but has been used for this purpose for many years.

The CSS float property is used for positioning and formatting content and allow elements float next to each other instead of on top of each other.

 

Example

How to use float to align div elements side by side:

<!DOCTYPE html>
<html>
<style>
div.mycontainer {
width:100%;
overflow:auto;
}
div.mycontainer div {
width:33%;
float:left;
}
</style>
<body>

<div>

<div style=”background-color:#FFF4A3;”>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

<div style=”background-color:#FFC0C7;”>
<h2>Oslo</h2>
<p>Oslo is the capital city of Norway.</p>
<p>Oslo has over 600.000 inhabitants.</p>
</div>

<div style=”background-color:#D9EEE1;”>
<h2>Rome</h2>
<p>Rome is the capital city of Italy.</p>
<p>Rome has almost 3 million inhabitants.</p>
</div>

</div>

</body>
</html>

 

Inline-block

If you change the <div> element’s display property from block to inline-block, the <div> elements will no longer add a line break before and after, and will be displayed side by side instead of on top of each other.

 

Example

How to use display: inline-block to align div elements side by side:

<!DOCTYPE html>
<html>
<style>
div {
width:30%;
display:inline-block;
}
</style>
<body>

<div style=”background-color:#FFF4A3;”>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

<div style=”background-color:#FFC0C7;”>
<h2>Oslo</h2>
<p>Oslo is the capital city of Norway.</p>
<p>Oslo has over 600.000 inhabitants.</p>
</div>

<div style=”background-color:#D9EEE1;”>
<h2>Rome</h2>
<p>Rome is the capital city of Italy.</p>
<p>Rome has almost 3 million inhabitants.</p>
</div>

</body>
</html>

 

Flex

The CSS Flexbox Layout Module was introduced to make it easier to design flexible responsive layout structure without using float or positioning.

To make the CSS flex method work, surround the <div> elements with another <div> element and give it the status as a flex container.

Example

How to use flex to align div elements side by side:

<!DOCTYPE html>
<html>
<head>
<style>
.mycontainer {
display: flex;
}
.mycontainer > div {
width:33%;
}
</style>
</head>
<body>

<h1>Flexbox Example</h1>

<p>Align three DIV elements side by side.</p>

<div>

<div style=”background-color:#FFF4A3;”>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

<div style=”background-color:#FFC0C7;”>
<h2>Oslo</h2>
<p>Oslo is the capital city of Norway.</p>
<p>Oslo has over 600.000 inhabitants.</p>
</div>

<div style=”background-color:#D9EEE1;”>
<h2>Rome</h2>
<p>Rome is the capital city of Italy.</p>
<p>Rome has almost 3 million.</p>
</div>

</div>

</body>
</html>

Grid

The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, making it easier to design web pages without having to use floats and positioning.

Sounds almost the same as flex, but has the ability to define more than one row and position each row individually.

The CSS grid method requires that you surround the <div> elements with another <div> element and give the status as a grid container, and you must specify the width of each column.

 

Example

How to use grid to align <div> elements side by side:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: 33% 33% 33%;
}
</style>
</head>
<body>

<h1>Grid Example</h1>

<p>Align three DIV elements side by side.</p>

<div>

<div style=”background-color:#FFF4A3;”>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>

<div style=”background-color:#FFC0C7;”>
<h2>Oslo</h2>
<p>Oslo is the capital city of Norway.</p>
<p>Oslo has over 600.000 inhabitants.</p>
</div>

<div style=”background-color:#D9EEE1;”>
<h2>Rome</h2>
<p>Rome is the capital city of Italy.</p>
<p>Rome has almost 3 million inhabitants.</p>
</div>

</div>

</body>
</html>


Previous


Next

Scroll to Top