在Bootstrap 3中创建基础样式的块网格?

时间:2014-04-21 19:48:05

标签: css responsive-design twitter-bootstrap-3 zurb-foundation

在Zurb Foundation 3+中,他们有一个"块网格的CSS结构"这是一个无序列表,您可以在其中指定行中的项目数。

Here's their docs on it.

我无法在Bootstrap 3中看到这样做;这是唯一的专栏。我希望能够轻松地将重复元素(例如LI)显示为响应网格,我可以在其中指定断点的数量,就像在Foundation中一样。

我想我可以自己动手,但想知道是否有人有过建议或者之前遇到过这个建议。

4 个答案:

答案 0 :(得分:6)

我也希望这是BS3中的功能。您必须创建自己的解决方案。

我的典型解决方法与基金会一样,通过使用百分比来定义宽度,第n个子选择器和调整边距。

[class*="block-grid-"] {
    display: block;
    padding: 0;
    margin: 0 -0.55556rem;
}

[class*="block-grid-"] > li {
    display: block;
    height: auto;
    float: left;
    padding: 0 0.55556rem 1.11111rem;
}

.small-block-grid-3 > li {
    width: 33.33333%;
    list-style: none;
}

.small-block-grid-3 > li:nth-of-type(1n) {
    clear: none;
}

.small-block-grid-3 > li:nth-of-type(3n+1) {
    clear: both;
}

答案 1 :(得分:2)

快速而脏的LESS文件,用于在Bootstrap 3中生成块网格(1到@grid-columns)。仍然需要制作响应式类(如block-grid-sm-3 block-grid-lg-6)。

// Bootstrap variables and mixisn
@import "bootstrap/less/variables.less";
@import "bootstrap/less/mixins.less";

.block-grid () {
  padding: 0;

  & > li {
    display: block;
    float: left;
    list-style: none;
  }
}

.make-block-grid (@columns, @spacing: @grid-gutter-width) when (@columns > 0) {
  .block-grid-@{columns} {
    &:extend(.clearfix all);
    .block-grid();

    margin: 0 (-@spacing / 2);

    > li {
      width: 100% / @columns;
      padding: 0 (@spacing / 2) @spacing;

      &:nth-of-type(1n) { clear: none; }
      &:nth-of-type(@{columns}n+1) { clear: both; }
    }
  }

  .make-block-grid(@columns - 1)
}

.make-block-grid(@grid-columns);

答案 2 :(得分:2)

这是响应版gremo的答案:

.make-block-grid(@class, @columns: @grid-columns, @spacing: @grid-gutter-width) when (@columns > 0) {
  .block-grid-@{class}-@{columns} {
    .clearfix;

    padding: 0;
    margin: 0 (-@spacing / 2);

    & > li {
      display: block;

      float: left;

      width: 100% / @columns;
      padding: 0 (@spacing / 2) @spacing;

      list-style: none;

      &:nth-of-type(1n) {
        clear: none;
      }
      &:nth-of-type(@{columns}n+1) {
        clear: both;
      }
    }
  }

  .make-block-grid(@class, @columns - 1);
}

.make-block-grid(xs);

@media (min-width: @screen-sm-min) {
  .make-block-grid(sm);
}

@media (min-width: @screen-md-min) {
  .make-block-grid(md);
}

@media (min-width: @screen-lg-min) {
  .make-block-grid(lg);
}

答案 3 :(得分:0)

这个CSS对我有用:



class*="block-grid-"] {
  display: block;
  padding: 0;
  margin: 0 -0.625rem;
  *zoom: 1; }
  [class*="block-grid-"]:before, [class*="block-grid-"]:after {
    content: " ";
    display: table; }
  [class*="block-grid-"]:after {
    clear: both; }
  [class*="block-grid-"] > li {
    display: inline;
    height: auto;
    float: left;
    padding: 0 0.625rem 1.25rem; }

