CSS Float

The CSS float property is used to position an element in the webpage. It can push an element right, left or can inherit the floating property of the parent element.

There is also clear property which is used to specify the direction where other floating elments are not allowed.

Syntax:
float : value;
clear : value;

CSS Float Properties

Property Description Values
clear left, right, none, inherit It is used to avoid other floating elements which flow around it.
float left, right, both, none, inherit It gives the floating direction to the element.

CSS Float Property Values

Values Description
none It is the default position of the element i.e., the element displayed where it occurs in the text.
right It is used to float the element to the right.
left It is used to float the element to the left.
initial It sets the initial floating property of the element.
inherit It specifies that the parent element's floating property will be inherited.

CSS Float


<!DOCTYPE html>
<html lang="en">
<head>
<title> CSS Float </title>
<meta charset="UTF-8">
<style>
#img {
float: right;
margin: 0 0 10px 10px;
}
</style>
</head>
<body>
<p><img id="img" src="image.png" style="width:55%;">
Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. </body>
</html>

Output

CSS Float

Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet.


CSS Float Clear Property

The clear property specifies the sides where other floating elements are not allowed to float.

CSS Float


<!DOCTYPE html>
<html lang="en">
<head>
<title> CSS clear property </title>
<meta charset="UTF-8">
<style type="text/css">
.clearfix:after {
content: ".";
display: block;
height: 0;
visibility: hidden;
clear: both;
}
div {
width: 500px;
background: pink;
border: 1px solid #000000;
}
div p {
width: 150px;
margin: 10px;
padding: 50px 0;
text-align: center;
}
p.blue {
float: left;
background: blue;
}
p.seagreen {
float: right;
background: seagreen;
}
</style>
</head>
<body>
<div class="clearfix">
<p class="blue">Float Left</p>
<p class="seagreen">Float Right</p>
</div>
</body>
</html>

Output

CSS clear property

Float Left

Float Right












Follow Us: