i

Learn HTML and CSS in 10 Days

Clearing and Containing Floats

Initially, the 'float' property was created to make the content to wrap around the images. An image was made to float, and all the contents that used to surround the image could then normally stream around it. Although the ‘float’ used to work great for the element, it was never really expected to be utilized for design and situating purposes. Hence, it accompanies a couple of pitfalls.

One of that pitfall is that every so often, the best possible styles won't render on an element that it is sitting by or is a parent element of a floated element. As we all know that when an element is floated, it is taken away from the original flow of the HTML page. This element's design around that floated element can be impacted negatively.

Often the value of the ‘padding’ and the ‘margin’ property aren't translated effectively, making them mix into the floated element; different properties can be influenced, as well.

Another major pitfall is that sometimes undesirable content starts to fold over a floated element. Getting rid of an element from the HTML document's flow offers all other element presents around the floated element to fold over and take up the available space around the floated element, which is often not needed.

In our previous example of 2 columns, after floating the  as well as the elements, and before providing the ‘width’ property value on both of them, the content that is present in the footer element would have enclosed in between the two floated element above it, occupying in any accessible space. Meanwhile the  element would have been placed in the trash between the  and  elements, taking up the space available.

To prevent content from folding over floated elements, we have to contain, or clear, those floats and set back the page to its ordinary stream.

Clearing Floats

One can use the 'clear' property to accomplish Clearing floats. It takes in values like: right, left, and both.

div {

  clear: left;

}

The 'right' value will clear the right floats, and the 'left' value will clear the left float. 'Both' can clear right as well as the left floatsand is regarded as the most ideal value.

In the previous example, if we are using the property of 'clear' with the value of 'both' on the footer, then we can clear the floats.

footer {

  clear: both;

}

Containing Floats

Apart from clearing floats, one more choice is to contain the floats. The result of clearing the floats as well as containing floats are almost the same; but, containing floats makes it sure that all styles will be properly rendered.

It is to be noted that the floated element must be placed within a parent element. Here the parent element will be acting as a container, leaving the progression of the archive typical outside of it. For that parent element, the CSS will be defined by the 'group' class, as shown below.

.group:before,

.group:after {

  content: "";

  display: table;

}

.group:after {

  clear: both;

}

.group {

  clear: both;

  *zoom: 1;

}

The:after and :before are pseudo-elements that are generated dynamically above and below the element with the 'group' class. Those elements do not contain any content and are viewed as table-level elements. It is much similar to block-level elements. The elements that are generated dynamically after the element with the 'group' class is clearing the floats inside the element with the 'group' class. In addition to that, the element having the 'group' class also clears any floats that are appearing above it.

CSS

.group:before,

.group:after {

  content: "";

  display: table;

}

.group:after {

  clear: both;

}

.group {

  clear: both;

  *zoom: 1;

}

section {

  float: left;

  margin: 0 1.5%;

  width: 63%;

}

aside {

  float: right;

  margin: 0 1.5%;

  width: 30%;

}

The procedure shown above for containing elements is known to be “clearfix” and can frequently be found in different sites with the class name of cf or clearfix.