@media only screen {
  .small-block-grid-1 > li {
    width: 100%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-1 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-1 > li:nth-of-type(1n+1) {
      clear: both; }

  .small-block-grid-2 > li {
    width: 50%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-2 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-2 > li:nth-of-type(2n+1) {
      clear: both; }

  .small-block-grid-3 > li {
    width: 33.33333%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-3 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-3 > li:nth-of-type(3n+1) {
      clear: both; }

  .small-block-grid-4 > li {
    width: 25%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-4 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-4 > li:nth-of-type(4n+1) {
      clear: both; }

  .small-block-grid-5 > li {
    width: 20%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-5 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-5 > li:nth-of-type(5n+1) {
      clear: both; }

  .small-block-grid-6 > li {
    width: 16.66667%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-6 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-6 > li:nth-of-type(6n+1) {
      clear: both; }

  .small-block-grid-7 > li {
    width: 14.28571%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-7 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-7 > li:nth-of-type(7n+1) {
      clear: both; }

  .small-block-grid-8 > li {
    width: 12.5%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-8 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-8 > li:nth-of-type(8n+1) {
      clear: both; }

  .small-block-grid-9 > li {
    width: 11.11111%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-9 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-9 > li:nth-of-type(9n+1) {
      clear: both; }

  .small-block-grid-10 > li {
    width: 10%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-10 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-10 > li:nth-of-type(10n+1) {
      clear: both; }

  .small-block-grid-11 > li {
    width: 9.09091%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-11 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-11 > li:nth-of-type(11n+1) {
      clear: both; }

  .small-block-grid-12 > li {
    width: 8.33333%;
    padding: 0 0.625rem 1.25rem; }
    .small-block-grid-12 > li:nth-of-type(n) {
      clear: none; }
    .small-block-grid-12 > li:nth-of-type(12n+1) {
      clear: both; } }
@media only screen and (min-width: 40.063em) {
  .medium-block-grid-1 > li {
    width: 100%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-1 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-1 > li:nth-of-type(1n+1) {
      clear: both; }

  .medium-block-grid-2 > li {
    width: 50%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-2 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-2 > li:nth-of-type(2n+1) {
      clear: both; }

  .medium-block-grid-3 > li {
    width: 33.33333%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-3 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-3 > li:nth-of-type(3n+1) {
      clear: both; }

  .medium-block-grid-4 > li {
    width: 25%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-4 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-4 > li:nth-of-type(4n+1) {
      clear: both; }

  .medium-block-grid-5 > li {
    width: 20%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-5 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-5 > li:nth-of-type(5n+1) {
      clear: both; }

  .medium-block-grid-6 > li {
    width: 16.66667%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-6 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-6 > li:nth-of-type(6n+1) {
      clear: both; }

  .medium-block-grid-7 > li {
    width: 14.28571%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-7 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-7 > li:nth-of-type(7n+1) {
      clear: both; }

  .medium-block-grid-8 > li {
    width: 12.5%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-8 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-8 > li:nth-of-type(8n+1) {
      clear: both; }

  .medium-block-grid-9 > li {
    width: 11.11111%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-9 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-9 > li:nth-of-type(9n+1) {
      clear: both; }

  .medium-block-grid-10 > li {
    width: 10%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-10 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-10 > li:nth-of-type(10n+1) {
      clear: both; }

  .medium-block-grid-11 > li {
    width: 9.09091%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-11 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-11 > li:nth-of-type(11n+1) {
      clear: both; }

  .medium-block-grid-12 > li {
    width: 8.33333%;
    padding: 0 0.625rem 1.25rem; }
    .medium-block-grid-12 > li:nth-of-type(n) {
      clear: none; }
    .medium-block-grid-12 > li:nth-of-type(12n+1) {
      clear: both; } }
@media only screen and (min-width: 64.063em) {
  .large-block-grid-1 > li {
    width: 100%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-1 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-1 > li:nth-of-type(1n+1) {
      clear: both; }

  .large-block-grid-2 > li {
    width: 50%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-2 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-2 > li:nth-of-type(2n+1) {
      clear: both; }

  .large-block-grid-3 > li {
    width: 33.33333%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-3 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-3 > li:nth-of-type(3n+1) {
      clear: both; }

  .large-block-grid-4 > li {
    width: 25%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-4 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-4 > li:nth-of-type(4n+1) {
      clear: both; }

  .large-block-grid-5 > li {
    width: 20%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-5 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-5 > li:nth-of-type(5n+1) {
      clear: both; }

  .large-block-grid-6 > li {
    width: 16.66667%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-6 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-6 > li:nth-of-type(6n+1) {
      clear: both; }

  .large-block-grid-7 > li {
    width: 14.28571%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-7 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-7 > li:nth-of-type(7n+1) {
      clear: both; }

  .large-block-grid-8 > li {
    width: 12.5%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-8 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-8 > li:nth-of-type(8n+1) {
      clear: both; }

  .large-block-grid-9 > li {
    width: 11.11111%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-9 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-9 > li:nth-of-type(9n+1) {
      clear: both; }

  .large-block-grid-10 > li {
    width: 10%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-10 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-10 > li:nth-of-type(10n+1) {
      clear: both; }

  .large-block-grid-11 > li {
    width: 9.09091%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-11 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-11 > li:nth-of-type(11n+1) {
      clear: both; }

  .large-block-grid-12 > li {
    width: 8.33333%;
    padding: 0 0.625rem 1.25rem; }
    .large-block-grid-12 > li:nth-of-type(n) {
      clear: none; }
    .large-block-grid-12 > li:nth-of-type(12n+1) {
      clear: both; } }

<ul class="small-block-grid-2 large-block-grid-4">
  <li><img src="../assets/img/examples/comet-th.jpg"></li>
  <li><img src="../assets/img/examples/launch-th.jpg"></li>
  <li><img src="../assets/img/examples/space-th.jpg"></li>
  <li><img src="../assets/img/examples/spacewalk-th.jpg"></li>
</ul>
&#13;
&#13;
&#13;