Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

Clear

When utilizing the float property, if we aim to position the next element below (not to the right or left), we employ the clear property.

Clear determines the behavior of an element positioned adjacent to a floating element.

The clear property options are:

  • none: The element remains unaffected by adjacent floated elements. This is the default behavior.
  • left: The element is positioned below left-floated elements.
  • right: The element is positioned below right-floated elements.
  • both: The element is positioned below both left and right-floated elements.
  • inherit: The element inherits the clear value from its parent.

For proper float clearing, align the clear property with the float: If an element is floated to the left, clear to the left. This ensures the floated element retains its position, while the cleared element appears below it on the webpage.

Example

In this example, the float is cleared to the left. Consequently, the <div2> element is positioned beneath the left-floated <div1> element.

div1 {
  float: left;
}

div2 {
  clear: left;
}

The clearfix Hack

When a floated element exceeds the height of its container, it extends beyond the container’s boundaries. To address this issue, we can apply a clearfix hack.

IMG_3515

Example

.clearfix {
  overflow: auto;
}

The overflow: auto clearfix is effective as long as you can manage margins and padding to avoid unintended scrollbars. However, the newer, modern clearfix hack is considered safer and is commonly employed in most web pages. The following code snippet is widely used for this purpose:

Example

.clearfix::after {
  content: “”;
  clear: both;
  display: table;
}
You’ll delve deeper into the ::after pseudo-element in a subsequent chapter.