css实现平行四边形


方法一:嵌套元素

我们对外层元素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;

        }



©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容