如何在不使用列填充的情况下为Bootstrap行的背景着色?

时间:2019-01-04 09:25:00

标签: html css twitter-bootstrap

我试图使背景适合Bootstrap的列大小,但仍会出现在整个行中。我该如何实现?

我尝试了以下操作:

.yourcontent {
  margin-bottom: 5px;
}

.yourcontent [class^=col-] {
  background-color: cyan;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

  <div class="row">
    <div class="col-xs-12">
      <div class="btn-group" role="group" style="margin-top: 5px; margin-bottom: 5px">
        <button type="button" class="btn btn-primary">Foo</button>
        <button type="button" class="btn btn-primary">Bar</button>
        <button type="button" class="btn btn-primary">Foobar</button>
        <button type="button" class="btn btn-primary">Barfoo</button>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-xs-12">

      <div class="row yourcontent">
        <div class="col-xs-6">
          <div class="">
            Foo
          </div>
        </div>
        <div class="col-xs-2">
          <div class="">
            Bar
          </div>
        </div>
        <div class="col-xs-2">
          <div class="">
            Foobar
          </div>
        </div>
        <div class="col-xs-2">
          <div class="">
            Barfoo
          </div>
        </div>
      </div>

    </div>

  </div>
</div>

JSFiddle

我希望它看起来像这样,除了两列之间的空白也应该用颜色填充:

.yourcontent {
  margin-bottom: 5px;
  background-color: cyan;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

  <div class="row">
    <div class="col-xs-12">
      <div class="btn-group" role="group" style="margin-top: 5px; margin-bottom: 5px">
        <button type="button" class="btn btn-primary">Foo</button>
        <button type="button" class="btn btn-primary">Bar</button>
        <button type="button" class="btn btn-primary">Foobar</button>
        <button type="button" class="btn btn-primary">Barfoo</button>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-xs-12">

      <div class="row">
        <div class="col-xs-6">
          <div class="yourcontent">
            Foo
          </div>
        </div>
        <div class="col-xs-2">
          <div class="yourcontent">
            Bar
          </div>
        </div>
        <div class="col-xs-2">
          <div class="yourcontent">
            Foobar
          </div>
        </div>
        <div class="col-xs-2">
          <div class="yourcontent">
            Barfoo
          </div>
        </div>
      </div>

    </div>

  </div>
</div>

JSFiddle

我希望背景在整个行中延伸,但不延伸给定行中第一个col-xs- *元素的左填充和最后一个col-xs- *元素的右填充。会有人碰巧知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

Bootstrap 4 中,您拥有.no-gutters类。当您使用 Bootstrap 3 时,不包含该类。但是,您可以轻松地自己创建此类。这将删除列之间的装订线

.yourcontent {
  margin-bottom: 5px;
  background-color: cyan;
}

/* 
  Optional, to fix to width of the row 
*/
.row.no-gutters {
  margin-left: 0;
  margin-right: 0;
}


.row.no-gutters > [class^="col-"] {
  padding-left: 0;
  padding-right: 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">

  <div class="row">
    <div class="col-xs-12">
      <div class="btn-group" role="group" style="margin-top: 5px; margin-bottom: 5px">
        <button type="button" class="btn btn-primary">Foo</button>
        <button type="button" class="btn btn-primary">Bar</button>
        <button type="button" class="btn btn-primary">Foobar</button>
        <button type="button" class="btn btn-primary">Barfoo</button>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-xs-12">

      <div class="row no-gutters">
        <div class="col-xs-6">
          <div class="yourcontent">
            Foo
          </div>
        </div>
        <div class="col-xs-2">
          <div class="yourcontent">
            Bar
          </div>
        </div>
        <div class="col-xs-2">
          <div class="yourcontent">
            Foobar
          </div>
        </div>
        <div class="col-xs-2">
          <div class="yourcontent">
            Barfoo
          </div>
        </div>
      </div>

    </div>

  </div>
</div>