简体   繁体   English

如何在rails app中使用twitter bootstrap和bootstrap-sass?

[英]How to use twitter bootstrap with bootstrap-sass in rails app?

I'm quite new to this, but i cannot figure out the problem. 我对此很新,但我无法弄清楚问题。

In twitter bootstrap i would use : 在twitter bootstrap中我会使用:

<div class="row-fluid">
  <div class="span2">Column1</div>
  <div class="span6">Column2</div>
</div>

And it all works fine. 一切正常。 But i do not want to write spanX and spanY directly into my html file, rather i would like to give meaningful class names, for example: 但我不想直接将spanX和spanY写入我的html文件,而是想提供有意义的类名,例如:

<div class="user-container">
  <div class="user-filter">First Column</div>
  <div class="user-list">Second Column</div>
</div>

Given the fact, that i'm using https://github.com/thomas-mcdonald/bootstrap-sass , how should i write my scss file? 鉴于事实,我正在使用https://github.com/thomas-mcdonald/bootstrap-sass ,我该如何编写我的scss文件? I've tried the following, and it does not work (two columns are not displayed): 我尝试过以下操作,但它不起作用(不显示两列):

@import "bootstrap";
@import "bootstrap-responsive";

.user-container {
    @extend .row-fluid;
}

.user-filter {
    @extend .span2;
}

.user-list {
    @extend .span10;
}

If i look at the generated code, it seems to me that everything should be ok: 如果我看一下生成的代码,在我看来一切都应该没问题:

/* line 164, ../../../../../.rvm/gems/ruby-1.9.3-p125/gems/bootstrap-sass-2.0.0/vendor/assets/stylesheets/bootstrap/_mixins.scss */
.span2, .user-filter {
  width: 140px;
}

and so on. 等等。

What am i doing wrong? 我究竟做错了什么?

UPDATE: 更新:

ok, just to be clear what is wrong - the columns are laid out as rows (one after another) rather like true columns (one next to each other), eg.: 好吧,只是为了清楚是什么问题 - 列被列为行(一个接一个),而不是真正的列(彼此相邻),例如:

with bootstrap: Column1 Column2 with bootstrap:Column1 Column2
with my custom classes: 使用我的自定义类:
First Column 第一栏
Second Column 第二栏

I've inspected the elements layout in Chrome and it seems that bootstrap classes have float property, and mine - no. 我已经检查过Chrome中的元素布局,似乎bootstrap类有float属性,而我的 - 没有。 Looking at css source i see classes like this: 看看css源我看到这样的类:

[class*="span"] {
  float: left;
  margin-left: 20px;
}

So in my case i think it should generate something like : [class*="user-filter"] { float: left; 所以在我的情况下,我认为它应该产生类似的东西:[class * =“user-filter”] {float:left; margin-left: 20px; margin-left:20px; } }

or not? 或不?

It's your use of @extend , or rather, Sass' inability to deal with wildcard class matching, which is rather unsurprising, since it gets rather complex. 这是你对@extend的使用,或者更确切地说,Sass无法处理通配符类匹配,这一点并不令人惊讶,因为它变得相当复杂。

Bootstrap uses a number of what I might refer to as 'nonstandard' methods to address some classes. Bootstrap使用了许多我可能称为“非标准”方法来解决某些类的问题。 You've mentioned one of those in your post above - [class*="span"] . 您已经在上面的帖子中提到了其中一个 - [class*="span"]

Naturally, when you use @extend x , Sass is extending the x class. 当然,当你使用@extend x ,Sass正在扩展x类。 Unfortunately, it (currently) has no way of knowing that a wildcard matcher also affects the output of the class. 不幸的是,它(目前)无法知道通配符匹配器也会影响类的输出。 So yes, in an ideal world, [class*="span"] would also be extended to define [class*="span"], .user-filter , but Sass can't currently do that. 所以是的,在一个理想的世界中, [class*="span"]也会扩展为定义[class*="span"], .user-filter ,但Sass目前无法做到这一点。

While extending .row-fluid is enough to include the rules nested underneath it wrt. 虽然扩展.row-fluid足以包含嵌套在它下面的规则。 the span classes, as per above, it won't adjust the wildcards for extended spans. 如上所述,span类不会调整扩展跨度的通配符。

