The clear and collapse fix, based on this clear fix, with added margin uncollapse:
.group:before, /* :before to uncollapse the top margin */
.group:after{
display: block;
clear: both; /* clear fix */
content: "a0 "; /* - just a space doesn't uncollapse margins */
visibility: hidden; /* make sure not to show anything */
height: 0;
}
.group{
zoom: 1; /* solves it all for IE < 8, and doesn't hurt other browsers */
}
Demo: jsFiddle, IE7 render with netrenderer
Note that content: "a0 ";
is equivalent to
and is used instead of a non-whitespace character (eg .
) so that when you select the block and copy it, you do not get extra visible characters, which otherwise happens in some browsers (for example IE9).
The drawbacks with this solution are:
:before
and :after
are defined, so special care needs to be taken if they are to be used.
- For every new selector you want to have this fix applied to you have to specify the selector 3 times.
- Not very short/trivial.
A similar solution is used by YUI, described in this article (but without
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…