CSS allows us to align <div> elements side by side in many ways. We’ll discuss some ways that are widely used.
The <div> tag is used to define parts of a page or a document. It groups large sections of HTML elements and then styles them with CSS. Three or more different <div> elements can be put side-by-side using CSS. It is very easy if you follow the steps described below.
Let’s see an example and try to discuss each part of the code together.
<body>
<main id="boxes">
<h2>W3docs</h2>
<div id="column1">
<h2>What is Lorem Ipsum?</h2>
Lorem Ipsum is simply dummying text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</div>
<div id="column2">
<h2>Why do we use it?</h2>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humor and the like).
</div>
<div id="column3">
<h2>Where does it come from?:</h2>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.
</div>
</main>
</body>
#boxes {
content: "";
display: table;
clear: both;
}
div {
float: left;
height: 470px;
width: 23%;
padding: 0 10px;
}
#column1 {
background-color: #a1edcc;
}
#column2 {
background-color: #a0e9ed;
width: 43%;
}
#column3 {
background-color: #f497f1;
}
h2 {
color: #000000;
text-align: center;
}
Let’s bring the code parts together to see the result!
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
#boxes {
content: "";
display: table;
clear: both;
}
div {
float: left;
height: 470px;
width: 23%;
padding: 0 10px;
}
#column1 {
background-color: #a1edcc;
}
#column2 {
background-color: #a0e9ed;
width: 43%;
}
#column3 {
background-color: #f497f1;
}
h2 {
color: #000000;
text-align: center;
}
</style>
</head>
<body>
<main id="boxes">
<h2>W3docs</h2>
<div id="column1">
<h2>What is Lorem Ipsum?</h2>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</div>
<div id="column2">
<h2>Why do we use it?</h2>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</div>
<div id="column3">
<h2>Where does it come from?:</h2>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cities of the word in classical literature, discovered the undoubtable source.
</div>
</main>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.container {
width: 600px;
height: 190px;
background-color: #5cbbf2;
padding: 35px 15px 5px;
}
.container:before,
.container:after {
content: "";
display: table;
clear: both;
}
.container div {
float: left;
width: 180px;
height: 160px;
}
#box2 {
background-color: #000000;
margin-left: 30px;
margin-right: 30px;
}
p {
color: white;
padding: 5px 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>W3docs</h2>
<div class="container">
<div id="box1">
<img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1" style="width:180px; height:160px;">
</div>
<div id="box2">
<p> We can create as many divs as many we want side by side with the same height and also with the different heights. </p>
</div>
<div id="box3">
<img src="https://pp.userapi.com/c622225/v622225117/10f33/47AAEI48pJU.jpg?ava=1" style="width:180px; height:160px;">
</div>
</div>
</html>
In this example, we used the CSS padding, margin properties. The padding property creates padding space on all sides of an element’s content. The margin property in CSS creates a space around the element. Also, you can choose any color you want from the color picker for the text.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.box {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
width: 310px;
height: 310px;
border: 2px solid green;
}
.box div {
width: 100px;
padding: 15px;
text-align: center;
color: #000000;
font-family: arial, sans-serif;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.gray {
background-color: gray;
}
.pink {
background-color: pink;
}
</style>
</head>
<body>
<h2>Flex property example</h2>
<div class="box">
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
<div class="gray">GRAY</div>
<div class="pink">PINK</div>
</div>
</body>
</html>
Here, we used the display property with the "flex" value. The display property defines the type of the box which is used for an HTML element. The "flex" value displays an element as a block-level-flex container.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.box div {
width: 100px;
display: inline-block;
padding: 15px;
text-align: center;
color: #000000;
font-family: arial, sans-serif;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.gray {
background-color: gray;
}
.pink {
background-color: pink;
}
.yellow {
background-color: yellow;
}
</style>
</head>
<body>
<h2>Flex property example</h2>
<div class="box">
<div class="green">GREEN</div>
<div class="blue">BLUE</div>
<div class="gray">GRAY</div>
<div class="pink">PINK</div>
<div class="yellow">YELLOW</div>
</div>
</body>
</html>
In the example above, we used the display property with the "inline-block" value, which displays an element as an inline-level block container. Also, we used font-family property which allows creating a prioritized list of font family names and/or generic family names for the selected element.