Are you struggling to place two or more divisions side by side in a web page? Then you should checkout CSS float
property.
In this article I will show you how to use CSS float property to display two divisions side by side. Basically we will float one div to left and the other one to right. The width of each div should be around 50%. At the end of floating divs, it is important to use an empty div with class clear-fix. If we forget to add this div, then our rest of the page element will overflow and it will ruin page design. Therefore, whenever you use CSS float
property, don’t miss to use clear
property to fix the floating elements.
<div id="container">
<div class="left">
This is left division
</div>
<div class="right">
This is right division
</div>
<!-- This div is important -->
<div class="clear-fix"></div>
<div>
Add the following CSS code in your css file :
.left{
width: 49%;
float: left;
border: 1px solid #ccc
}
.right{
width: 49%;
float: right;
border: 1px solid #ccc
}
.clear-fix{
clear: both
}
Notice the use of last div with class clear-fix.
The float
property accepts 4 values – left
, right
, inherit
and none
.
Here is complete example:
<html>
<head>
<title>Use Of CSS Float & Clear Properties</title>
<style>
.left{
width: 49%;
float: left;
border: 1px solid #ccc
}
.right{
width: 49%;
float: right;
border: 1px solid #ccc
}
.clear-fix{
clear: both
}
</style>
</head>
<body>
<div id="container">
<div class="left">
This is left division
</div>
<div class="right">
This is right division
</div>
<!-- This div is important -->
<div class="clear-fix"></div>
<div>
</body>
</html>
If you want to display three divisions side by side, use two divs with class .left
and one div with class .right
(or, two divs with class .right
and one div with class .left
). At the end you will have to add one empty div with class .clear-fix
in both cases. Each div width
should be around 33.3% to fit all in a line.
You can try this code here : Datatype Code Editor