Skip to main content

HTML Div Element

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


The <div> Element

By default a <div> is a block element, so it takes the full available width and has line breaks before and after it. It has no required attributes, but style, class, and id are common.

Example

A <div> taking up the full width, breaking the surrounding text onto separate lines.

Prompt
A page showing that a div takes the full available width: the text "Lorem Ipsum", then a div reading "I am a div", then "dolor sit amet.", so the div breaks onto its own line.
Output: complete HTML page

<div> as a container

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

Example

A <div> grouping a heading and paragraphs.

Prompt
A div used to group a section: a div containing an h2 "London" and two paragraphs about London.
Output: complete HTML page

Center align a <div> element

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

Example

A centered <div>.

Prompt
A page that center-aligns a 300px-wide div using the CSS margin auto property, containing an h2 "London" and two paragraphs.
Output: complete HTML page

Multiple <div> elements

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

Example

Three <div> containers stacked on the page.

Prompt
A page with three separate div containers, one each for London, Oslo, and Rome, each with an h2 and two paragraphs.
Output: complete HTML page

Aligning <div> elements side by side

When building pages, you often want two or more <div> elements side by side. There are several methods, all using some CSS. The most common are float, inline-block, flex, and grid.


Float

The CSS float property was not originally meant for laying out <div> elements side by side, but it has been used that way for years. It positions and formats content, letting elements sit horizontally instead of vertically.

Example

Three divs placed side by side with float.

Prompt
A page that aligns three city divs side by side using the CSS float property, with each div set to about one third width inside a container.
Output: complete HTML page

Inline-block

If you change a <div>'s display property from block to inline-block, the divs no longer add line breaks before and after, so they sit side by side instead of stacked.

Example

Three divs side by side with display: inline-block.

Prompt
A page that aligns three city divs side by side by setting their display property to inline-block at about 30% width each.
Output: complete HTML page

Flex

The CSS Flexbox layout module makes it easier to build flexible, responsive layouts without floats or positioning. To use it, wrap the <div> elements in another <div> and make it a flex container.

Example

Three divs side by side with flexbox.

Prompt
A page that aligns three city divs side by side using CSS flexbox: a flex container wrapping the three divs.
Output: complete HTML page

Grid

The CSS Grid layout module gives a grid-based system of rows and columns, making layouts easier without floats or positioning. It is similar to flex but can define more than one row and position each one. Wrap the <div> elements in a grid container and set the width of each column.

Example

Three divs side by side with CSS grid.

Prompt
A page that aligns three city divs side by side using CSS grid with three equal columns.
Output: complete HTML page