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
8
votes
2 answers

Clearfix with absolute positioned elements

My problem is the following: The border does not wrap the containing items. I know this is because I position the content-item absolute, but I need them to be absolute for the layout to work. This also means that I cannot use the clearfix solution…
Rick Hoving
  • 3,585
  • 3
  • 29
  • 49
7
votes
2 answers

Why overflow:hidden expands parent element (containing floated child elements)?

In short: Basically, I just want to know why overfow:hidden explands the container containing a floated item. Shouldnt it hide the overflowing element like in this image http://css-tricks.com/wp-content/csstricks-uploads/css-overflow-hidden.png why…
masterpiece
  • 573
  • 3
  • 10
  • 19
6
votes
3 answers

HTML/CSS: Clear floating elements in the middle without adding unneeded tags

Most of the clearfix techniques out there involves adding things at the very bottom of the parent container, and I prefer the pseudo element approach the most since it doesn't add unneeded elements into the HTML. However, recently I found that I am…
Xavier_Ex
  • 8,432
  • 11
  • 39
  • 55
5
votes
1 answer

Why
used?

I'll adapt my self to what 52framework.com offers. (HTML5, CSS3, JS framework) Despite watching grid tutorial video and inspecting other demo source codes, I couldn't understand why framework used
code at 12th…
Andre Chenier
  • 1,166
  • 2
  • 18
  • 37
5
votes
2 answers

The modern way to clear floated content?

What's the modern way to clear floated content these days? There's the "recent" modern way of adding a ".clearfix" on the parent element to clear the contained floats and that would work great. In fact, this is my favorite method and still use this…
Daniel Fischer
  • 3,042
  • 1
  • 26
  • 49
4
votes
0 answers

Unknown offset. (inline-flex element + clearfix)

Unknown offset. In latest Chrome, FF, IE Explain please, what the top offset (1em) ? JSFiddle #a { border:1px solid blue; } #b { display: -webkit-inline-box; display: -webkit-inline-flex; display: -ms-inline-flexbox; …
Alvego
  • 336
  • 3
  • 7
4
votes
2 answers

twitter bootstrap clearfix push down

I'm using twitter bootstrap 3, to design a layout, which has two columns, a float sidebar and a main content with a left margin relative to the sidebar size, but when I use the clearfix class in an ul element inside the main content, seems that the…
dhamaso
  • 55
  • 1
  • 6
4
votes
1 answer

CSS Wrapper / Clearfix Resolution

Been having issues trying to get my floated elements to extend with all of my content. I've done some research but I have become officially stumped/exhausted. I've attempted a few clearfix resolutions, standard, micro, and inline, essentially…
4
votes
1 answer

Applying a clearfix to nth-child without additional markup

First up, for extreme clarity, here a JS fiddle demonstrating what I'm trying to achieve: http://jsfiddle.net/bb_matt/VsH7X/ Here's the explanation - my rationale: I'm creating a responsive site using the 1140 grid framework. It's a fairly complex…
3
votes
5 answers

"Best clearfix ever?"

I saw this rather different method for clearfix here: http://www.marcwatts.com.au/blog/best-clearfix-ever/ It proposes adding the following CSS code which automates clearfix and does not require you to add a 'clearfix' or similar class to the…
Jake Petroules
  • 23,472
  • 35
  • 144
  • 225
3
votes
1 answer

What is the most reliable clearfix method for HTML in emails?

clear doesn't work in Outlook 2007/2010, as well as display (and many other CSS properties). What should I use? I need to support all of the clients in this matrix, except Lotus Notes. FYI: I am using an XSL that is building all grid-based markup…
David Rivers
  • 2,896
  • 1
  • 31
  • 39
3
votes
3 answers

What might cause this CSS margin sharing(?) bug in Firefox to occur?

I ran into this unusual Firefox-only (as far as I know - I only checked against Safari and Chrome, and was using Firefox 3.6) CSS bug today at work, and managed to reproduce the problem with a much smaller snippet of code, here:
Johnny
  • 698
  • 8
  • 21
3
votes
7 answers

Bootstrap grid is not correctly aligning

On my search results page I have 2 posts per row (col-md-6). The grid works fine and everything is aligned correctly, except when the excerpt or the post title is longer than the other excerpts or post titles. On my test site I have all of the…
user6738171
  • 1,009
  • 2
  • 15
  • 50
3
votes
1 answer

Nested clearfix with fluid width contents

Consider this case: There is a layout with fluid width content, and fixed sidebar floated to the right. Container of said layout is using clearfix to clear right float Inside the content block there is another block doing the same. This second…
Max
  • 1,149
  • 3
  • 10
  • 20
3
votes
2 answers

display: block; VS display: table;

I can't spot the difference between display: block; and display: table;. For example, when writing clearfix, the following two CSS rule seems to behaviour identical: Using display: block; .clearfix:after { content: ""; display: block; …
nalzok
  • 14,965
  • 21
  • 72
  • 139
1
2
3
15 16