html - 如何使用 CSS 创建类似紧身胸衣的效果?
问题描述
我想用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;
}
<div class="triangle_left">
</div>
解决方案
一个想法是使用渐变构建形状并依靠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;
}
<div class="box">
</div>
您也可以这样做,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;
}
<div class="drop">
<div class="box">
</div>
</div>
推荐阅读
- powershell - 输入带有凭据的 PSSession Id
- google-cloud-platform - GCP Dataflow 中的流式数据处理支持哪些数据源?
- python - json.load 不允许加载 nan 和 inf 值
- python - 在 Python 中将 SQL 语句解析为 JSON 格式?
- c# - How to add a cookie to a request using HttpClient from IHttpClientFactory
- php - TYPO3 8.7 扩展 - 缺少存储页面 ID
- flutter - readAsBytesSync 不完整
- java - Android:从firebase数据库方法访问变量
- docker - Docker 中的 Jenkins:关于管道中绑定挂载的说明
- git - 文件名中的特殊字符导致“远程:GitLab:API 不可访问”