【问题标题】:Div background-color does not extend to child divsdiv 背景颜色不会扩展到子 div
【发布时间】:2020-06-23 22:39:20
【问题描述】:

我有一个容器 div,里面有几个子 div。父 div 有一个背景色,但是,这似乎并没有扩展到最后几个子 div。请看下面的代码:

http://codepen.io/tombarton/pen/aNaGGa

<div class="container">
    <ul>
        <li>
            ...
        </li>
    </ul>
    <hr>
    <div class="checkout">
        <div class="left">
                          ...
        </div>
        <div class="right">
            <div class="button">
                                ...
            </div>
        </div>
    </div>
</div>

CSS

body {
  background-color: #F8F8F8;
  font-family: 'Open Sans', sans-serif;
  text-transform: uppercase;
}

.container {
  display: block;
  max-width: 600px;
  height: 100%;
  margin: 20vh auto;
  padding: 0 3%;
  background-color: white;
  text-align: justify;
}

ul {
  margin: 0;
  padding: 0;
}

li {
  position: relative;
  list-style-type: none;
  margin: 20px auto;
}

.checkout {
  display: block;
  margin-top: 10px;
  width: 50%;
  float: right;
}

.left {
  display: block;
  width: 55%;
  float: left;
  line-height: 40px;
  text-align: right;
}

.right {
  display: block;
  width: 40%;
  margin: auto;
  float: right;
  cursor: pointer;
}

我猜这是因为最后两个 div 没有内容,但我不能 100% 确定。有人有什么想法吗?

谢谢。

【问题讨论】:

  • 你只需要清除浮动。在容器上尝试overflow:hidden
  • 主要是因为.container{display:block}.checkout{float:right}。你可以使用.container{display:table}

标签: html css


【解决方案1】:

您需要在父 div 中的最后一个浮动元素之后添加&lt;div style="clear:both;"&gt;&lt;/div&gt;

你的新代码变成这样:

body {
  background-color: #F8F8F8;
  font-family: 'Open Sans', sans-serif;
  text-transform: uppercase;
}

.container {
  display: block;
  max-width: 600px;
  height: 100%;
  margin: 20vh auto;
  padding: 0 3%;
  background-color: green;
  text-align: justify;
}

h1 {
  font-size: 1em;
  letter-spacing: 1px;
  font-weight: 700;
  padding-top: 20px;
}

ul {
  margin: 0;
  padding: 0;
}

.inline-row {
  display: inline-block;
  height: 56px;
  vertical-align: top;
}

li {
  list-style-type: none;
  margin: 20px auto;
}

.image-container {
  width: 24%;
  img {
    width: 100%;
    height: auto;
  }
}

.product {
  width: 58%;

  h2 {
    margin: 0;
    padding: 0;
    line-height: 28px;
    vertical-align: top;
  }
  p {
    margin: 0;
    padding: 0;
    line-height: 28px;
  }
}

.cost {
  width: 13%;
  text-align: right;
  p {
    margin: 0;
    padding: 0;
    line-height: 28px;
  }
}

.delete {
  float: right;
  width: 3%;
  text-align: right;
  font-size: 0.7em;
  font-weight: 700;
  line-height: 28px;
  cursor: pointer;
}

hr {
 margin-top: 30px;
}

.checkout {
  display: block;
  margin-top: 10px;
  width: 50%;
  float: right;
}

.left {
  display: block;
  width: 55%;
  float: left;
  line-height: 40px;
  text-align: right;
}

.right {
  display: block;
  width: 40%;
  margin: auto;
  float: right;
  cursor: pointer;
}

.button {
  height: 40px;
  width: 100%;
  border: 1px black solid;
  border-radius: 20px;
  text-align: center;
  line-height: 40px;
  font-weight: 700;
}
.clear{
  clear:both;
}
<div class="container">
  <h1>Shopping Cart</h1>
  <ul>
    <li>
      <div class="inline-row image-container">
        <img src="http://placekitten.com/150/56">
      </div>
      <article class="inline-row product">
        <h2>barolo.</h2>
        <p>barolo di castiglione falletto</p>
      </article>
      <div class="inline-row cost"><p>39.99 EUR</p></div>
      <div class="inline-row delete">X</div>
    </li>
    <li>
      <div class="inline-row image-container">
        <img src="http://placekitten.com/150/56">
      </div>
      <article class="inline-row product">
        <h2>barolo.</h2>
        <p>barolo di castiglione falletto</p>
      </article>
      <div class="inline-row cost"><p>39.99 EUR</p></div>
      <div class="inline-row delete">X</div>
    </li>
    <li>
      <div class="inline-row image-container">
        <img src="http://placekitten.com/150/56">
      </div>
      <article class="inline-row product">
        <h2>barolo.</h2>
        <p>barolo di castiglione falletto</p>
      </article>
      <div class="inline-row cost"><p>39.99 EUR</p></div>
      <div class="inline-row delete">X</div>
    </li>
  </ul>

  <hr>   
  
  <div class="checkout">
    <div class="left">TOTAL 148.98 EUR</div>
    <div class="right">
      <div class="right button">CHECKOUT</div>
    </div>
  </div>
  <div class="clear"></div>
</div>

此处示例:https://jsfiddle.net/22L7engy/

【讨论】:

  • 太棒了,谢谢!谁能解释为什么浮动会导致这种行为?
  • float 折叠了父 div 的高度,所以我们需要要么清除它,要么给出 "overflow: hidden"
  • 我使用 clear: 这两种方法,从未实际使用过溢出一种,所以不知道是否更好或什么
【解决方案2】:

将父 div 样式应用到子 div 或者删除

> float:left

从子或添加 float:left 到父 div。希望这能解决您的问题。

【讨论】:

    【解决方案3】:
    .container {
      display: block;
      max-width: 600px;
      height: 100%;
      margin: 20vh auto;
      padding: 0 3%;
      background-color: white;
      text-align: justify;
    
      overflow: auto;
    }
    

    您的结帐是浮动的,因此它不能正确应用于容器的行为方式

    【讨论】:

      【解决方案4】:

      在您的 CSS 或内联中,将代码白化为:

      #adiv {
       background-color: #F8F8F8;
      }
      
      #anotherdiv {
       background-color: #F8F8F8;
      }
      

      在 HTML 中,确保将每个 div 调用到它们各自的 CSS。

      【讨论】:

        【解决方案5】:

        你只需要在容器上添加溢出:隐藏或溢出:自动

        https://stackoverflow.com/a/20180165/2482513

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-04
          • 2012-01-13
          • 2023-03-31
          • 2017-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多