HTML & CSSEasy
What is clear?
clear stops an element from sitting next to earlier floated elements: the element is moved down below any floats on the specified side. Values are left, right, both, none, plus logical inline-start and inline-end.
How it works
clear: leftmoves the element below all left floats that come before it.clear: rightdoes the same for right floats.clear: bothmoves it below every preceding float.- It only affects floats earlier in the same block formatting context.
Typical uses
- A footer or the next section that must start below a floated sidebar or image.
- The classic clearfix to make a parent contain its floats:
.clearfix::after { content: ""; display: block; clear: both; }
The pseudo-element sits at the end of the parent and clears the floats, forcing the parent's height to include them.
Modern alternatives
display: flow-rooton the parent contains floats without any extra element or pseudo-element.- Flexbox and Grid do not use floats at all;
floatandclearhave no effect on flex and grid items.
Interview tip
Explain the difference between clearing (moving one element below floats) and containing floats (making a parent wrap them), and name flow-root as the modern way to contain them.
Code example
html
1<style>2 body { font-family: system-ui; max-width: 520px; }3 .box { border: 2px solid #0b1020; padding: 8px; margin-bottom: 12px; }4 .left { float: left; width: 110px; height: 60px; background: #dbeafe; margin-right: 8px; }5 .right { float: right; width: 110px; height: 90px; background: #fef9c3; margin-left: 8px; }6 .clear-left { clear: left; background: #dcfce7; }7 .clear-both { clear: both; background: #fee2e2; }8 .clearfix::after { content: ""; display: block; clear: both; }9</style>10<div class="box clearfix">11 <div class="left">float left</div>12 <div class="right">float right (taller)</div>13 <p>Regular text wraps between the floats.</p>14 <p class="clear-left">clear: left (moves below the left float only)</p>15 <p class="clear-both">clear: both (moves below every float)</p>16</div>17<p>The bordered box contains its floats thanks to the clearfix.</p>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.