如何使用 HTML、CSS 使背景 div 内部弯曲?
How to make Background div inner curved using HTML, CSS?
我希望我的部分背景看起来像最后的图片所示,我如何使用 css 来做到这一点?
.bg{
width: 400px;
height: 200px;
padding: 20px;
text-align: center;
border: 1px solid #000;
background: red;
color: #fff;
display: flex;
}
.bg p{
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
您可以像下面这样使用 linear-gradient
考虑多个背景,而不需要额外的元素:
.bg{
width: 400px;
height: 220px;
padding: 50px 0;
box-sizing:border-box;
text-align: center;
background:
linear-gradient(to bottom left , red 50%,transparent 51%) bottom left,
linear-gradient(to bottom left , transparent 49%,red 50%) top left,
linear-gradient(to bottom right, red 50%,transparent 51%) bottom right,
linear-gradient(to bottom right, transparent 49%,red 50%) top right,
red content-box;
background-size:50% 50px;
background-repeat:no-repeat;
color: #fff;
display: flex;
}
.bg p{
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
但如果你需要边框,我会考虑 pseudo-elements 这样的:
.bg{
width: 400px;
height: 200px;
padding: 20px;
text-align: center;
position:relative;
color: #fff;
display: flex;
z-index:0;
}
.bg:before,
.bg:after{
content:"";
position:absolute;
top:0;
bottom:0;
background:red;
border:2px solid #000;
z-index:-1;
}
.bg:before {
right:50%;
left:0;
transform:skewY(15deg);
transform-origin:top left;
border-right:none;
}
.bg:after {
left:50%;
right:0;
transform:skewY(-15deg);
transform-origin:top right;
border-left:none;
}
.bg p{
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
您可以使用 clip-path to do such things and create them using this tool.
.bg {
width: 400px;
height: 200px;
padding: 20px;
text-align: center;
border: 1px solid #000;
background: red;
color: #fff;
display: flex;
-webkit-clip-path: polygon(100% 0, 100% 80%, 50% 100%, 0 80%, 0 0, 50% 20%);
clip-path: polygon(100% 0, 100% 80%, 50% 100%, 0 80%, 0 0, 50% 20%);
}
.bg p {
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
您可以按照以下步骤来获得您需要的形状。
.left {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
width: 150px;
height: 200px;
border: none;
color: rgba(0,0,0,1);
-o-text-overflow: clip;
text-overflow: clip;
background: #1abc9c;
-webkit-transform: skewY(15deg);
transform: skewY(15deg);
float: left;
margin-top: 20px;
}
.right {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
width: 150px;
height: 200px;
border: none;
color: rgba(0,0,0,1);
-o-text-overflow: clip;
text-overflow: clip;
background: #1abc9c;
-webkit-transform: skewY(-15deg);
transform: skewY(-15deg);
float: left;
margin-top: 20px;
}
<div class="left">Hello</div>
<div class="right">World</div>
我希望我的部分背景看起来像最后的图片所示,我如何使用 css 来做到这一点?
.bg{
width: 400px;
height: 200px;
padding: 20px;
text-align: center;
border: 1px solid #000;
background: red;
color: #fff;
display: flex;
}
.bg p{
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
您可以像下面这样使用 linear-gradient
考虑多个背景,而不需要额外的元素:
.bg{
width: 400px;
height: 220px;
padding: 50px 0;
box-sizing:border-box;
text-align: center;
background:
linear-gradient(to bottom left , red 50%,transparent 51%) bottom left,
linear-gradient(to bottom left , transparent 49%,red 50%) top left,
linear-gradient(to bottom right, red 50%,transparent 51%) bottom right,
linear-gradient(to bottom right, transparent 49%,red 50%) top right,
red content-box;
background-size:50% 50px;
background-repeat:no-repeat;
color: #fff;
display: flex;
}
.bg p{
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
但如果你需要边框,我会考虑 pseudo-elements 这样的:
.bg{
width: 400px;
height: 200px;
padding: 20px;
text-align: center;
position:relative;
color: #fff;
display: flex;
z-index:0;
}
.bg:before,
.bg:after{
content:"";
position:absolute;
top:0;
bottom:0;
background:red;
border:2px solid #000;
z-index:-1;
}
.bg:before {
right:50%;
left:0;
transform:skewY(15deg);
transform-origin:top left;
border-right:none;
}
.bg:after {
left:50%;
right:0;
transform:skewY(-15deg);
transform-origin:top right;
border-left:none;
}
.bg p{
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
您可以使用 clip-path to do such things and create them using this tool.
.bg {
width: 400px;
height: 200px;
padding: 20px;
text-align: center;
border: 1px solid #000;
background: red;
color: #fff;
display: flex;
-webkit-clip-path: polygon(100% 0, 100% 80%, 50% 100%, 0 80%, 0 0, 50% 20%);
clip-path: polygon(100% 0, 100% 80%, 50% 100%, 0 80%, 0 0, 50% 20%);
}
.bg p {
text-align: center;
margin: auto;
font-size: 36px;
}
<div class="bg">
<p>Section Content</p>
</div>
您可以按照以下步骤来获得您需要的形状。
.left {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
width: 150px;
height: 200px;
border: none;
color: rgba(0,0,0,1);
-o-text-overflow: clip;
text-overflow: clip;
background: #1abc9c;
-webkit-transform: skewY(15deg);
transform: skewY(15deg);
float: left;
margin-top: 20px;
}
.right {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
width: 150px;
height: 200px;
border: none;
color: rgba(0,0,0,1);
-o-text-overflow: clip;
text-overflow: clip;
background: #1abc9c;
-webkit-transform: skewY(-15deg);
transform: skewY(-15deg);
float: left;
margin-top: 20px;
}
<div class="left">Hello</div>
<div class="right">World</div>