0

I am trying to add a background-color to a div that contains two more divs within it. But the background color doesn't stay all the way under the two divs.

The two divs are inside the div that i added the background to, so I thought the background should end under or beside the two divs.

enter image description here

.characters {
    margin: 0;
    border: 5px solid black;
    float: left;
    width: 50%;
    box-sizing: border-box;
    }

    .characters img {
    width: 100%;
    height: auto;
    display: block;
    border-bottom: 5px solid black;
    }

    .infos {
    background-color: gray;
    padding: 35px;
    text-align: center;
    }

    .infos h2, .infos h3 {
    margin: 0;
    }

    .page {
    margin-top: 35px;
    padding: 10px;
    background-color: lightgray;
    }
<div class="page">
    <div class="characters">
        <img src="pic.jpg" alt="pic" width="100px" height="100px"/>
        <div class="infos">
            <h2>text</h2>
            <h3>text</h3>
        </div>
    </div>
    <div class="characters">
        <img src="pic.jpg" alt="pic1" width="100px" height="100px"/>
        <div class="infos">
            <h2>text</h2>
            <h3>text</h3>
        </div>
    </div>
</div>
pr -
  • 240
  • 2
  • 9
Airi
  • 13
  • 6

3 Answers3

0

You need to clear the float...

One way is to apply overflow:auto to the parent.

.page {
  overflow: auto;
}

.characters {
  margin: 0;
  border: 5px solid black;
  float: left;
  width: 50%;
  box-sizing: border-box;
}

.characters img {
  width: 100%;
  height: auto;
  display: block;
  border-bottom: 5px solid black;
}

.infos {
  background-color: gray;
  padding: 35px;
  text-align: center;
}

.infos h2,
.infos h3 {
  margin: 0;
}

.page {
  margin-top: 35px;
  padding: 10px;
  background-color: lightgray;
}
<div class="page">
  <div class="characters">
    <img src="pic.jpg" alt="pic" width="100px" height="100px" />
    <div class="infos">
      <h2>text</h2>
      <h3>text</h3>
    </div>
  </div>
  <div class="characters">
    <img src="pic.jpg" alt="pic1" width="100px" height="100px" />
    <div class="infos">
      <h2>text</h2>
      <h3>text</h3>
    </div>
  </div>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

This is because of the

.characters {
float: left;
}

Property.

Just add an empty div inside the .page and add the CSS clear: both property like below.

<div class="page">
<div class="characters">
    <img src="pic.jpg" alt="pic" width="100px" height="100px"/>
    <div class="infos">
        <h2>text</h2>
        <h3>text</h3>
    </div>
</div>
<div class="characters">
    <img src="pic.jpg" alt="pic1" width="100px" height="100px"/>
    <div class="infos">
        <h2>text</h2>
        <h3>text</h3>
    </div>
   
</div>

 <div class="empty"></div>

</div>

Ans the styles are

.empty{
    clear: both;
}

.characters {
margin: 0;
border: 5px solid black;
float: left;
width: 50%;
box-sizing: border-box;
}

.characters img {
width: 100%;
height: auto;
display: block;
border-bottom: 5px solid black;
}

.infos {
background-color: gray;
padding: 35px;
text-align: center;
}

.infos h2, .infos h3 {
margin: 0;
}

.page {
margin-top: 35px;
padding: 10px;
background-color: lightgray;

}

I use this technique. It may not be the correct method but will be helpful to you. Also, remember to close the .page class div tag. :)

-1

Make the .characters element lightgray, instead of the .page element:

.characters {
  margin: 0;
  border: 5px solid black;
  float: left;
  width: 50%;
  box-sizing: border-box;
  background: lightgray;
}

.characters img {
  width: 100%;
  height: auto;
  display: block;
  border-bottom: 5px solid black;
}

.infos {
  background-color: gray;
  padding: 35px;
  text-align: center;
}

.infos h2,
.infos h3 {
  margin: 0;
}

.page {
  margin-top: 35px;
  padding: 10px;
}
<div class="page">
  <div class="characters">
    <img src="pic.jpg" alt="pic" width="100px" height="100px" />
    <div class="infos">
      <h2>text</h2>
      <h3>text</h3>
    </div>
  </div>
  <div class="characters">
    <img src="pic.jpg" alt="pic1" width="100px" height="100px" />
    <div class="infos">
      <h2>text</h2>
      <h3>text</h3>
    </div>
  </div>
</div>
pr -
  • 240
  • 2
  • 9
  • I mean the background (behind them too, not just the inside). Is there a way for it? – Airi Apr 17 '21 at 17:12