Div background-color does not extend to child divs

前端 未结 5 1726
不知归路
不知归路 2021-01-28 12:21

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.

相关标签:
5条回答
  • 2021-01-28 12:52

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

    Your new codes becomes this:

    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>

    Example here: https://jsfiddle.net/22L7engy/

    0 讨论(0)
  • 2021-01-28 13:03

    to apply parent div styles to child div either remove

    > float:left
    

    from child or add float:left to the parent div. hope this will solve your problem.

    0 讨论(0)
  • 2021-01-28 13:07
    .container {
      display: block;
      max-width: 600px;
      height: 100%;
      margin: 20vh auto;
      padding: 0 3%;
      background-color: white;
      text-align: justify;
    
      overflow: auto;
    }
    

    Your checkout is floating, so it doesn't apply correctly to how the container behaves

    0 讨论(0)
  • 2021-01-28 13:11

    Either in your CSS, or inline, white the code as:

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

    In HTML, make sure you call each div to their respective CSS.

    0 讨论(0)
  • 2021-01-28 13:19

    You just need to add overflow:hidden or overflow:auto on the container

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

    0 讨论(0)
提交回复
热议问题