Back to CSS Notes
Topic #76

Clear / Clearfix

Clear and clearfix stop elements from wrapping around floats. They are the classic fixes for collapsed float containers.


The CSS clear Property

The clear property specifies what should happen with the element that is NEXT to a floating element.

The clear property prevents elements from wrapping around or beside the floated content.

This property can have one of the following values:

  • none - Default. Allows elements to float on either side
  • left - The element is pushed below floated elements on its left side
  • right - The element is pushed below floated elements, on its right side
  • both - The element is pushed below floated elements, on both its left and right side
  • inherit - The element inherits the clear value from its parent

Example

div1 {
  float: left;
}

div2 {
  clear: left;
}

The CSS Clearfix Hack

If a floated element is taller than the containing element, it will "overflow" outside of its container. We can then add a clearfix hack to solve this problem:

The clearfix hack involves the ::after pseudo-element, and is used to ensure that the parent container properly encloses its floated child elements:

Example

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

Example explained:

  • .clearfix::after - Targets a pseudo-element that is generated after the content of any element with class="clearfix"
  • content: "" - Ensures the pseudo-element is rendered, even if it has no visible content
  • clear: both - This clears both left and right floats, effectively pushing any following content below the floated elements, and forces the parent container to expand to include them
  • display: table - This creates a new block formatting context, which helps in containing floats

Note: You will learn more about the ::after pseudo-element in a later chapter.


CSS Properties

Property Description
clear Specifies what should happen with the element that is next to a floating element
float Specifies whether an element should float to the left, right, or not at all

Want to go beyond the notes?

Join Coding Now Tech Institute's CSS course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Clear / Clearfix – FAQs

Quick answers about learning Clear / Clearfix in CSS.

This free note from Coding Now Tech Institute explains Clear / Clearfix in CSS — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every CSS topic on Coding Now Tech Institute, including Clear / Clearfix, is 100% free with no signup required.
With focused practice, most students grasp Clear / Clearfix in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now