如何让CSS中.circle .blad3元素在深蓝色body背景中隐藏?多方案尝试未果
解决方案
先分析核心问题:
- 你给
.blad3写了两个background-image,后一个会直接覆盖前一个,导致之前的渐变设置完全失效; .blad3的z-index: -1让它处于.drawing(z-index:1)的下层,.drawing的overflow:hidden无法裁剪它,超出容器的部分会直接显示在body背景上。
下面提供两种直接可行的实现方式:
方式一:调整层级+父容器裁剪
通过调整定位上下文和层级,让.drawing的overflow:hidden能正常裁剪超出的.blad3部分:
/* 修改.drawing,添加overflow:hidden */ .drawing { position :relative; width: 200em; height: 100em; margin: auto; background-color: rgb(100,167,223); outline: 1.5em solid rgb(56, 118, 29); outline-offset: -1em; z-index: 1; overflow: hidden; /* 添加这行 */ } /* 给.circle添加相对定位,让子元素的绝对定位基于它 */ .circle { position: relative; } /* 修改.blad3的样式 */ .circle .blad3{ position: absolute; width: calc(50% - 120px); height: 80px; bottom: -66px; left: 35px; background-color: rgb(255, 255, 0); /* 直接用纯色背景,裁剪后自动隐藏超出部分 */ filter:contrast(120%); transform:perspective(100px) rotateX(5deg) rotateZ(-264deg); border-radius: 30% 105% 1% 125% / 30% 125% 20% 105%; z-index: 1; /* 调整层级,让它在父元素内部 */ }
方式二:使用clip-path精准裁剪
如果不想调整层级,可以用clip-path直接把.blad3超出.drawing的部分裁剪掉,只保留容器内的黄色区域:
.circle .blad3{ position: absolute; width: calc(50% - 120px); height: 80px; bottom: -66px; left: 35px; background-color: rgb(255, 255, 0); filter:contrast(120%); transform:perspective(100px) rotateX(5deg) rotateZ(-264deg); border-radius: 30% 105% 1% 125% / 30% 125% 20% 105%; z-index: -1; /* 根据.drawing的位置和padding计算裁剪范围,可根据实际显示微调数值 */ clip-path: inset(38px 18px 58px 18px); }
内容的提问来源于stack exchange,提问作者WalterLie
相关产品推荐
相关产品推荐

