css - 如何弯曲 CSS 伪元素背景?
问题描述
我有一个红色的伪元素,我将它添加到我网站上某些部分的背景中。
一切正常,但我想把它从直线改为曲线。
知道怎么做吗?
.top-section:before {
opacity: .7;
content: '';
position: absolute;
width: 100%;
height: 600px;
margin-top: 12rem;
transform: skewY(20deg);
background: linear-gradient(-20deg,#fff 0,#fff 67%,red 100%);
z-index: -1;
}
<div class="top-section">
<div class="inner-section">
</div>
</div>
解决方案
一种方法是使用自定义 svg 创建一个clip-path
来转换伪元素:
body {
margin: 0
}
.top-section:before {
opacity: .7;
content: '';
position: absolute;
width: 100%;
height: 600px;
margin-top: 12rem;
clip-path: url(#svgPath);
background: linear-gradient(-20deg, #fff 0, #fff 67%, red 100%);
z-index: -1;
}
<div class="top-section">
<div class="inner-section">
</div>
</div>
<!--svg mask-->
<svg height="0" width="0">
<defs>
<clipPath id="svgPath" clipPathUnits="objectBoundingBox">
<path d='M1,1c-0.43,0-0.69-0.055-1-0.246V0c0.362,0.213,0.573,0.252,1,0.226V1z'/>
</clipPath>
</defs>
</svg>
推荐阅读
- java - 用于在不同国家注册的电话号码的 Spring Formatter
- java - 连接到 MySQL 成功,但在查询时出现 Communications link failure 错误
- reactjs - 无法清除material-ui Aucocomplete中的输入文本
- javascript - 如何同时增加值而不是按顺序映射它们?
- javascript - 未捕获的类型错误:Inventory.updateInventory 不是函数
- ios - 每天在 Firebase 上检索不同的字符串 - swift
- android - 如何将 ID 的数组列表更改为其他列值 SQLite 的数组列表?
- html - 在网页中从右到左显示内容(html 和 css)
- java - 使用 badass-runtime 插件中的图像运行 gradle 测试
- angular - gojs-angular 无法重现最小示例