logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

HTML Div Element


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:

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.


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


<div>

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


Multiple <div> elements

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

Example

<div>

<h2> London </h2>

<p> London is the capital city of England. </p>

<p> London has over 9 million inhabitants. </p>

</div>

<div>

<h2> Oslo </h2>

<p> Oslo is the capital city of Norway. </p>

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

</div>

<div>

<h2> Rome </h2>

<p> Rome is the capital city of Italy. </p>

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


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:


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:

<style>
div {
width:30px;
display:inline-block;
}
</style>


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:

<style>
.mycontainer {
display: flex;
}
.mycontainer > div {
width:33%;
}
</style>

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.


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 <p> elements side by side:

<style>
.grid-container {
display: grid;
grid-template-columns: 33% 33% 33%;
}
</style>

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.


HTML Tags

Tag Description
<div> Defines a section in a document (block-level)

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis