Code has been added to clipboard!

CSS Clear: Learn to Clear Floating Elements

Reading time 2 min
Published Sep 30, 2019
Updated Feb 10, 2020

TL;DR — CSS clear is for indicating whether HTML elements should move below the floating elements that come before it. In other words, clear specifies which sides of a floating element should not float.

Clearing floats

The CSS clear controls the behavior of the floating element by preventing the overlapping of consecutive elements over the floating element.

The value of the property clear specifies the side on which the floating element is not supposed to float. The values of clear property can be none, left, right, both, inherit, inline-start, and inline-end.

In the example, we make CSS clear floating elements on the left:

Without clear

div2 - Notice that div2 is after div1 in the HTML code. However, since div1 floats to the left, the text in div2 flows around div1.

 

With clear

Here, clear: left; moves div4 down below the floating div3. The value "left" clears elements floated to the left. You can also clear "right" and "both".
Example
div {
   clear: left;
}

Note: when you specify the same direction for CSS clear and float, the element moves below the floated element.

What clearfix is

When you need to make CSS clear floats, it is useful to apply CSS clearfix. For instance, when containers have only floated elements, they do not wrap around them and collapse. The CSS clearfix makes sure that the container wraps floated items by clearing children elements automatically.

Here is the syntax of CSS clearfix:

Example
#container::after { 
  content: "";
  display: block; 
  clear: both;
}

Note: the CSS clearfix works with the ::after pseudo element.

Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index