div背景色不会扩展到子div [英] Div background-color does not extend to child divs

查看:100
本文介绍了div背景色不会扩展到子div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个容器div,其中有几个子div。父级div具有背景色,但是似乎没有扩展到最后几个子级div。请参见下面的代码:

I have a container div with several child divs within this. The parent div has a background-color, however, this doesn't seem to be extending to the last couple of child divs. Please see code below:

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%。有人有任何想法吗?

I'm guessing this is due to the fact that final two divs do not have content, but I'm not 100% sure. Does anyone have any ideas?

谢谢。

推荐答案

您需要在父div的最后一个浮动元素之后添加< div style = clear:both;>< / div>

You need to add <div style="clear:both;"></div> after the last floating element within the parent div.

您的新代码变为:

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/

这篇关于div背景色不会扩展到子div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