如何在 CSS 中创建彼此相邻的多边形形状 [英] How to create polygonal shapes right next to each other in CSS
问题描述
我创建了一个多边形三角形,我想将它们并排堆叠
我使用了 shape-outside,但它似乎不起作用.我希望这是动态的,以便可以在无需更改代码的情况下添加更多内容
div:nth-child(odd) {宽度:280px;高度:280px;背景:#1e90ff;-webkit-clip-path: 多边形(50% 0%, 0% 100%, 100% 100%);显示:内联块;背景:url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg) 中心;背景尺寸:封面;向左飘浮;剪辑路径:多边形(50% 0%、0% 100%、100% 100%);形状外:多边形(50%0%,0%100%,100%100%);}div:第n个孩子(偶数){宽度:280px;高度:280px;背景:#1e90ff;显示:内联块;背景:url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/Harry-Potter-1-.jpg) 中心;背景尺寸:封面;向左飘浮;-webkit-clip-path: 多边形(50% 100%, 0 0, 100% 0);剪辑路径:多边形(50% 100%, 0 0, 100% 0);形状外:多边形(50% 100%, 0 0, 100% 0);左:-137px;}div {位置:相对;}
<div></div><div></div><div></div><div></div><div></div>
你可以使用
的伪选择器 :nth-child(even)
为了选择所有偶数"元素.
我没有使用剪辑路径(由于浏览器兼容性问题),所以这不是最干净的,但这(应该)适用于更多浏览器:
.parent {高度:100px;宽度:100px;边距:2px;显示:内联块;位置:相对;溢出:隐藏;左边距:-50px;}.parent:nth-child(odd) {顶部:-50px;}.父母:第一个孩子{左边距:0;}.parent:nth-child(odd) .child {位置:绝对;底部:0;左:0;背景:番茄;高度:70%;宽度:70%;变换:旋转(45度);变换原点:左下角;}.parent:nth-child(even) .child {位置:绝对;顶部:0;左:0;背景:番茄;高度:70%;宽度:70%;变换:旋转(-45度);变换原点:左上角;}
<div class="child"></div><div class="parent"><div class="child"></div>
<div class="parent"><div class="child"></div>
<div class="parent"><div class="child"></div>
<div class="parent"><div class="child"></div>
<div class="parent"><div class="child"></div>
<div class="parent"><div class="child"></div>