bootstrap-sass already had a mixin for fixed width columns/rows, makeRow() and makeColumn() . bootstrap-sass已经有一个mixin用于固定宽度的列/行, makeRow()makeColumn() I've just pushed a makeFluidColumn() mixin, that, well, makes a fluid span. 我刚刚推了一个makeFluidColumn() mixin,好吧,它会产生流畅的跨度。 Your code would then become: 您的代码将变为:

.user-container {
  @extend .row-fluid;
}

.user-filter {
  @include makeFluidColumn(2);
}

.user-list {
  @include makeFluidColumn(10);
}

Unfortunately (as per usual) it's not quite so simple. 不幸的是(按照惯例)它并不那么简单。 Bootstrap uses this snippet to reset the margin on the first spanx class that is a child of the row. Bootstrap使用此代码段重置第一个spanx类的边距,该第一个spanx类是该行的子节点。

> [class*="span"]:first-child {
  margin-left: 0;
}

However, we cannot redefine this for each call of makeFluidColumn , and so we must manually set no margin-left on any element that will be the first child of a fluid row. 但是,我们无法在每次调用makeFluidColumn重新定义它,因此我们必须在任何将成为流体行的第一个子元素的元素上手动设置margin-left。 It's also worth noting that mixing spanx classes and makeFluidColumn classes will cause the first spanx class to have its margin reset, regardless of whether it's actually the first column in the row. 值得注意的是,混合spanx类和makeFluidColumn类将导致第一个spanx类重置其边距,而不管它是否实际上是行中的第一列。

Your code would therefore be 因此,您的代码将是

.user-container {
  @extend .row-fluid;
}

.user-filter {
  @include makeFluidColumn(2);
  margin-left: 0; // reset margin for first-child
}

.user-list {
  @include makeFluidColumn(10);
}

It's not a particularly pretty solution, but it works, and is all to do with how Bootstrap uses wildcard class matching, as you gathered in your question update. 它不是一个特别漂亮的解决方案,但它可以工作,并且与你在问题更新中收集的Bootstrap如何使用通配符类匹配有关。 I've only just pushed this to the 2.0.2 branch, so you'll have to use Bundler with Git to install it, but I'm hoping for a release in the next couple of days. 我只是把它推到2.0.2分支,所以你必须使用Bundler和Git来安装它,但我希望在接下来的几天内发布。

You are right. 你是对的。 Twitter is pushing an ani-patter here. Twitter正在推动这里的模式。 See this article. 看到这篇文章。

http://ruby.bvision.com/blog/please-stop-embedding-bootstrap-classes-in-your-html http://ruby.bvision.com/blog/please-stop-embedding-bootstrap-classes-in-your-html

Using boostrap-sass 2.3.2.2 gem I had to create my own mixin based on bootstrap's mixins to make CSS classes act like bootstrap .span classes. 使用boostrap-sass 2.3.2.2 gem我必须基于bootstrap的mixins创建我自己的mixin,以使CSS类像bootstrap .span类一样。

// private mixin: add styles for bootstrap's spanX classes
@mixin _makeFluidSpan($gridColumns, $fluidGridColumnWidth, $fluidGridGutterWidth) {
  @include input-block-level();
  float: left;
  margin-left: $fluidGridGutterWidth;
  *margin-left: $fluidGridGutterWidth - (.5 / $gridRowWidth * 100px * 1%);
  @include grid-fluid-span($gridColumns, $fluidGridColumnWidth, $fluidGridGutterWidth);
}

// thats what you should use
@mixin makeFluidSpan($gridColumns) {
  @media (min-width: 980px) and (max-width: 1199px) {
    @include _makeFluidSpan($gridColumns, $fluidGridColumnWidth, $fluidGridGutterWidth);
  }
  @media (min-width: 1200px) {
    @include _makeFluidSpan($gridColumns, $fluidGridColumnWidth1200, $fluidGridGutterWidth1200);
  }
  @media (min-width: 768px) and (max-width: 979px) {
    @include _makeFluidSpan($gridColumns, $fluidGridColumnWidth768, $fluidGridGutterWidth768);
  }
  &:first-child {
    margin-left: 0;
  }
  @media (max-width: 767px) {
    float: none;
    display: block;
    width: 100%;
    margin-left: 0;
    box-sizing: border-box;
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}

example: 例:

  .like-span3 { // this class acts like .span3
    @include makeFluidSpan(3);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM