如何在 CSS 中创建响应式、缩放"pile" div 效果?



如何创建一个"效果像这张图?

我希望堆的形状/间距保持原样,并且随着窗口大小的调整,堆可以向左或向右移动。

我一直在摆弄绝对/相对定位,但我是一个CSS新手,我不确定这是不是该走的路。

这是我目前为止写的:

.boxes {
position: absolute;
top: 100px;
}
.box1 {
position: relative;
left: 10vw;
width:fit-content;
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgba(0, 0, 0);
}
.box2 {
position: relative;
width:fit-content;
left: 16vw;
bottom: 13vh;
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgba(0, 0, 0);
transform: rotate(10.84deg);
}
.box3 {
position: relative;
width:fit-content;
left: 25vw;
bottom: 20vh;
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgba(0, 0, 0);
transform: rotate(21deg);
}
<div class="boxes">
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
</div>

你应该设置box为relative,而它的子绝对值为:

.boxes {
top: 100px;
position: relative;
}
.box1  {
position: absolute;
...
}
.box2{
position: relative;
width: 70px;
left: 19vw;
bottom: 7vh;    
width: 70px;
transform: rotate(16deg);
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgb(0 0 0);
}
.box3 {
left: 27vw;
width: 40px;
bottom: 32vh;
position: relative;
padding: 0px 10px 4px 10px;
transform: rotate(-4deg);
box-shadow: 0px 0px 0px 2px rgb(0 0 0);
}
//etc..

尝试旋转变换- MDN

我创建了一个#wrapper,widthheight。然后我给包装器命名为position: relative;,因为我们要用position: absolute;来定位单个元素。

下面是我使用的代码:

* {
margin: 0;
padding: 0;
}
#wrapper {
position: relative;
height: 150px;
width: 450px;
border: 1px solid;
}
#blue {
width: 100px;
height: 30px;
background: blue;
position: absolute;
bottom: 0;
left: 70px;
}
#purple {
width: 100px;
height: 30px;
background: purple;
position: absolute;
bottom: 20px;
left: 145px;
transform: rotate(25deg);
}
#green {
width: 50px;
height: 30px;
background: green;
position: absolute;
bottom: 63px;
left: 177px;
transform: rotate(-10deg);
}
#red {
width: 100px;
height: 30px;
background: red;
position: absolute;
bottom: 21px;
left: 220px;
transform: rotate(28deg);
}
<div id="wrapper">
<div id="blue"></div>
<div id="purple"></div>
<div id="green"></div>
<div id="red"></div>
</div>

最新更新