方法一:嵌套元素
我们对外层元素skew(45deg)后,再对内层元素skew(-45deg),使内层元素变为原来的样子。避免我们使用skew拉伸后,里面的内容同步拉伸,如下图:
解决如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
margin: 0 auto;
width: 200px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
background-color: aqua;
transform: skew(-45deg);
}
.content {
transform: skew(45deg);
}
</style>
</head>
<body>
<div class="box">
<div class="content">
我是平行四边形
</div>
</div>
</body>
</html>
方法二:伪元素法
把与内容无关的样式应用到伪元素上,再对伪元素进行变形即可。
.button{
width:200px;
height: 100px;
color:white;
font-size: 26px;
font-weight: bold;
text-align: center;
line-height: 100px;
position: relative;
}
.button::before{
content:'';
transform: skew(-45deg);
background:lightseagreen;
position:absolute;
z-index: -1;
top:0;
left: 0;
bottom: 0;
right: 0;
}