将文字放在图像下的更好方法?

时间:2016-10-11 02:15:52

标签: html css

我很难搞清楚这一点。当我第一次尝试让文字停留在图片下时,float始终保持原样。此代码似乎有效,但我认为我做错了,因为当您将.resmontage ul宽度从70%更改为60%时,所有图片都会变小。他们不会留在我最初指定的widthheight

为什么我需要将.resmontage img设置为width: 100%才能使其与.resmontage li一起使用?如果我从resmontage li移除宽度,一切都会搞砸!我知道你可以使用bootstrap,但我更喜欢自己动手。



#bt {
  color: white;
  margin-top: 1%;
  margin-left: -8%;
}
.resmontage ul {
  float: left;
  background-color: black;
  width: 70%;
  height: 25%;
  padding-left: 7%;
  margin-top: 1%;
  border-radius: 1%;
}
.resmontage li {
  list-style-type: none;
  width: 20%;
  margin-top: 1%;
  float: left;
  padding-right: 3%;
}
.resmontage img {
  width: 100%;
  height: 65%;
}
.hello {
  text-align: center;
  color: white;
  margin-top: 2%;
}
.clearfix:after {
  content: "";
  display: both;
  clear: both;
}

<div class="resmontage clearfix">
  <ul>
    <div id="bt">Games that will be released next month.</div>
    <li>
      <img src="https://upload.wikimedia.org/wikipedia/en/a/a5/Grand_Theft_Auto_V.png" />
      <div class="hello">Gta v</div>
    </li>

    <li>
      <img src="http://media.steampowered.com/apps/valvestore/images/slider/store_image_02.png" />
      <div class="hello">CSGO</div>
    </li>

    <li>
      <img src="https://upload.wikimedia.org/wikipedia/commons/e/e0/Rocket_League_coverart.jpg" />
      <div class="hello">Rocket League</div>
    </li>

    <li>
      <img src="http://blogs-images.forbes.com/erikkain/files/2014/12/The-Witcher-3-Wild-Hunt.jpg" />
      <div class="hello">Witcher 3</div>
    </li>

  </ul>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您已经通过将图片width置于各种li中来设置width: 100%。当您指定它们为li时,您只是告诉他们占用他们的容器,即width,其20%#bt { color: white; margin-top: 1%; margin-left: -8%; } .resmontage ul { float: left; background-color: black; width: 70%; height: 25%; padding-left: 7%; margin-top: 1%; border-radius: 1%; } .resmontage li { list-style-type: none; width: 20%; margin-top: 1%; float: left; padding-right: 3%; } .resmontage img { width: 100%; height: auto; } .hello { text-align: center; color: white; margin-top: 2%; }

<div id="bt">Games that will be released next month.</div>
    <li>
      <img src="https://upload.wikimedia.org/wikipedia/en/a/a5/Grand_Theft_Auto_V.png" />
      <div class="hello">Gta v</div>
    </li>

    <li>
      <img src="http://media.steampowered.com/apps/valvestore/images/slider/store_image_02.png" />
      <div class="hello">CSGO</div>
    </li>

    <li>
      <img src="https://upload.wikimedia.org/wikipedia/commons/e/e0/Rocket_League_coverart.jpg" />
      <div class="hello">Rocket League</div>
    </li>

    <li>
      <img src="http://blogs-images.forbes.com/erikkain/files/2014/12/The-Witcher-3-Wild-Hunt.jpg" />
      <div class="hello">Witcher 3</div>
    </li>

  </ul>
</div>
size