使用CSS3的transform属性可以实现响应式平行四边形背景图像效果。
步骤如下:
<div class="parallelogram"></div>
.parallelogram {
background: url("your-image.jpg") no-repeat center center fixed;
background-size: cover;
height: 500px;
position: relative;
width: 100%;
}
.parallelogram::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform: skewY(-12deg);
transform-origin: 0;
background: rgba(0, 0, 0, 0.5);
}
其中,transform: skewY(-12deg);实现了平行四边形的倾斜效果,transform-origin: 0;设置了变换的基点,background: rgba(0, 0, 0, 0.5);设置了伪元素的背景色。
<div class="parallelogram">
<div class="content">
<h1>Your Title Here</h1>
<p>Your Content Here</p>
</div>
</div>
.content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
transform: skewY(12deg);
transform-origin: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1, p {
transform: skewY(-12deg);
}
其中,content容器使用了flex布局,将文字内容居中显示,并使用transform: skewY(12deg);将其旋转回来,以保持文字的水平排列。h1和p标签也使用了transform: skewY(-12deg);来保持文字的水平排列。
完整的CSS代码如下:
.parallelogram {
background: url("your-image.jpg") no-repeat center center fixed;
background-size: cover;
height: 500px;
position: relative;
width: 100%;
}
.parallelogram::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transform: skewY(-12deg);
transform-origin: 0;
background: rgba(0, 0, 0, 0.5);
}
.content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
transform: skewY(12deg);
transform-origin: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
h1, p {
transform: skewY(-12deg);
}