Tutorial Playlist
Cascading Style Sheets (CSS) is a fundamental technology for designing and styling web pages. It allows developers to control the layout and appearance of elements on a website. Float is an essential aspect of CSS. Float in CSS plays an important role in creating dynamic page layouts. The "float" property allows elements to be removed from the normal flow of the document and positioned either to the left or right of their parent container. This allows the other content to flow around them.
Through this tutorial, you will be better equipped to create attractive, responsive, and versatile web layouts. Keep reading to know more!
In this tutorial, we will delve deeper into how float works, its properties, and some CSS float examples which will help you gain a deeper understanding of CSS float.
CSS float, a fundamental layout property, enables horizontal shifting of elements within a container, with options limited to left or right placement. This property exclusively impacts horizontal positioning, precluding any vertical floating capabilities.
Upon applying float to an element, it is extracted from the standard document flow and is maneuvered as far to the left or right as feasible within its enclosing container. Consequently, the floated element can occupy an extreme position, either leftmost or rightmost, contingent on the specified float direction.
A primary application of CSS float is evident in text-image integration. For instance, when an image is floated to the right, adjoining text gracefully envelops it on the left, yielding an aesthetically pleasing composition. Similarly, the leftward image floating prompts text to flow around the image's right side.
Notably, subsequent elements dynamically adapt their arrangement to encompass the floated element. Conversely, antecedent elements remain unperturbed, steadfastly preserving their layout without entwining around the floated element.
clear | This property is used to remove elements after the floating elements. | left, right, both, none, inherit |
float | It defines whether the box can float or not. | left, right, none, inherit |
Example:
Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
 float: right;
}
</style>
</head>
<body>
<h2>Float Right</h2>
<p>The image will float to the right,and the text in the paragraph will wrap around the image.</p>
<p><img src="https://th.bing.com/th/id/OIP.bxPVEkbjhp9Osye5ruAffAAAAA?pid=ImgDet&rs=1"Â
style="width:170px;height:170px;margin-left:15px;">
The queen of flowers, rose is very beautiful and attractive. It grows in different colours like red, white, yellow,
pink and other varieties. Small thorns on the stem protect the plant. In the world of art, poetry and literature,
rose has been glorified as a symbol of love, compassion and eternal beauty. So it is one of the most commonly used flowers whenÂ
gifting a bouquet or decorating a space with floral arrangements. Rose also has many beneficial properties. So they are used inÂ
many home remedies and self-care products. Rose is also the most beautifully fragrant flower, and its essence is also usedÂ
in many cosmetic products like creams, soaps and perfumes. Interestingly, the rose is an edible flower so it lends itselfÂ
to several recipes like rose milk or rose lassi, or even rose flavoured candy, chocolates and mithais. Roses stand outÂ
amongst all other beautiful and colourful flowers.</p>
</body>
</html>
The CSS float property is used to specify how an element should be floated (positioned) within its containing element. It's commonly used for creating layouts, especially for creating multi-column designs or wrapping text around images. However, it's important to note that the float property is somewhat outdated and has some limitations. Modern layout techniques like Flexbox and CSS Grid are generally preferred for creating more complex layouts.
The float property has the following values:
It's important to understand that when an element is floated, it is taken out of the normal flow of the document, which can have unintended consequences for layout and positioning of other elements. You often need to use additional CSS techniques, such as clearfixes, to ensure that the containing element properly encloses the floated elements.
As mentioned earlier, modern layout techniques like Flexbox and CSS Grid provide more powerful and flexible ways to create layouts without some of the complications that float-based layouts can introduce.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
 float: left;
}
</style>
</head>
<body>
<h2>Float Left</h2>
<p>The image will float to the left,and the text in the paragraph will wrap around the image.</p>
<p><img src="https://th.bing.com/th/id/OIP.bxPVEkbjhp9Osye5ruAffAAAAA?pid=ImgDet&rs=1"Â
style="width:170px;height:170px;margin-left:15px;">
The queen of flowers, rose is very beautiful and attractive. It grows in different colours like red, white, yellow,
pink and other varieties. Small thorns on the stem protect the plant. In the world of art, poetry and literature,
rose has been glorified as a symbol of love, compassion and eternal beauty. So it is one of the most commonly used flowers whenÂ
gifting a bouquet or decorating a space with floral arrangements. Rose also has many beneficial properties. So they are used inÂ
many home remedies and self-care products. Rose is also the most beautifully fragrant flower, and its essence is also usedÂ
in many cosmetic products like creams, soaps and perfumes. Interestingly, the rose is an edible flower so it lends itselfÂ
to several recipes like rose milk or rose lassi, or even rose flavoured candy, chocolates and mithais. Roses stand outÂ
amongst all other beautiful and colourful flowers.</p>
</body>
</html>
none:
Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
 float: none;
}
</style>
</head>
<body>
<h2>Float none</h2>
<p>The image will be displayed just where it occurs in the text</p>
<p><img src="https://th.bing.com/th/id/OIP.bxPVEkbjhp9Osye5ruAffAAAAA?pid=ImgDet&rs=1"Â
style="width:170px;height:170px;margin-left:15px;">
The queen of flowers, rose is very beautiful and attractive. It grows in different colours like red, white, yellow,
pink and other varieties. Small thorns on the stem protect the plant. In the world of art, poetry and literature,
rose has been glorified as a symbol of love, compassion and eternal beauty. So it is one of the most commonly used flowers whenÂ
gifting a bouquet or decorating a space with floral arrangements. Rose also has many beneficial properties. So they are used inÂ
many home remedies and self-care products. Rose is also the most beautifully fragrant flower, and its essence is also usedÂ
in many cosmetic products like creams, soaps and perfumes. Interestingly, the rose is an edible flower so it lends itselfÂ
to several recipes like rose milk or rose lassi, or even rose flavoured candy, chocolates and mithais. Roses stand outÂ
amongst all other beautiful and colourful flowers.</p>
</body>
</html>
Left:
Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
 float: left;
}
</style>
</head>
<body>
<h2>Float Left</h2>
<p>The image will float to the left,and the text in the paragraph will wrap around the image.</p>
<p><img src="https://th.bing.com/th/id/OIP.bxPVEkbjhp9Osye5ruAffAAAAA?pid=ImgDet&rs=1"Â
style="width:170px;height:170px;margin-left:15px;">
The queen of flowers, rose is very beautiful and attractive. It grows in different colours like red, white, yellow,
pink and other varieties. Small thorns on the stem protect the plant. In the world of art, poetry and literature,
rose has been glorified as a symbol of love, compassion and eternal beauty. So it is one of the most commonly used flowers whenÂ
gifting a bouquet or decorating a space with floral arrangements. Rose also has many beneficial properties. So they are used inÂ
many home remedies and self-care products. Rose is also the most beautifully fragrant flower, and its essence is also usedÂ
in many cosmetic products like creams, soaps and perfumes. Interestingly, the rose is an edible flower so it lends itselfÂ
to several recipes like rose milk or rose lassi, or even rose flavoured candy, chocolates and mithais. Roses stand outÂ
amongst all other beautiful and colourful flowers.</p>
</body>
</html>
Right:
Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
 float: right;
}
</style>
</head>
<body>
<h2>Float Right</h2>
<p>The image will float to the right,and the text in the paragraph will wrap around the image.</p>
<p><img src="https://th.bing.com/th/id/OIP.bxPVEkbjhp9Osye5ruAffAAAAA?pid=ImgDet&rs=1"Â
style="width:170px;height:170px;margin-left:15px;">
The queen of flowers, rose is very beautiful and attractive. It grows in different colours like red, white, yellow,
pink and other varieties. Small thorns on the stem protect the plant. In the world of art, poetry and literature,
rose has been glorified as a symbol of love, compassion and eternal beauty. So it is one of the most commonly used flowers whenÂ
gifting a bouquet or decorating a space with floral arrangements. Rose also has many beneficial properties. So they are used inÂ
many home remedies and self-care products. Rose is also the most beautifully fragrant flower, and its essence is also usedÂ
in many cosmetic products like creams, soaps and perfumes. Interestingly, the rose is an edible flower so it lends itselfÂ
to several recipes like rose milk or rose lassi, or even rose flavoured candy, chocolates and mithais. Roses stand outÂ
amongst all other beautiful and colourful flowers.</p>
</body>
</html>
inherit:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Float Inherit Example</title>
<style>
 .parent {
  float: left;
  width: 200px;
  background-color: lightblue;
  padding: 10px;
 }
 Â
 .child {
  float: inherit; /* Inherit the float value from the parent */
  width: 100px;
  height: 100px;
  background-color: lightgreen;
  margin: 10px;
 }
</style>
</head>
<body>
 <div class="parent">
  <div class="child">Child 1</div>
  <div class="child">Child 2</div>
 </div>
</body>
</html>
Thus, embracing CSS float empowers designers to develop attractive, responsive, and versatile web layouts that enhance user experience and aesthetics. CSS float is a preferred choice for web designers. If you want to delve deeper into the field of web designing, consider taking up a full-stack development course from upGrad. It is an excellent choice for students who have creative and analytical minds and want to build exciting web pages.
Floating elements are commonly used for creating text wrapping around images, creating multi-column layouts, and positioning elements in specific arrangements within their containers.
To prevent other elements from wrapping around a floated element, you can use the "clear" property. By setting "clear" to "left" or "right" on an element, you force it to move below any floated elements on that side.
While CSS float has been widely used in the past, newer layout techniques like Flexbox and CSS Grid offer more control and flexibility. For complex layouts, it is recommended to use these modern methods instead of relying solely on CSS float.
Using float may lead to layout problems like clear-fixing, where elements lose their height, and nested floats may cause unexpected behavior. Additionally, floated elements can sometimes be challenging to center within their containers.
Yes, you can float non-block elements like images and inline elements. When floated, they will behave like block-level elements and create wrapping effects for surrounding content.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...