css - 如何将元素居中但将它们显示在彼此之上

标签 css

有没有一种方法可以使元素的子元素水平和垂直居中,但将所有子元素显示在彼此之上,就好像它们具有 position: absolute 一样。但是没有为每个 child 独立设置样式。

<div style="widht: 100: height: 100">
  <div style="widht: 80: height: 80">
  <div style="widht: 60: height: 60">
  <div style="widht: 40: height: 40">
  <div style="widht: 20: height: 20">
</div>  

结果应该是这样的:

enter image description here

最佳答案

如果你不能嵌套你的 div,你可以使用 position:absolute; 和 top/left 值来定位每个 div :

DEMO

HTML :

<div class="wrap" style="width: 100px; height: 100px;">
    <div class="one" style="width: 80px; height: 80px;"></div>
    <div class="two" style="width: 60px; height: 60px;"></div>
    <div class="three" style="width: 40px; height: 40px;"></div>
    <div class="four" style="width: 20px; height: 20px;"></div>
</div>

CSS:

div {
    position:absolute;
    background:#38E1BA;
    border:5px solid #000;
}
.one{
    top:5px;
    left:5px;
}
.two{
    top:15px;
    left:15px;
}
.three{
    top:25px;
    left:25px;
}
.four{
    top:35px;
    left:35px;
}

关于css - 如何将元素居中但将它们显示在彼此之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23933120/

相关文章:

javascript - HTML - 发票 : How to add values to text field and calculate total

html - <code> inside <p> 自动清除元素

javascript - 如何保持 slider 两侧的边距?

css - 当一个类名存在于其他地方时,如何在 div 类中 CSS 一个 div 类?

css - 取消或重置继承的 css 样式

jquery - 选项卡中的 CSS 过渡

html - css响应页面上的图像重叠

html - purecss 下拉菜单不起作用

javascript - 如何在背景视频上叠加文字?

css - 如何使用主窗口垂直滚动条使外部 <div> 固定而内部 <div> 可滚动