zdd*_*zdd 1 html css css-shapes
我想用CSS创建如下效果,最初,我想合并两个带圆角的矩形,但我不能用那种方式,因为它是一个锐角,不是圆角,看看圆形区域,这个是最难的部分。
所以我这样做:
1. 创建一个圆角矩形作为轮廓。边框颜色为灰色。
2. 在矩形左侧的中心创建一个灰色三角形。
3. 创建另一个白色的三角形,覆盖第 2 步中的灰色三角形,稍微偏移,使其看起来像矩形的边框。
这是效果:
我的问题是:
1. 如何添加正确的部分?似乎我只能在 CSS 选择器 :before 或 :after 中同时绘制一个形状。
2.如果我想添加边框阴影效果怎么办?如果我添加矩形的阴影,会出现白色三角形,它很难看。
3. 或者有什么其他方法可以实现这个效果?
这是代码:
.triangle_left {
width: 600px;
height: 600px;
position: relative;
margin: 20px auto;
border: 2px solid #cccccc;
/* box-shadow: 0px 0px 8px 1px darkgrey; */
border-radius: 20px;
}
.triangle_left:before {
content: '';
width: 0;
height: 0;
border: 20px solid transparent;
border-left-color: #cccccc;
position: absolute;
left: 0;
top: 50%;
margin-top: -20px;
}
.triangle_left:after {
content: '';
width: 0;
height: 0;
border: 18px solid transparent;
border-left-color: white;
position: absolute;
left: -2px;
top: 50%;
margin-top: -18px;
}Run Code Online (Sandbox Code Playgroud)
<div class="triangle_left">
</div>Run Code Online (Sandbox Code Playgroud)
一个想法是使用梯度构建形状并依靠drop-shadow过滤器
.box {
margin-top:50px;
border-radius:10px;
width:200px;
height:200px;
background:
/*the middle line */
repeating-linear-gradient(to right,gold 0px,gold 5px,transparent 5px,transparent 10px) center/calc(100% - 40px) 2px,
/*The background*/
linear-gradient(to bottom right,#fff 49%,transparent 50%) 100% calc(50% - 10px) / 20px 20px,
linear-gradient(to top right,#fff 49%,transparent 50%) 100% calc(50% + 10px) / 20px 20px,
linear-gradient(to bottom left ,#fff 49%,transparent 50%) 0 calc(50% - 10px) / 20px 20px,
linear-gradient(to top left ,#fff 49%,transparent 50%) 0 calc(50% + 10px) / 20px 20px,
linear-gradient(#fff,#fff) top right / 20px calc(50% - 20px),
linear-gradient(#fff,#fff) bottom right / 20px calc(50% - 20px),
linear-gradient(#fff,#fff) top left / 20px calc(50% - 20px),
linear-gradient(#fff,#fff) bottom left / 20px calc(50% - 20px),
linear-gradient(#fff,#fff) center/calc(100% - 40px) 100%;
background-repeat:no-repeat;
filter:drop-shadow(0 0 5px #000);
}
body {
background:pink;
}Run Code Online (Sandbox Code Playgroud)
<div class="box">
</div>Run Code Online (Sandbox Code Playgroud)
您也可以使用它,clip-path但您需要一个额外的包装器来应用drop-shadow过滤器
.box {
margin-top: 50px;
border-radius: 10px;
width: 200px;
height: 200px;
background:
repeating-linear-gradient(to right, gold 0px, gold 5px, transparent 5px, transparent 10px) center/100% 2px no-repeat,
#fff;
clip-path:polygon(
0 0,100% 0,
100% calc(50% - 20px),calc(100% - 20px) 50%,100% calc(50% + 20px),
100% 100%, 0 100%,
0 calc(50% + 20px),20px 50%,0 calc(50% - 20px)
);
}
.drop {
filter: drop-shadow(0 0 5px #000);
}
body {
background: pink;
}Run Code Online (Sandbox Code Playgroud)
<div class="drop">
<div class="box">
</div>
</div>Run Code Online (Sandbox Code Playgroud)