Questions tagged [clearfix]

A clearfix is a way for an element to automatically clear after itself, so that you don't need to add additional markup. It's generally used in float layouts where elements are floated to be stacked horizontally.

Description

A clearfix is a way for an element to automatically clear after itself, so that you don't need to add additional markup. It's generally used in float layouts where elements are floated to be stacked horizontally.

The clearfix is a way to combat the zero-height container problem for floated elements.


Examples

.clearfix:after {
    content: " ";
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
}

Normally you would need to do something as follows:

<div style="float: left;">Sidebar</div>
<div style="clear: both;"></div> <!-- Clear the float -->

With clearfix, you only need to

<div style="float: left;" class="clearfix">Sidebar</div>
<!-- No Clearing div! -->

References

  1. StackOverflow - What is clearfix?
  2. Article by Chris Coyer @ CSS-Tricks
  3. A new micro clearfix hack
  4. An Overview of Different clearfix Methods
230 questions
3
votes
2 answers

Bootstrap content doesn't lay out correctly in conjunction with fixed width sidebars

There is a large white gap between the first row of columns and the second row. JSFiddle: https://jsfiddle.net/5o3ug26g/
W.T
  • 33
  • 4
3
votes
1 answer

CSS clear float with :after element not working

I can't seem to get this right. It's quite simple, clear the float after the h4 element (widget-title), so that the text box aligns left on a new line. The HTML cannot be changed. Here is the fiddle: http://jsfiddle.net/j29ubvjw/ .widget-wrap { …
Kevin M
  • 1,202
  • 15
  • 30
3
votes
1 answer

Difference (Firefox vs Chrome) in height of absolute positioned element with float, clearfix, negative margin inside

I encountered an issue with difference in rendering an element, which is absolute positioned, containing another element inside negative margins, then inside another one with clearfix and lastly some floated element in it. Test case:…
hawk
  • 299
  • 2
  • 8
3
votes
3 answers

CSS clearfix not working

This (http://jsfiddle.net/77RRA/1/) is working, while this (http://jsfiddle.net/77RRA/) is not. Isn't clearfix supposed to substitute the line
?
user369246
2
votes
2 answers

Inline clearfix before in css

I would like to break the line automatically before a tag begins. I'd try a couple of things, but can't find out how to do so. I tried: strong, b {font-weight: bolder;clear:both;display:block;} and strong:before, b:before…
Maarten Klok
  • 215
  • 3
  • 20
2
votes
3 answers

float-css overlaps next to div image

My logo and navigation float to the left and right respectively when the screen gets to 40rem. However it overlaps with the next div which contains a background image. I tried setting margins but that just moved both logo/nav and the image lower on…
ankit
  • 125
  • 4
  • 13
2
votes
4 answers

Is there a non-trivial purpose for defining a css style for HTML[xmlns]?

Note this clearfix solution offered here. Why is there a separate style defined for HTML[xmlns]? Is this a CSS hack designed to target a specific browser? UPDATE: Here is the code in question, since some of the answers are obviously way off the mark…
Ovesh
  • 5,209
  • 11
  • 53
  • 73
2
votes
1 answer

How to correctly add in a row to bootstrap to fix grid system moving onto multiple lines

I am using bootstrap to display the posts on my front page. I have my posts alternate between 2 rows of 3 posts and 1 large post. Everything looked good except for the fact that I noticed that if there is a longer title or excerpt in one of the…
user6738171
  • 1,009
  • 2
  • 15
  • 50
2
votes
1 answer

Why top margins are collapsing but bottom margins are not?

As you can see in the following demo, in left-column, the upper padding edge of .clearfix-using_display-table(yellow part) and that of .clearfix-using_display-table p(silver part) touch each other. However, on the lower edge, for some reason, two…
nalzok
  • 14,965
  • 21
  • 72
  • 139
2
votes
1 answer

Html5 and Css3 - Horizontal navigation bar issues

I am designing a webpage. I have two levels of navigation, the social media buttons on the first level and later a navigation bar. I am having issues getting the background colour of the navigation bar to go the complete length of the webpage, at…
2
votes
2 answers

Clearfix: Height 1%

In the premium templates on ThemeForest, I see that many templates adopt this method to clearfix: .clearfix { height: 1%; } And add the class clearfix to various elements. Why height 1%? Is a good way?
Keaire
  • 879
  • 1
  • 11
  • 30
2
votes
3 answers

:nth-child(3n):after cleafix not working in CSS

Hello there I have tried the following which I think should be valid CSS and it does not work (tested with Google Chrome).
2
votes
2 answers

Divs not auto resizing height inside another div

The solution has been found. The code is below. Thank you to everyone who helped. The Solution was to clear the float, the code below fixed it for me. .div#content:after { content:""; display:table; clear:both; } Thanks Again. Original Question I…
2
votes
2 answers

Clearfixing floats inside the container but ignoring floats outside the container?

I've got the following HTML structure: %aside.sidebar %main %article %h2 %aside.picture %img %p %article %h2 %aside.picture %img %p (If you're not familiar with HAML: the structure above represents an HTML…
Andrey Mikhaylov - lolmaus
  • 23,107
  • 6
  • 84
  • 133
2
votes
1 answer

CSS Float Left and Clearfix not working together

I have a simple image gallery dynamically controlled by PHP to add and remove image thumbnails from a page. The problem I have is that I want the images to be aligned next to eachother until there is no room on that row and is forced to start…
SteppingHat
  • 1,199
  • 4
  • 19
  • 50
1 2
3
15 16