bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

How is 'clearfix' implemented within twitter-bootstrap??

Open tommls opened this issue 14 years ago • 4 comments

Often this is needed to clear image floats and such... What is the proper way to use it within a twitter-bootstrap-constructed website??

tommls avatar Feb 13 '12 22:02 tommls

Bootstrap's implementation of clearfix is based on the one also used on HTML 5 Boilerplate.

Internally, it's on line 12 of mixins.less:

.clearfix() {
  *zoom: 1;
  &:before,
  &:after {
    display: table;
    content: "";
  }
  &:after {
    clear: both;
  }
}

You use it like you would any other clearfix, right on the parent element that's holding the floats.

rizqidjamaluddin avatar Feb 14 '12 07:02 rizqidjamaluddin

From what I understand of your explanation.

html: `

1234
5678

`

less: .parent { .clearfix(); }

as .clearfix is not to be seen anywhere in the compiled css

peshi avatar Feb 15 '12 14:02 peshi

That would be correct. You must use clearfix through LESS, as far as I remember, it's only implemented as a mixin (it uses parentheses at the end). LESS does not carry along mixins to compiled CSS, so there is no .clearfix class. You must use the mixin in other rules, and LESS will inject the clearfix mixin straight into that rule.

Just to make that clear, here's an example. First, without using mixins:

.injected { color: red; }
.foo { .injected; }

That would compile to this CSS:

.injected { color: red; }
.foo { color: red; }

Notice that it preserves the .injected class. As for a mixin in LESS:

.injected() { color: red; }
.foo{ .injected(); }

Would compile to:

.foo{ color: red; }

The .injected mixin does not carry over. Same goes for the .cleafix() mixin.

rizqidjamaluddin avatar Feb 15 '12 14:02 rizqidjamaluddin

Pushed a fix to 2.0.1-wip for this to enable .clearfix to be used as a class. I highly encourage folks to not use this though in all their markup—use it via LESS whenever possible.

mdo avatar Feb 17 '12 06:02 mdo