Floats and clear
Definition and purpose
A float moves toward an edge of its containing block and permits nearby inline content to wrap around it. Clear requests that an element move below relevant preceding floats.
Left float and clearance
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a { float: left; width: 60px; height: 70px; } #b { clear: left; } </style> </head> <body> <div id="a">Float</div> <div id="b">Following</div> <div id="d">Footer</div> </body></html>B begins below A.
Syntax and mental model
float: left or right positions a float; clear: left, right or both applies clearance on the chosen side. Floats still influence line layout, unlike an absolutely positioned overlay. They are useful for text wrapping rather than general page layout.
Right float
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; } #a { float: right; width: 60px; height: 40px; } #b { clear: both; } </style> </head> <body> <div id="a">Float</div> <div id="b">Following</div> <div id="d">Footer</div> </body></html>Both-side clearance also clears a right float.
Common mistakes
Do not confuse clear with deleting a float. It changes the position of the clearing box, not the earlier float. Use flow-root when the parent needs to enclose floats.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Block formatting contexts. Next lesson: Display contents and hidden boxes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Floats and clear — knowledge check
Five questions on floats and clear.
What can wrap beside a float?
Which clears either side?
Does clear remove the earlier float?
Which property helps a parent contain floats?
What is a sensible modern use of float?