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
1
vote
4 answers

How to clearfix without markup?

I need to find a way to clearfix a set of floated elements without extra markup. For example, I have a 2 columns grid in section#main_features. Each div.feature has width: 50% and is float: left. What I want is to find a way to clearfix the rows…
Bernardo
  • 519
  • 5
  • 16
1
vote
2 answers

Chrome and Firefox render positioned layout with negative margin differently - how to prevent?

I have a very specific layout to achieve with HTML/CSS. Basically I have a text container with a couple of left-floated boxes within. I then have a footer which needs to overlap this text container by a few pixels at the bottom. I'm achieving…
Dan
  • 5,836
  • 22
  • 86
  • 140
1
vote
2 answers

Using clearfix when there's nothing to fix?

It is clear to me (pun intended) that the two best ways to implement clearfix is either through Nicolas Gallager's micro-version .cf { *zoom: 1; }, or using overflow: hidden. But should either clearfix be used when there's nothing to fix? (Pun…
Baumr
  • 6,124
  • 14
  • 37
  • 63
1
vote
3 answers

Could this be the new clearfix?

Is there a more simplified way to use .clearfix? old: .clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; } My new clear fix: .coreys-clearfix:after { content: ""; …
Corey Trombley
  • 136
  • 1
  • 9
1
vote
0 answers

How do I clear:left, while keeping my float:left intact?

I have a jquery script that relies on the height of 'ul' to resize its containing 'div'. Without 'clear:left' on my 'li' elements, their parent 'ul' has a height of one 'li'. With 'clear:left' on my 'float:left' 'li' elements, the structure is…
Michael Ray-Von
  • 125
  • 2
  • 9
1
vote
2 answers

Why is this happening? Think it is an issue with clearfix?

Here is the issue: As you can see on the left the floated elements do not align, and on the right they do. https://i.stack.imgur.com/Qx8Zs.jpg This doesn't happen all the time, and it isn't browser specific (AFAIK) - it corrects itself upon…
Joe McKie
  • 96
  • 1
  • 9
1
vote
3 answers

CSS clearfix how to over come the inability to center an element using it

Ok I am remastering a site for one of my clients and for years I have been doing things a little old fashion as far as CSS goes. Ive read up on some things and found the method referred to as clearfix, and I have been wanting to give it a shot since…
chris
  • 36,115
  • 52
  • 143
  • 252
1
vote
4 answers

What's causing my header to move about?

I'm working on a simple layout that requires a header, footer and main content in between. However, something is bugging me a little. If I include an image beneath my header http://www.reversl.net/before/ everything sits exactly where I want. But if…
Jedda
  • 1,007
  • 5
  • 14
  • 26
1
vote
1 answer

css positioning and order of objects

I'm trying to tweak my site and have been sitting for a bit too long at the computer hence I do not know where my mistake is. Hoping someone could help out a person with basic knowledge of css. Here is my css: #body {float: left; width: 100%;…
bikeechick
  • 65
  • 1
  • 7
0
votes
2 answers

Jquery tabbed navigation causing floating issues with elements below

I've been having a huge headache trying to figure out why the facebook comments box is floating so high up right under the tabs of the tabbed navigation. I've tried multiple variations of the clearfix for floating elements to no avail, I've figured…
James
  • 17
  • 6
0
votes
3 answers

Floating divs in the absolute div are not expanding the parent. Clearfix doesn't seem to work

I have this issue: two floating divs inside an absolute-positioned parent div. CSS: .wrapper { position: absolute; left: 0; top: 0; overflow: hidden; /* doesn't do anything! */ } .left { float: left; } .right { float:…
Vlad
  • 2,475
  • 21
  • 32
0
votes
1 answer

Clearfix not making div's height expand to fit content

I have a div which contains 2 child elements. The 1st div have content added dynamically via jQuery on user interaction. The 2nd is a text area which have a problem not getting contained within its parent div.
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
0
votes
2 answers

Having an issue with clear fix not working with 960 grid system

I was building a mockup with 960 Grid System and ran into an issue with .clearfix. I'm not a fan of having extra divs in my code to trigger clear and in ALL the documentation around .clearfix seems to presented as the solution. 960gs also comes with…
QueueHammer
  • 10,515
  • 12
  • 67
  • 91
0
votes
1 answer

Jquery how to find floating elements in DOM?

Jquery how to find floating elements in DOM? float: left/right Clearfix is related to element float. That is not needed in our case. How to find which elements floating in DOM?
eastwater
  • 4,624
  • 9
  • 49
  • 118
0
votes
2 answers

Height not actually changing hieght while floating

Right now I'm coding a menu that has a two column layout. This is the code. HTML: replit
Ship
  • 3
  • 5