html - 使相对div具有内容大小

标签 html css

强文本

.inside{
position: absolute;
background-color:red;
top:0px;
left:0px;
width:200px;
height: 200px;
}
.inside2{
height: 210px;
background-color: blue;
}
.inside3{
height: 220px;
background-color: black;
}
.box {
  position: relative;
}
<html>

<body>
    <div class="box">
      <div class="inside"></div>
      <div class="inside"></div>
    </div>
  <div class="box">
    <div class="inside inside2"></div>
    <div class="inside inside2"></div>
  </div>
  <div class="box">
    <div class="inside inside3"></div>
    <div class="inside inside3"></div>
  </div>
</body>

</html>

所以我有 2 个重叠的 div(内部类),它的大小是不固定的(可能是 200px 或 210px ...)。我已经使用 positive: absolute 使其重叠并将框设置为相对的。盒子的问题是它的高度为 0px,所以这些盒子只是重叠在相同的位置(而不是 3 个单独的盒子)。如何解决:

enter image description here

我想要什么: enter image description here

最佳答案

如果我理解你的问题,那么你需要结合使用 display: table-cell for .boxfloat: left对于 .inside:

.inside{ width:50px; float: left; }
.inside1 { height: 220px; background-color: red; }
.inside2 { height: 210px; background-color: blue; }
.inside3 {  height: 200px; background-color: black;}
.box { position: relative; display: table-cell; border: 1px solid red; }

<div class="box">
  <div class="inside inside1"></div>
  <div class="inside inside2"></div>
  <div class="inside inside3"></div>
</div>

[ https://jsfiddle.net/2akhf3fo/ ]

关于html - 使相对div具有内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37511656/

相关文章:

html - 从给定单元格开始 rowspan 和 colspan 并减少 rowspan 的填充

css - React Native - 如何使用本地 SVG 文件(并为其着色)

css - 图像未与 Bootstrap 行中的中心对齐

html - 相对于另一个元素定位元素,相对于窗口绝对定位

javascript - 如何从 html 调用具体的 Coffeescript 函数

html - Flexbox 标题匹配其他行内容

html - ie8中的菜单不运行

jquery - 使用 CSS 动画对 div 进行动画处理,在两个之间稍作延迟

php - 如何迭代 Yii CActiveDataProvider 对象?

html - 如何通过 CSS FLEX for RWD 将列跨度更改为行跨度