html - 为什么将 float 与 % 一起使用时 div 会消失?

标签 html css

似乎当我使用 float 和 % 作为宽度时,其他 div 消失了

 <div id="banner">
 <div id="container">

      <div class="right">
      <div class="topimage"></div>
      <div class="bottomimage"></div>
      </div>
      <div class="bigimage"></div>

 </div>
 </div>

CSS:

 #banner {
 margin-top: 30px;
 }

 #container {
 width: 80%;
 margin: auto;
 }

 .right {
 Float: right;
 }

 .topimage {
 background: url(img1.jpg) no-repeat;
 background-size: cover;
 width: 20%;
 height: 150px;
 }

 .bottomimage {
 background: url(img2.jpg) no-repeat;
 background-size: cover;
 width: 20%;
 height: 150px;
 }

 .bigimage {
 background: url(imgbig.jpg) no-repeat;
 background-size: cover;
 width: 80%;
 height: 300px;
 }

现在这使得 2 个较小的 div 消失了,奇怪的是当我在像素上设置 3 个子 div 的宽度时它工作得很好..

最佳答案

当使用 float 时,元素采用其中内容的宽度。 由于您没有任何内容,因此宽度为 0px。 所以即使 0px 的 100% 仍然是 0px。

您应该为“ float 的 div”添加一些宽度或在空的 div 中添加一些内容。

.right {
 float: right;
 width: 50%;
}

Demo

关于html - 为什么将 float 与 % 一起使用时 div 会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19385842/

相关文章:

html - 我一直在使用负边距和正填充来代替 calc(),但仍然存在一些问题

html - 具有内容的响应式图库

layout - 如何控制不同字体大小的非 block 级元素之间的行高?

javascript - 滚动的在线帖子

javascript - 隐藏/显示多个按钮,除非相关字段已完成

javascript - slider 无法正常工作,并且出现 TypeError : jQuery(. ..).easyResponsiveTabs 不是函数

jquery - Bootstrap 4 : How to create a dropdown menu with an accordion inside it?

php - 当数据库仍为空时,默认图像不会显示

c# - 如何动态更改 td 内容?

jquery - 从 td 中删除背景