【问题标题】:Bootstrap & LESS: importing mixins only as referenceBootstrap & LESS:导入 mixins 仅作为参考
【发布时间】:2013-11-30 00:00:36
【问题描述】:

我正在使用 Bootstrap 3.0 和 LESS 1.5。我将为许多站点使用相同的 bootstrap.css(或使用它们的 CDN)。所以我正在使用

@import (reference) "bootstrap-3.0.0/less/bootstrap.less";
@import (reference) "bootstrap-3.0.0/less/mixins.less";

仅作为参考导入。

我的 app.less 有(除其他外)

.herocontainer{
    .make-row();
    .iphoneblock{
        .make-sm-column-offset(1);
        .make-sm-column(4);
        text-align: center;
    }
    .copyblock{
        .make-sm-column(5);
        text-align: center;
        .copytext{
            @media(min-width: @screen-sm) {
              padding-top: 100px;
              }
        }
        .buybutton{
            .btn-lg;
            .btn-primary;
            background-color: #d6822f;
            }
    }
}

生成的站点只是单列输出。但是,如果我从 mixins 中删除(参考),例如:

@import (reference) "bootstrap-3.0.0/less/mixins.less";

然后我得到一个两列响应输出,但生成的 css 也有我不需要的类。

所以, a)我如何只为我在 app.less 中编写的类而不是因引导类而臃肿的类获取 css 中的类 b) 我该如何调试此类 CSS 问题? (我确实使用 Google chrome 工具,但这个问题超出了我的理解/调试范围)

谢谢你,
约瑟夫

【问题讨论】:

    标签: less twitter-bootstrap-3


    【解决方案1】:

    另见:https://stackoverflow.com/a/14463540/1596547。其中说:

    该文件不会以 CSS 形式输出任何实际代码,但所有代码都可用作 mixins。

    在您的情况下,它们将与 make-sm-column() 有所不同,此 mixin 包含媒体查询定义。如果您在导入 mixins.less 时使用 (reference),则此媒体查询部分不会包含在您的 CSS 中。

    // Generate the small columns
    .make-sm-column(@columns; @gutter: @grid-gutter-width) {
      position: relative;
      // Prevent columns from collapsing when empty
      min-height: 1px;
      // Inner gutter via padding
      padding-left:  (@gutter / 2);
      padding-right: (@gutter / 2);
    
      // Calculate width based on number of columns available
      @media (min-width: @screen-sm-min) {
        float: left;
        width: percentage((@columns / @grid-columns));
      }
    }
    

    将给予:

    .herocontainer {
      position: relative;
      min-height: 1px;
      padding-left: 15px;
      padding-right: 15px;
    }
    @media (min-width: 768px) {
      .herocontainer {
        float: left;
        width: 33.33333333333333%;
      }
    }
    

    使用(reference) 你只会得到:

    .herocontainer {
      position: relative;
      min-height: 1px;
      padding-left: 15px;
      padding-right: 15px;
    }
    

    注意,您还使用来自buttons.less 的btn-lg。对我来说,引用 button.less 而不是 mixins.less 似乎是最好的解决方案(理论上的 mixins 应该只包含 mixins,所以引用应该会有所不同)。否则,创建一个 mixins.less,只包含你需要的 mixins。

    更新

    1. 有一个错误Reference import not importing media queries
    2. 当引用的导入中的类从未引用的导入中调用 mixin 时,该 mixin 的输出将(意外)显示在您的 css 中。所以在上面的答案中,不使用 mixins.less 的参考确实会产生很多不需要的类

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2015-03-05
      • 2016-07-23
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多