为什么mt-3 [mb-3]不适用于bootstrap中的每一行?

时间:2018-05-20 22:08:02

标签: html twitter-bootstrap

我有两行,想要在顶部和底部添加一些空间。所以我添加了属性mt-3和mb-3。但这些属性似乎只应用于第一行。

如何将它们应用到每一行?

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
    <div class="row mt-3 mb-3" style="background-color:white">
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
    </div>

    <div class="row mt-3 mb-3" style="background-color:whitesmoke">
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
        <div class="col-3">
            completions based on variable types, function definitions, and imported modules.
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

你所拥有的是margin collapse

  

...当两个元素的垂直边距接触时,只有具有最大边距值的元素的边距将被尊重,而具有较小边距值的元素的边距将被折叠为零。

基本上,边距适用于两个元素,但边距重叠

要解决此问题,您有三种选择:

  • float: left
  • position: absolute
  • display: inline-block

最简单的解决方案(在我看来)只是将float: left应用于有问题的元素:

&#13;
&#13;
.mt-3, .mb-3 {
  float: left;
}
&#13;
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
  <link href="styles.css" rel="stylesheet" />

  <title>Web-UI-Automation</title>
</head>

<body>
  <div class="row mt-3 mb-3" style="background-color:white">
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
  </div>

  <div class="row mt-3 mb-3" style="background-color:whitesmoke">
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
    <div class="col-3">
      completions based on variable types, function definitions, and imported modules.
    </div>
  </div>

  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
</body>

</html>
&#13;
&#13;
&#13;