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>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic float syntax demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles one image and one paragraph. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The selector .demo-img targets only the image that has this class. float: left removes the image from the normal top-to-bottom flow and pushes it to the left edge of its container. The text after it moves up beside the image and wraps around it.
Wrapped text would touch the image edge without a margin. margin-right: 15px adds a 15px gap on the side where the text flows. For a right float you would use margin-left instead.
The blue border makes the exact edge of the image visible, so you can see where the text stops. border-radius: 6px rounds the corners. The } closes the .demo-img rule.
line-height: 1.7 sets each line to 1.7 times the font size, which makes the wrapping easier to read. The paragraph itself is not floated. It stays a normal block, and only its text lines flow around the floated image.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content. The order of the two elements inside matters for floats.
The floated element must appear in the HTML before the text that should wrap around it. class="demo-img" applies the float, src loads a 120px sample photo, and alt describes the image for screen readers.
The paragraph box still spans the full width, but its lines of text are shortened to avoid the floated image. Once the text becomes taller than the image, the following lines use the full width again. Resize the preview or add more words to see this.
</body> closes the body. Note that this example has no clearfix, because the paragraph is longer than the image.
</html> is the final closing tag and ends the document.
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>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the float left demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo floats an image left and clears it afterwards. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
float: left pulls the image to the left edge of the page. Because it leaves the normal flow, the paragraph behaves as if the image were not there, except that its text lines avoid the image area.
margin-right: 15px keeps a 15px gap between the picture and the wrapped text. Margins on a floated element are included when the browser calculates where the text may start.
A 3px blue border shows where the image ends and border-radius softens the corners. This is only decoration and does not change how the float works.
line-height: 1.7 adds space between lines so the paragraph is easy to read next to the image.
.demo-clearfix is a helper class. clear: both tells the element to move below any left or right floats before it, so it sits underneath them instead of beside them. A cleared empty div also makes the parent container grow to include the floated items.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: an image, a paragraph and a clearfix element, in that order.
A 150px sample image with the demo-img class. It must come before the paragraph in the HTML, otherwise the paragraph would already be placed and could not wrap around the image.
The three lines of text are one paragraph. Its first lines sit to the right of the image. When the text is longer than the image is tall, the remaining lines flow underneath the picture and use the full width.
This empty div uses the clearfix class. Because it is placed after the floated elements, it drops below them and stops later content from wrapping around the float.
</body> closes the body. Everything the learner sees is inside this section.
</html> is the final closing tag and ends the document.
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>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the float right demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo floats an image to the right. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
float: right pushes the image to the right edge of the container. The text now starts at the left and wraps around the image on its left side.
The text is on the left of the image this time, so the gap goes on the image’s left side with margin-left: 15px. Always put the margin on the side that faces the text.
A green border and rounded corners make the image edge easy to see. They are decorative only.
line-height: 1.7 spaces the lines out so the shape of the wrap is clear.
.demo-clearfix is a helper class. clear: both tells the element to move below any left or right floats before it, so it sits underneath them instead of beside them. A cleared empty div also makes the parent container grow to include the floated items.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content. The image still comes before the paragraph, even though it appears on the right.
Even though the picture is displayed on the right, it is written before the paragraph in the HTML. Float moves it visually, but the source order still decides how text wraps.
The first lines are shortened to leave room for the image on the right. After the image ends, the lines return to full width.
This empty div uses the clearfix class. Because it is placed after the floated elements, it drops below them and stops later content from wrapping around the float.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the two floating boxes demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses one class for both boxes. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
float: left lets each box sit beside the previous one instead of stacking. width: 43% is needed because a floated block shrinks to fit its content. Two boxes at 43% leave room for their margins, so both fit on one row.
padding: 20px adds space inside the box around the text. margin: 10px adds space outside, which separates the boxes from each other and from the edges.
A blue background with white text makes the boxes stand out. border-radius rounds the corners and text-align: center centers the label.
box-sizing: border-box includes padding in the 43% width. Without it, the padding would be added on top, the boxes would become wider, and the second box could wrap onto a new line.
.demo-clearfix is a helper class. clear: both tells the element to move below any left or right floats before it, so it sits underneath them instead of beside them. A cleared empty div also makes the parent container grow to include the floated items.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: two boxes and a clearfix.
The first box floats to the left edge, because nothing is before it.
The second box floats left too, so it stops against the right edge of Box One and forms a row. If the container were too narrow for both, it would drop to the next line.
This empty div uses the clearfix class. Because it is placed after the floated elements, it drops below them and stops later content from wrapping around the float.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the clear property demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses three different classes. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
float: left pins the green box to the left. width: 45% gives it nearly half of the row.
Padding, a green background, white text, rounded corners and centered text style the box. box-sizing: border-box keeps the padding inside the 45% width.
float: right pins the red box to the opposite edge. The two 45% boxes leave a 10% gap between them.
The same layout properties as the left box, with a red background so the two boxes are easy to tell apart.
clear: both moves this element below any left float and any right float. Without it, the text would sit in the gap between the two boxes. padding-top: 20px adds space under the boxes.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: two floated boxes followed by a cleared text block.
This box is floated left and takes its place at the start of the row.
This box is floated right and is placed on the same row, as it fits beside the left box.
This block has clear: both, so the browser places it below the tallest float. Remove clear: both in the editor and watch the text move up between the boxes.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the float menu demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles links as menu buttons. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
Links are inline elements and normally sit on one line without proper box sizing. float: left turns each link into a block-level box and places them in a row.
padding: 14px 20px gives each link 14px above and below and 20px left and right, which makes it look like a button. Floating lets this vertical padding work. The blue background and white text give strong contrast.
text-decoration: none removes the default underline. margin-right: 5px separates the buttons and border-radius rounds the corners.
:hover applies when the mouse is over a link. A darker blue gives feedback that the item can be clicked.
.demo-clearfix is a helper class. clear: both tells the element to move below any left or right floats before it, so it sits underneath them instead of beside them. A cleared empty div also makes the parent container grow to include the floated items.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: four menu links and a clearfix.
Each link has the same class, so they share the same float and style. They appear in source order from left to right. href="#" keeps the demo from navigating away.
This empty div uses the clearfix class. Because it is placed after the floated elements, it drops below them and stops later content from wrapping around the float.
</body> closes the body.
</html> is the final closing tag and ends the document.
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>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the article layout demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles an image and an article block. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
float: left pulls the image to the left of the article, like a picture in a newspaper.
margin-right: 20px leaves a gap between the image and the article text. The border and rounded corners frame the picture.
line-height: 1.8 makes the text airy. This container is not floated, so its heading and paragraph wrap around the image.
.demo-clearfix is a helper class. clear: both tells the element to move below any left or right floats before it, so it sits underneath them instead of beside them. A cleared empty div also makes the parent container grow to include the floated items.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: an image, an article container and a clearfix.
A 180px image placed before the article, which makes it available for the article text to wrap around.
The article container holds the heading and paragraph. Its box spans the full width, but the text inside is pushed away from the floated image.
The heading starts beside the image, because the image is floated left.
The paragraph continues beside the image and flows below it when it is longer than the image.
</div> closes the article container.
This empty div uses the clearfix class. Because it is placed after the floated elements, it drops below them and stops later content from wrapping around the float.
</body> closes the body.
</html> is the final closing tag and ends the document.
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.