我们可以通过 skew() 的变形属性来对这个矩形进行斜向拉伸

  1. .box{
  2. width: 200px;
  3. height: 100px;
  4. background-color: blue;
  5. transform: skewX(-45deg);
  6. }
  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/22615031/1632271378234-1c4197d7-88c1-4b93-9f86-178504e9dc58.png#clientId=u81b03a70-a879-4&from=paste&height=83&id=ud08e5f23&margin=%5Bobject%20Object%5D&name=image.png&originHeight=166&originWidth=348&originalType=binary&ratio=1&size=2392&status=done&style=none&taskId=ua7f4bba4-45e1-4516-b4b4-8f138e553bb&width=174)

有没有办法只让容器的形状倾斜,而保持其内容不变呢?
我们可以对内容再应用一次反向的 skew() 变形,从而抵消容器的变形 效果,

  1. .button{
  2. display: block;
  3. width: 100px;
  4. height: 50px;
  5. background-color: red;
  6. transform: skewX(-45deg);
  7. }
  8. .button>div{
  9. transform: skewX(45deg);
  10. }
  1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/22615031/1632271654687-95cbab0e-dff4-4330-94fe-4d06d04e76f8.png#clientId=u81b03a70-a879-4&from=paste&height=93&id=u21b4af5e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=93&originWidth=200&originalType=binary&ratio=1&size=1615&status=done&style=none&taskId=u1f0bf5fe-cb8a-4a80-96ce-2c4b5e9716e&width=201)

嵌套元素方案

  1. .button{
  2. position: relative;
  3. }
  4. .button::before{
  5. content: '';
  6. position: absolute;
  7. top: 0;
  8. right: 0;
  9. bottom: 0;
  10. left: 0;
  11. transform: skewX(-45deg);
  12. background-color: black;
  13. z-index: -1;
  14. }
  15. <a href="#yolo" class="button">
  16. Click me
  17. </a>

image.png