contact@codebetter.in 8823075444
9993928766
  • Tutorials Home
  • Contact

What is the CSS float property used for?

The CSS `float` property is used for positioning and aligning elements within a container. It allows elements to be moved to the left or right, which can have various layout and design effects.

The primary purposes of the `float` property are:

  1. Text Wrapping
  2. Creating Columns
  3. Alignment

Text Wrapping: One common use of the `float` property is to make text wrap around an element, such as an image or a block of text. When an element is floated, text content will flow around it instead of overlapping it.

Example:
<html>
<head>
<style>
img {
float: left;
width: 120px;
height: 120px;
margin-right: 15px;
}
</style>
</head>
<body>
<p>In this example, the image will float to the left in the text, and the text in the paragraph will wrap around the image.</p>
<p><img src="insert image here" alt="Give name for image">
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.</p>
</body>
</html>
Output:

In this example, the image will float to the left in the text, and the text in the paragraph will wrap around the image.

Flower 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.



Creating Columns: The `float` property can be used to create multi-column layouts by floating multiple elements side by side. This is often used in magazine-style layouts or when you want to divide content into columns.

Example:
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.column {
float: left;
width: 40%;
padding: 10px;
}
</style>
</head>
<body>
<div class="row">
<div class="column" style="background-color:#aaa;">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#bbb;">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
</div>
</body>
</html>
Output:

Column 1

Some text..

Column 2

Some text..


Alignment:You can use `float` to align elements horizontally within a container, either to the left or right.

Example:
<html>
<head>
<style>
img {
float: right;
width: 120px;
height: 120px;
margin-right: 15px;
}
</style>
</head>
<body>
<p>In this example, the image will float to the right in the text, and the text in the paragraph will wrap around the image.</p>
<p><img src="insert image here" alt="Give name for image">
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.</p>
</body>
</html>
Output:

In this example, the image will float to the right in the text, and the text in the paragraph will wrap around the image.

Flower 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.