【问题标题】:How to set a custom shaped background using CSS clip-path property?如何使用 CSS 剪辑路径属性设置自定义形状的背景?
【发布时间】:2019-02-07 00:35:51
【问题描述】:

我正在努力使用clip-path 属性以创建抽象背景,并且我不想使用图像或 svg 文件,我尝试使用该属性但我无法实现此结果: enter image description here

我的基本代码:

.bg{
  height: 100vh;
    margin: 0;
}
.shape-1{
     -webkit-clip-path: polygon(0 0, 10% 45%, 100% 0);
    clip-path: polygon(0 10%, 40% 36%, 100% 0);
    background:  #3e19c6;
    height: 100%;
    width: 100%;
    position: absolute;
    margin: 0;
    z-index: -1; 
}


.shape-2{
  -webkit-clip-path: polygon(0 62%, 100% 21%, 100% 100%, 0% 100%);
    clip-path: polygon(0 62%, 90% 21%, 100% 100%, 0% 85%);
background:  #c61951;
height: 100%;
width: 100%;
position: absolute;
margin: 0;
z-index: -1;   
}
<div class="bg">
    <div class="shape-1">    </div>
    <div class="shape-2">    </div>
</div>

【问题讨论】:

    标签: html css clip-path


    【解决方案1】:

    考虑到多个背景和渐变以及只有一个元素,您可以实现这一点。它也会响应:

    body {
     margin:0;
     height:100vh;
     background:
      linear-gradient(to top left,transparent 49.5%, #3e19c6 50%) top/100% 30%,
      linear-gradient(to bottom right,transparent 49.5%, #c61951 50%) 0 30%/100% 30%,
      linear-gradient(#c61951,#c61951) bottom/100% 49.1%;
      
     background-repeat:no-repeat;
    }

    这是另一个使用倾斜变换和伪元素的想法:

    body {
     margin:0;
     height:100vh;
     position:relative;
     overflow:hidden;
    }
    body::before,
    body::after {
      content:"";
      position:absolute;
      left:0;
      width:100%;
      transform-origin:right;
      transform:skewY(-8deg);
    
    }
    
    body::before {
      bottom:100%;
      height:100vh;
      background:#3e19c6;
    }
    
    body::after {
      bottom:0;
      height:80%;
      background:#c61951;
    }

    这里是clip-path 解决方案:

    body {
     margin:0;
     height:100vh;
     position:relative;
     overflow:hidden;
    }
    body::before,
    body::after {
      content:"";
      position:absolute;
      left:0;
      width:100%;
    
    }
    
    body::before {
      top:0;
      height:25%;
      background:#3e19c6;
      -webkit-clip-path:polygon(0 0,100% 0,0 100%);
      clip-path:polygon(0 0,100% 0,0 100%);
    }
    
    body::after {
      bottom:0;
      height:75%;
      background:#c61951;
      -webkit-clip-path:polygon(0% 33.33%,100% 0,100% 100%,0 100%);
      clip-path:polygon(0% 33.33%,100% 0,100% 100%,0 100%);
    }

    【讨论】:

    • 但它会产生模糊的形状
    • @alexisdavid 只需调整色标的值,检查更新
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2016-08-03
    • 2014-04-21
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多