bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/HTML/HTML Foundations
HTML•HTML Foundations

HTML Div Element

Concept visual

HTML Div Element

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

The

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

Formula

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

Formula

A < div > element takes up all available width:
Lorem Ipsum < div > I am a div </div >

dolor sit amet.

Result

Lorem Ipsum

I am a div dolor sit amet.

The

<div> element has no required attributes, but style, class and id are common.

Formula

< div > as a container

The

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

Example

Formula

A < div > element with HTML elements:

<div>

Formula

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

</div>

Result

London

London is the capital city of England. London has over 9 million inhabitants.

Formula

Center align a < div > element

If you have a

<div>

Formula

element that is not 100% wide, and you want to center - align it, set the CSS

margin property to auto.

Example

<style>

div {
width:300px;
margin:auto;
}

</style>

Result

London

London is the capital city of England. London has over 9 million inhabitants.

Formula

Multiple < div > elements

You can have many

<div> containers on the same page.

Example

<div>

Formula

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

</div> <div>

Formula

< h2 > Oslo </h2 >
< p > Oslo is the capital city of

Norway.</p>

Formula

< p > Oslo has over 700,000 inhabitants.</p >

</div> <div>

Formula

< h2 > Rome </h2 >
< p > Rome is the capital city of

Italy.</p>

Formula

< p > Rome has over 4 million inhabitants.</p >

</div>

Result

London

London is the capital city of England. London has over 9 million inhabitants.

Oslo

Oslo is the capital city of Norway. Oslo has over 700,000 inhabitants.

Rome

Rome is the capital city of Italy. Rome has over 4 million inhabitants.

Formula

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:

London

London is the capital city of England. London has over 9 million inhabitants.

Oslo

Oslo is the capital city of Norway. Oslo has over 700,000 inhabitants.

Rome

Rome is the capital city of Italy. Rome has over 4 million inhabitants. There are different methods for aligning elements side by side, all include some CSS styling. We will look at the most common methods:

Float

Previous

HTML Block and Inline Elements

Next

HTML class Attribute