Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Created July 28, 2010 00:07
Show Gist options
  • Save chriseppstein/493085 to your computer and use it in GitHub Desktop.
Save chriseppstein/493085 to your computer and use it in GitHub Desktop.

This is how you can do truly semantic clearfixing easily in Sass.

The @extend directive is used to inherit the .group class for other selectors. Sass takes care of spreading the selectors around for you.

.group:after {
visibility: hidden;
display: block;
clear: both;
height: 0;
font-size: 0;
content: " ";
* html & {
height: 1%;
}
*:first-child+html & {
min-height: 1px;
}
}
#content,
#sidebar,
#some .other .thing {
@extend .group;
}
.group:after,
#content:after,
#sidebar:after,
#some .other .thing:after {
visibility: hidden;
display: block;
clear: both;
height: 0;
font-size: 0;
content: " ";
}
* html .group:after,
* html #content:after,
* html #sidebar:after,
* html #some .other .thing:after,
#some .other * html .thing:after {
height: 1%;
}
*:first-child + html .group:after,
*:first-child + html #content:after,
*:first-child + html #sidebar:after,
*:first-child + html #some .other .thing:after,
#some .other *:first-child + html .thing:after {
min-height: 1px;
}
@andyford
Copy link

I suppose it's technically harmless, but does it really compile the ie6 and ie7 filter hacks by doubling up on them?

The compiled output shows:

  • html #some .other .thing:after,
    #some .other * html .thing:after
    and
    *:first-child + html #some .other .thing:after,
    #some .other *:first-child + html .thing:after

@chriseppstein
Copy link
Author

Yes in the general case complex extends must be permuted to account for various possible DOM structures. However I think we can safely add a heuristic around selectors that mention html and body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment