Academic Block

CSS Float Get Certified

What is CSS Float?

The CSS float property is used to position an element to the left or right of its container, allowing surrounding content such as text or images to wrap around it. Before Flexbox and Grid became popular, float was widely used to create webpage layouts. Today, it is mainly used for wrapping text around images and creating simple side-by-side elements.

Why Use CSS Float?

The float property helps create attractive text and image layouts. It is commonly used for image galleries, newspaper-style articles, sidebars, and wrapping text around images. Although modern layouts usually use Flexbox or Grid, understanding float is still important because many websites continue to use it.

Common Float Values

Value
Description
Common Use
left
Moves the element to the left.
Images, sidebars
right
Moves the element to the right.
Images, buttons
none
Default value. No floating occurs.
Normal layout
inherit
Inherits the float value from its parent.
Inherited layouts

Basic Float Syntax

What this code does: Floats a small image to the left so the paragraph text wraps around it.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    float: left;
    margin-right: 15px;
    border: 3px solid #007BFF;
    border-radius: 6px;
}
.demo-text {
    line-height: 1.7;
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/120" alt="Sample image">
<p class="demo-text">
This text wraps around the image because the image is floated to the left. As the text grows longer than the image is tall, the extra lines return to the full width of the container and flow underneath the picture.
</p>
</body>
</html>

Step 1: Float an Image to the Left

Using float:left moves the image to the left side while allowing text to wrap around it.

What this code does: Moves the image to the left with float: left and adds a right margin so the wrapped text does not touch it.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    float: left;
    margin-right: 15px;
    border: 3px solid #007BFF;
    border-radius: 6px;
}
.demo-text {
    line-height: 1.7;
}
.demo-clearfix {
    clear: both;
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/150" alt="Sample image">
<p class="demo-text">
This paragraph automatically wraps around the image because the image is floated
to the left. Keep adding text and you will see it flow under the image once it
passes the bottom edge of the picture.
</p>
<div class="demo-clearfix"></div>
</body>
</html>

Step 2: Float an Image to the Right

The float:right value moves the image to the right while text flows on the left side.

What this code does: Moves the image to the right with float: right and adds a left margin so the text on its left has breathing room.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    float: right;
    margin-left: 15px;
    border: 3px solid #28a745;
    border-radius: 6px;
}
.demo-text {
    line-height: 1.7;
}
.demo-clearfix {
    clear: both;
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/150" alt="Sample image">
<p class="demo-text">
The image is placed on the right, and the text wraps neatly around it on the left
side. Keep adding text and you will see it flow under the image once it passes the
bottom edge of the picture.
</p>
<div class="demo-clearfix"></div>
</body>
</html>

Step 3: Create Two Floating Boxes

Two elements can be placed side by side by floating one or both of them.

What this code does: Places two blue boxes side by side by floating both to the left with a width of 43% each.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-box {
    float: left;
    width: 43%;
    padding: 20px;
    margin: 10px;
    background: #007BFF;
    color: white;
    border-radius: 6px;
    text-align: center;
    box-sizing: border-box;
}
.demo-clearfix {
    clear: both;
}
</style>
</head>
<body>
<div class="demo-box">Box One</div>
<div class="demo-box">Box Two</div>
<div class="demo-clearfix"></div>
</body>
</html>

Step 4: Clear Floated Elements

The clear property prevents elements from wrapping around floated elements. It is commonly used after floated layouts.

What this code does: Floats a green box left and a red box right, then uses clear: both so the last line of text drops below both boxes.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-left {
    float: left;
    width: 45%;
    padding: 20px;
    background: #28a745;
    color: white;
    border-radius: 6px;
    text-align: center;
    box-sizing: border-box;
}
.demo-right {
    float: right;
    width: 45%;
    padding: 20px;
    background: #dc3545;
    color: white;
    border-radius: 6px;
    text-align: center;
    box-sizing: border-box;
}
.demo-clear {
    clear: both;
    padding-top: 20px;
}
</style>
</head>
<body>
<div class="demo-left">Left Box</div>
<div class="demo-right">Right Box</div>
<div class="demo-clear">
This text appears below both floating boxes.
</div>
</body>
</html>

Step 5: Float Navigation Links

Float can also arrange navigation links horizontally. However, Flexbox is generally recommended for modern navigation menus.

What this code does: Floats four blue links to the left so they sit in one horizontal row like a simple menu.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-link {
    float: left;
    padding: 14px 20px;
    background: #007BFF;
    color: white;
    text-decoration: none;
    margin-right: 5px;
    border-radius: 4px;
}
.demo-link:hover {
    background: #0056b3;
}
.demo-clearfix {
    clear: both;
}
</style>
</head>
<body>
<a class="demo-link" href="#">Home</a>
<a class="demo-link" href="#">About</a>
<a class="demo-link" href="#">Services</a>
<a class="demo-link" href="#">Contact</a>
<div class="demo-clearfix"></div>
</body>
</html>

Complete CSS Float Example

What this code does: Builds a small article layout with a floated image on the left and a heading and paragraph that wrap around it.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    float: left;
    margin-right: 20px;
    border-radius: 6px;
    border: 3px solid #007BFF;
}
.demo-article {
    line-height: 1.8;
}
.demo-clearfix {
    clear: both;
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/180" alt="Sample image">
<div class="demo-article">
<h2>CSS Float Example</h2>
<p>
The image is floated to the left while the paragraph wraps around it naturally.
This technique is commonly used in blog posts and news articles.
</p>
</div>
<div class="demo-clearfix"></div>
</body>
</html>

Advantages of CSS Float

Advantage
Benefit
Description
Text Wrapping
Better Layout
Allows text to wrap around images naturally.
Simple Layouts
Easy Design
Creates basic side-by-side layouts.
Wide Browser Support
Compatible
Works in all modern browsers.
Useful for Images
Professional Look
Improves articles and blog layouts.

Best Practices

  • Use Float mainly for wrapping text around images.
  • Always clear floated elements using the clear property or a clearfix.
  • Add margins around floated elements to improve spacing.
  • Use Flexbox or Grid instead of Float for complex webpage layouts.
  • Keep floated elements inside their parent containers.
  • Test layouts on different screen sizes.
  • Use meaningful class names for floated elements.
  • Keep your CSS clean and avoid unnecessary floating.

Web Resources on CSS Float

1. MDN Web Docs : float CSS property
2. MDN Web Docs : Test your skills, Floats
3. Dev.to : Here’s how floats work in CSS
4. CSS Tricks.com : Is CSS float deprecated?

๐Ÿงช Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

๐Ÿ“ CSS Code
๐Ÿ‘๏ธ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.