clear
Example
.sidebar {
float: left;
}
.footer {
clear: right;
}.sidebar {
float: left;
}
.footer {
clear: both;
}'clear: right' only clears right floats, so the footer still wraps beside the left-floated sidebar, while 'both' moves it below any float.
Common mistakes
- Adding empty
<div style="clear: both">elements to fix layouts.
Key points
- Parents of floats contain them with
display: flow-rootinstead of an empty clearing<div>. clearis set on the element that should move below the float, not on the float itself.
Interview questions
What does clear: both do?
It moves the element below any left or right floats that come before it.
What is a clearfix?
An older hack using ::after with clear: both to make a parent wrap its floats. display: flow-root replaces it.