首页 > 解决方案 > 绝对位置与边距底部。添加间隔 div 不起作用

问题描述

我有一个 div,里面有一些元素。然后我在它下面有一个绝对 div(我将它推到页面顶部)。我想将 margin-bottom 添加到绝对 div 中。

但是要求是:我不想将margin-top 添加到“container-fluid” div。绝对 div 应该保持绝对。

我尝试添加一个间隔 div,但没有奏效(它应该有效,但它没有)。感谢所有帮助:)

演示:

#container-test {
  position: absolute;
  background-color: #ffffff;
  top: 0;
  left: 0;
  width: 100%;
  height: 65px;
  margin-bottom: 500px;
}

.spacer {
  height: 50px;
  margin: 0 0 -50px 0;
  /* margin: 20px 0 -50px 0; use this if you want #container to have a 'bottom padding', in this case of 20px */
  background: transparent;
  /* you'll need this if #container's parent element has a different background from #container itself */
}
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="./example.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>

<body>
  <div class="container-fluid">
    <h1>Hello World!</h1>
    <p>Resize the browser window to see the effect.</p>
    <p>The columns will automatically stack on top of each other when the screen is less than 768px wide.</p>
    <div class="row">
      <div class="col-sm-4">
        <h3>Title 1 - col-sm-4</h3>
      </div>
      <div class="col-sm-4" style="background-color:lavenderblush;">
        <div class="section-topleft">
          <h3>Title 1 - col-sm-4</h3>
        </div>
        <h3>Title 2 - col-sm-4</h3>
      </div>
      <div class="col-sm-4" style="background-color:lavender;">
        <h3>Title 3 - col-sm-4</h3>
      </div>
    </div>
  </div>

  <div id='container-test'>
    <div class="spacer"></div>
    <p>This text should have a margin-bottom to push the other elements down.</p>

  </div>
</body>

标签: htmlcsstwitter-bootstrap-3

解决方案


推荐阅读