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
231 questions
1044
votes
15 answers

How do you keep parents of floated elements from collapsing?

Although elements like
s normally grow to fit their contents, using the float property can cause a startling problem for CSS newbies: If floated elements have non-floated parent elements, the parent will collapse. For example:
Nathan Long
  • 113,812
  • 91
  • 316
  • 418
1043
votes
9 answers

What is a clearfix?

Recently I was looking through some website's code, and saw that every
had a class clearfix. After a quick Google search, I learned that it is for IE6 sometimes, but what actually is a clearfix? Could you provide some examples of a layout…
H Bellamy
  • 20,906
  • 22
  • 68
  • 110
879
votes
29 answers

What methods of ‘clearfix’ can I use?

I have the age-old problem of a div wrapping a two-column layout. My sidebar is floated, so my container div fails to wrap the content and sidebar.
There seem to be…
Chris J Allen
  • 18,148
  • 20
  • 71
  • 109
86
votes
3 answers

What does the clearfix class do in css?

I've seen div tags use a clearfix class when it's child divs use the float property. The clearfix class looks like this: .clearfix:after { clear: both; content: "."; display: block; height: 0; visibility: hidden; } .clearfix { …
Gnijuohz
  • 3,174
  • 5
  • 27
  • 47
61
votes
3 answers

Understanding Bootstrap's clearfix class

.clearfix { *zoom: 1; &:before, &:after { display: table; content: ""; // Fixes Opera/contenteditable bug: // http://nicolasgallagher.com/micro-clearfix-hack/#comment-36952 line-height: 0; } &:after { clear: both; …
Lanston
  • 9,146
  • 8
  • 28
  • 36
49
votes
8 answers

Is clearfix deprecated?

You are aware of the age-old problem: Containers containing floated elements don't automatically expand their height to enclose their children. One approach to fix this is the "clearfix" which adds a number of CSS rules to ensure a container…
Pekka
  • 418,526
  • 129
  • 929
  • 1,058
35
votes
6 answers

How do I make a newline after a twitter bootstrap element?

I apologize because this seems like such a simple thing. What's the correct way to insert a newline in a way that the next element is on a newline? It seems like some things automatically do this (such as

within `) but I'm seeing behavior where…

Danny
  • 3,440
  • 11
  • 33
  • 45
32
votes
1 answer

ClearFix vs Overflow

Its the standard float issue. You have a bunch of floating elements inside a parent container div. Since the childs are floating, the parent doesnt expand to include all of them. I know about the clearfix solution as well as setting the overflow…
Rajat
  • 29,134
  • 17
  • 60
  • 84
27
votes
1 answer

Clearfix with twitter bootstrap

I have an issue with twitter bootstrap that looks a little bit strange to me. I have a sidebar with fixed with at the left side and a main area.
SebastianStehle
  • 2,245
  • 1
  • 20
  • 30
16
votes
2 answers

Issue with class clearfix in bootstrap

I am using twitter bootstrap, i have an issue with clearfix class in bootstrap. My html is :
This is a text

What i am expecting is horizontal line should come in next line of displayed text but…
user1740381
  • 2,047
  • 8
  • 34
  • 60
12
votes
2 answers

960 grid's clearfix vs HTML5 Boilerplate's clearfix - What's the difference?

960 grid's clearfix vs HTML5 Boilerplate's clearfix - What's the difference? Here's the clearfix found in Nathan Smith's 960 grid's css: /* http://www.yuiblog.com/blog/2010/09/27/clearfix-reloaded-overflowhidden-demystified…
Johnny
  • 3,631
  • 6
  • 29
  • 36
10
votes
2 answers

Left Align Label - Right Align Select Element (CSS)

I have a form layout that I want to display the label aligned left and the form control aligned right. I have been trying to get it to work using a float:right on the form control (in this case a ) and then applying the clearfix class to it but the…
Barbs
  • 1,065
  • 2
  • 13
  • 28
10
votes
4 answers

Sass / SCSS Mixin for Clearfix - best approach?

I want to remove the clearfix class from my HTML and include a clearfix mixin in my SCSS (Rails 3.1 application). What is the best approach to this? I am thinking of just taking the HTML 5 Boilerplate clearfix and turning it into a mixin, then…
Richard Jordan
  • 7,788
  • 2
  • 35
  • 45
9
votes
2 answers

clearfix issue with

I use clearfix to clear the float. But the problem is ,there is a different height in
  • and
    . li.clearfix height is 32px, but div.clearfix height is 18px. when I delete .clearfix:before , they are all the same. But, when tried in bootstrap,…
    user3114590
    • 121
    • 1
    • 5
  • 8
    votes
    5 answers

    Is there a way to use the clearfix hack alongside flexbox?

    I've been using flexbox for layouts, with CSS floats as a fallback for older browsers. On the whole this works well, since browsers that understand display: flex will ignore float on any flex items. However, the one place that I've run into a…
    Nick F
    • 8,686
    • 5
    • 66
    • 84
    1
    2 3
    15 16