Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Elements

0/1

HTML Attributes

0/1

HTML Headings

0/1

HTML Paragraphs

0/1

HTML Styles

0/1

HTML Formatting

0/1

HTML Quotation

0/1

HTML Comments

0/1

HTML Colors

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Block and Inline

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML - The Head Element

0/1

HTML Style Guide

0/1

HTML Entities

0/1

HTML Symbols

0/1
Text lesson

HTML Div Element

The <div> Element

By default, the <div> element is a block-level element, thus occupying the entire available width and introducing line breaks before and after itself.

Example

A <div> element utilizes the entire width available.

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

Result

Lorem Ipsum

I am a div

dolor sit amet.

While the <div> element doesn’t necessitate mandatory attributes, style, class, and id are frequently used.

<div> as a container

The <div> element is commonly employed to organize sections of a web page into groups.

Example

A <div> element containing HTML elements:

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

Result

IMG_2787

Center align a <div> element

To center-align a <div> element that isn’t 100% wide, apply the CSS margin property with the value of auto.

Example

<style>
div {
  width:300px;
  margin:auto;
}
</style>

Result

IMG_2788

Multiple <div> elements

Multiple <div> containers can coexist on the same page.

Example

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

<div>
  <h2>Oslo</h2>
  <p>Oslo is the capital city of Norway.</p>
  <p>Oslo has over 600.000 inhabitants.</p>
</div>

<div>
  <h2>Rome</h2>
  <p>Rome is the capital city of Italy.</p>
  <p>Rome has almost 3 million inhabitants.</p>
</div>

Result

IMG_2789

Aligning <div> elements side by side

When creating web pages, it’s common to arrange two or more <div> elements horizontally, as shown here:

Result

When constructing web pages, it’s common to desire positioning two or more <div> elements adjacent to each other, such as this:

IMG_2790

Various techniques exist for aligning elements side by side, all of which involve applying CSS styling. We’ll explore the most prevalent methods.

Float

Initially, the CSS float property wasn’t designed specifically for aligning <div> elements side by side, yet it has been employed for this purpose for numerous years.

The CSS float property serves for positioning and formatting content, enabling elements to float adjacent to each other rather than stacking atop one another.

Example

“Using the float property to align <div> elements side by side:”

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

Result

IMG_2791

Inline-block

When you modify the display property of a <div> element from block to inline-block, it eliminates the line breaks before and after the <div> elements. Consequently, they are displayed adjacent to each other instead of stacked vertically.

Example

“Utilizing display: inline-block to align <div> elements side by side:”

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

Result

IMG_2792

Flex

The CSS Flexbox Layout Module was introduced to simplify the creation of flexible and responsive layout structures without relying on float or positioning.

To implement the CSS flex method, enclose the <div> elements within another <div> element and designate it as a flex container.

Example

“Using flex to align <div> elements side by side:”

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

Result

IMG_2793

Grid

The CSS Grid Layout Module provides a grid-oriented layout system featuring rows and columns, simplifying web page design without necessitating floats and positioning.

While similar to flex, the grid method offers the capability to define multiple rows and position each row independently.

To implement the CSS grid method, encapsulate the <div> elements within another <div> element designated as a grid container. Additionally, specify the width of each column.

Example

“How to align <div> elements side by side using grid:”

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

Result

IMG_2794