🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Clear

AI & DATA SCIENCE // clear

The clear property forces an element to move below any preceding floated elements on its specified side, rather than wrapping alongside them.

Syntax

clear: left | right | both | none;

Deep Dive Course

clear: left pushes an element down past any preceding left-floated elements, clear: right does the same for right-floated elements, and clear: both handles floats on either side, ensuring the cleared element starts on a fresh line below all preceding floats rather than wrapping next to them. It's commonly applied either to an element that should visually start below a floated image rather than wrapping beside it, like a footer that shouldn't overlap a floated sidebar, or as part of the classic clearfix technique, applying clear to a pseudo-element specifically to fix a parent container's height-collapsing issue when all its children are floated.

1Understanding Clear

clear: left pushes an element down past any preceding left-floated elements, clear: right does the same for right-floated elements, and clear: both handles floats on either side, ensuring the cleared element starts on a fresh line below all preceding floats rather than wrapping next to them. It's commonly applied either to an element that should visually start below a floated image rather than wrapping beside it, like a footer that shouldn't overlap a floated sidebar, or as part of the classic clearfix technique, applying clear to a pseudo-element specifically to fix a parent container's height-collapsing issue when all its children are floated.

💡

Use clear: both, rather than just clear: left or clear: right individually, unless you specifically know only one particular side has floated elements that need clearing — both is the safer, more universally applicable default.

editor.html
.sidebar {
  float: left;
  width: 200px;
}

.footer {
  clear: both;
}
localhost:3000

2Practical Example

Here is a real-world application of Clear showing how it is used in production CSS code.

editor.html
.clearfix::after {
  content: "";
  display: block;
  clear: both;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Clear:

1. Apply clear to an element that should start below preceding floated content rather than wrapping beside it, like a footer following a floated sidebar

2. Use clear: both as the safer general default, unless you specifically know only floats on one particular side need to be cleared

3. Apply clear via a ::after pseudo-element on a parent, the classic clearfix technique, specifically to fix that parent's height collapsing due to floated children, rather than adding an extra empty element purely for clearing

⚠️

Tip: Use clear: both, rather than just clear: left or clear: right individually, unless you specifically know only one particular side has floated elements that need clearing — both is the safer, more universally applicable default.

editor.html
.sidebar {
  float: left;
  width: 200px;
}

.footer {
  clear: both;
}
localhost:3000

Examples

Example 01Basic Usage
.sidebar {
  float: left;
  width: 200px;
}

.footer {
  clear: both;
}
Example 02Advanced Example
.clearfix::after {
  content: "";
  display: block;
  clear: both;
}

Best Practices

  • Apply clear to an element that should start below preceding floated content rather than wrapping beside it, like a footer following a floated sidebar
  • Use clear: both as the safer general default, unless you specifically know only floats on one particular side need to be cleared
  • Apply clear via a ::after pseudo-element on a parent, the classic clearfix technique, specifically to fix that parent's height collapsing due to floated children, rather than adding an extra empty element purely for clearing

Interview Question

Why does the classic clearfix technique specifically add clear to a pseudo-element, rather than just applying clear directly to the parent container that's collapsing?

Hint: Think about what clear actually does to the specific element it's applied to, versus what the actual goal is here — fixing that same element's own height, or something else entirely.

clear affects how the specific element it's applied to positions itself relative to preceding floated elements, forcing that element to move below them rather than wrap alongside — but applying clear directly to the collapsing parent container itself wouldn't accomplish anything useful here, since the parent isn't wrapping alongside its own floated children in the first place, that's a completely different relationship than what clear is designed to address. The actual goal of the clearfix technique is to give the parent some genuinely in-flow content after its floated children, content that itself gets cleared below those floats, so that the parent, in the process of properly containing that new in-flow, cleared content, is forced to extend its own height to actually contain it, indirectly solving the collapsing-height problem as a side effect. Using a ::after pseudo-element specifically accomplishes this without needing to add any extra, purely-presentational empty HTML element into the actual markup just for this technical clearing purpose, keeping the fix entirely and cleanly contained within the CSS itself.

Exercises

MediumPractice using Clear in a real scenario.
View Solution
.sidebar {
  float: left;
  width: 200px;
}

.footer {
  clear: both;
}

Frequently Asked Questions

Why does the classic clearfix technique specifically add clear to a pseudo-element, rather than just applying clear directly to the parent container that's collapsing?

clear affects how the specific element it's applied to positions itself relative to preceding floated elements, forcing that element to move below them rather than wrap alongside — but applying clear directly to the collapsing parent container itself wouldn't accomplish anything useful here, since the parent isn't wrapping alongside its own floated children in the first place, that's a completely different relationship than what clear is designed to address. The actual goal of the clearfix technique is to give the parent some genuinely in-flow content after its floated children, content that itself gets cleared below those floats, so that the parent, in the process of properly containing that new in-flow, cleared content, is forced to extend its own height to actually contain it, indirectly solving the collapsing-height problem as a side effect. Using a ::after pseudo-element specifically accomplishes this without needing to add any extra, purely-presentational empty HTML element into the actual markup just for this technical clearing purpose, keeping the fix entirely and cleanly contained within the CSS itself.

Related Functions

FloatPositionDisplay-flex