在不更改整个布局的情况下无法向Bootstrap网格添加边距

时间:2019-04-26 10:15:14

标签: twitter-bootstrap bootstrap-4 containers

我开始学习Bootstrap,但是由于某些原因,在尝试创建网格时遇到了这个问题。我希望它看起来像this image,但是当我尝试添加左边距时,它只是中断了。我可以添加一个空白页边距没有问题,但是每当我水平将其放大一点时,它们都会破裂,并且它从this开始 例如,当我在任何元素this.

中添加“ ml-2”时

这是我的HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Tasca 21</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="css/master.css">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  </head>
  <body>
    <div class="container">
      <div class="row">
        <div class="col" id="top"></div>
      </div>
      <div class="row">
        <div class="mt-2 col-lg-2 col-md-2 col-sm-3 xs-hidden" id="left"></div>
        <div class="col-10">
          <div class="row">
            <div class="mt-2 col" id="top2"></div>
          </div>
          <div class="row" id="mid">
            <div class="mt-2 col-lg-6 col-md-6 col-sm-12 col-xs-12"></div>
            <div class="mt-2 col-lg-6 col-md-6 col-sm-12 col-xs-12"></div>
          </div>
          <div class="row" id="cubes">
            <div class="mt-2 col-2"></div>
            <div class="mt-2 col-2"></div>
            <div class="mt-2 col-4"></div>
            <div class="mt-2 col-2"></div>
            <div class="mt-2 col-2"></div>
          </div>
          <div class="row" id="cubes">
            <div class="mt-2 col-2"></div>
            <div class="mt-2 col-2"></div>
            <div class="mt-2 col-4"></div>
            <div class="mt-2 col-2"></div>
            <div class="mt-2 col-2"></div>
          </div>
      </div>
    </div>
    <div class="row">
      <div class="col-12 mt-2" id="bottom"></div>
    </div>
  </div>
  </body>
</html>

还有我的CSS:

body {
  margin: 5em;
}
#top {
  background-color: #2ECC40;
  height: 2em;
}
#bottom {
  background-color: #2ECC40;
  height: 1em;
}
#left {
  background-color: #FF4136;
  height: 14.5em;
}
#top2 {
  background-color: #F012BE;
  height: 2em;
}
#mid div {
  background-color: #FF851B;
  height: 5em;
}
#cubes div {
  background-color: #0074D9;
  height: 3em;
}

2 个答案:

答案 0 :(得分:0)

您的样式规则可能会与引导冲突。我建议您使用自己的样式将行包装在div中,如下所示:

<div id="cubes">
  <div class="row">
    <div class="mt-2 col-2"></div>
    <div class="mt-2 col-2"></div>
    <div class="mt-2 col-4"></div>
    <div class="mt-2 col-2"></div>
    <div class="mt-2 col-2"></div>
  </div>
</div>

这将消除可能的冲突

答案 1 :(得分:0)

更改左/右页边距 会发生变化how the grid works,因为row 上的负页边距专门与上边距(装订线)有关列。

如果要更改列之间的间距,最好在列上使用填充 ,或在内容上使用 边距 as explained here