The <div> element is used as a container for other HTML elements. The <div> element is by default a block element, meaning that it takes all available width, and comes with line breaks before and after.

HTML DIV Example

Lorem Ipsum
I am a div
dolor sit amet.

The yellow background is added to demonstrate the footprint of the DIV element.

The <div> element has no required attribute, but <style>, <class> and <id> are common.The yellow background is added to demonstrate the footprint of the DIV element. The <div> element is often used to group sections of a web page together.

London

London is the capital city of England.

London has over 13 million inhabitants.

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>.

London

London is the capital city of England.

London has over 13 million inhabitants.

Multiple DIV Elements

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

London

London is the capital city of England.

London has over 13 million inhabitants.

Olso

Olso is the capital city of Italy.

Olso has almost 3 million inhabitants.

Rome

Rome is the capital city of Italy.

Rome has almost 3 million inhabitants.

CSS styles are added to make it easier to separate the divs, and to make them more pretty:)

When buiding web pages, you often want to have two or more <div> element 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. The CSS <float> property was not originnally meant to align <div> element 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 element float next to each other instead of on top of each other.

London

London is the capital city of England.

London has over 13 million inhabitants.

Oslo

Olso is the capital city capital of Norway.

Olso has over 600.000 inhabitants.

Rome

Rome is the capital city of Italy.

Rome has almost 3 million inhabitants.

Flex Example

The CSS Flexbox Layout Module was introduced to make it easier to design flexible responsive layout structure with 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.

Align there DIV element side by side.

London

London is the capital city of England.

London has over 13 million inhabitands.

Oslo

Oslo is the capital of Norway.

Oslo has over 600.000 inhabitants.

Rome

Rome is the capital city of Italy.

Rome has almost 3 million.

Grin Example

Align three DIV elements side by side.

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. Sound almost the same as flex, but has the ability to define more than one row and position each row individually. The CSS grid method reguires 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.

London

London is capital city of England.

London has over 13 million inhabitants.

Oslo

Oslo is the capital city of Norway.

Oslo has over 600.000 inhabitants.

Rome

Rome is the capital city of Italy.

Rome has almost 3 million inhabitants.