Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using .container as a mixin is impossible #10357

Closed
dminkovsky opened this issue Sep 1, 2013 · 9 comments
Closed

Using .container as a mixin is impossible #10357

dminkovsky opened this issue Sep 1, 2013 · 9 comments
Labels

Comments

@dminkovsky
Copy link

I am trying to avoid semantically polluting my markup with Bootstrap's selectors. Rather than including Bootstrap's classes in my HTML, I would rather incorporate its rulesets in my own rulesets.

For example, say I've already got this HTML:

<div class="content"></div>

I want to make div#content a Boostrap .container. To that end, I wish that in my LESS I could simply do:

@import "variables.less";
@import "../boostrap/less/grid.less";
.content {
  .container;
}

However, this doesn't really make my .content DIV a .container because of the @media queries in grid.less that are beyond the initial .container ruleset. They don't get included in the resulting CSS for .content. Am I missing some way I could do this with LESS?

I've searched the issues list for this and the closest topic I found was "#10160". That PR doesn't seem to address this problem.

@mdo
Copy link
Member

mdo commented Sep 4, 2013

Use .container-fixed() and set the width yourself as you need it.

@mdo mdo closed this as completed Sep 4, 2013
@dminkovsky
Copy link
Author

Thanks for your attention to this, @mdo.

Why can't the @media queries from grid.less be moved to .container-fixed(), like:

// Centered container element
.container-fixed() {
  margin-right: auto;
  margin-left: auto;
  padding-left:  (@grid-gutter-width / 2);
  padding-right: (@grid-gutter-width / 2);
  .clearfix();
+
+ @media (min-width: @screen-sm) {
+    width: @container-sm;
+ }
+
+ @media (min-width: @screen-md) {
+    width: @container-md;
+ }
+ 
+ @media (min-width: @screen-lg-min) {
+    width: @container-lg;
+ }
}

The result in grid.less would be identical—it would simply be achieved by .container { .container-fixed(); } without further specification in the media queries below, and .container-fixed() would be a full-fledged "mobile first" mixin.

@mdo
Copy link
Member

mdo commented Sep 5, 2013

The intention of the .container-fixed() mixin is not to house all our break points—and we can't change it to do that at this point since it's be a break of backwards compatibility. It's a method of generating a centered containing element. That said, we could do that for the regular .container class and simply move those declarations from the existing media queries into their own per your example.

@dminkovsky
Copy link
Author

Thank you @mdo. If I understood you correctly, how about the change proposed in the above-listed PR?

The result is that

@import "less/variables.less"; 
@import "less/mixins.less";
@import "less/grid.less";

#elem {
  .container;
}

yields:

// ... [ @import code exclude ]
#elem {
  margin-right: auto;
  margin-left: auto;
  padding-left: 15px;
  padding-right: 15px;
}
#elem:before,
#elem:after {
  content: " ";
  /* 1 */

  display: table;
  /* 2 */

}
#elem:after {
  clear: both;
}
@media (min-width: 768px) {
  #elem {
    width: 750px;
  }
}
@media (min-width: 992px) {
  #elem {
    width: 970px;
  }
}
@media (min-width: 1200px) {
  #elem {
    width: 1170px;
  }
}

Thus, .container is usable as a responsive mixin in its Bootstrap entirety.

@dineshdh
Copy link

Hi @dminkovsky

Can't you just extend your class


.content {
    &:extend(.container);
}

@fatuk
Copy link

fatuk commented Sep 27, 2013

I've tried this way and it's ok. Here is sample

.wrapper {
    &:extend(.container);

    .block {
        &:extend(.row);

        .content {
            &:extend(.col-md-8, .col-sm-12);
            background: orange;
        }

        .aside {
            &:extend(.col-md-4, .col-sm-12);
            background: yellowgreen;
        } 
    }
}

@dminkovsky
Copy link
Author

@dineshdh: thanks for this-- I haven't had a chance to try it out but will test this and let you know soon. Would love to solve this with LESS, but I didn't know about extend().

@dminkovsky
Copy link
Author

So yeah, @dineshdh, your suggestion is totally sufficient. Finally had a chance to use this. No patch needed. Compiles with with lessc ~1.4.

However, the grunt-recess that's with the latest Bootstrap won't compile &:extend(). So if you want to custom build with Bootstrap's grunt setup then you are out of luck. In Bootstrap's package.json, grunt-recess is currently at ~0.4.0. This version requires "recess": "~1.1.6" which in turn is using "less": "~1.3.0". And we need ~1.4.0.

The thing is, recess or grunt-recess don't seem to be compatible with 1.4 or 1.5. When you swap either of those in, you get:

$ grunt dist-css
Running "recess:css" (recess) task
Fatal error: Cannot read property 'data' of undefined

What to do? Such a good solution here. Would love to use it.

Thank you,
DM

@mdo mdo mentioned this issue Nov 28, 2013
mdo added a commit that referenced this issue Nov 28, 2013
@dminkovsky
Copy link
Author

Thank you for the time you put into this @mdo. Greatest appreciation..

stempler pushed a commit to stempler/bootstrap that referenced this issue Apr 11, 2014
…y by moving the width declarations within one class
stempler pushed a commit to stempler/bootstrap that referenced this issue Nov 4, 2014
…y by moving the width declarations within one class
carlitosvargas pushed a commit to carlitosvargas/AYS2024 that referenced this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants