Can I create this shape with CSS only?

前端 未结 1 603
名媛妹妹
名媛妹妹 2021-01-03 10:11

I\'m building a hero section for a webpage that has a particular shape, at the moment I\'m just using an image as an overlay for the actual section background, but I\'m look

相关标签:
1条回答
  • 2021-01-03 10:51

    Here is an idea with one element and radial-gradient to approximate it

    .box {
      width: 400px;
      height: 200px;
      display:inline-block;
      overflow:hidden;
      position:relative;
    }
    .box:before,
    .box:after{
      content:"";
      position:absolute;
      top:0;
      left:0;
      right:50%;
      bottom:0;
      background:
        radial-gradient(100% 116.3% at top   right, transparent 99%,#fff 100%) top,
        radial-gradient(100% 116.3% at bottom left, #fff 99%,transparent 100%) bottom;
      background-size:100% 50%;
      background-repeat:no-repeat;
    }
    .box:after {
      right:0;
      left:50%;
      transform:scaleX(-1);
    }
    
    body {
      background:linear-gradient(to right, purple, blue);
    }
    <div class="box">
    
    </div>

    You can then adjust left/right/bottom properties to control the overal shape by having some oveflow and overlap:

    .box {
      width: 400px;
      height: 200px;
      display:inline-block;
      overflow:hidden;
      position:relative;
    }
    .box:before,
    .box:after{
      content:"";
      position:absolute;
      top:0;
      left:-2px;
      right:40%;
      bottom:-45%;
      background:
        radial-gradient(100% 116.3% at top   right, transparent 99%,#fff 100%) top,
        radial-gradient(100% 116.3% at bottom left, #fff 99%,transparent 100%) bottom;
      background-size:100% 50%;
      background-repeat:no-repeat;
    }
    .box:after {
      right:-2px;
      left:40%;
      transform:scaleX(-1);
    }
    
    body {
      background:linear-gradient(to right, purple, blue);
    }
    <div class="box">
    
    </div>


    Here is an idea using SVG to replace the radial-gradient:

    .box {
      height: 200px;
      overflow:hidden;
      position:relative;
    }
    .box:before,
    .box:after{
      content:"";
      position:absolute;
      top:0;
      left:0;
      right:50%;
      bottom:0;
      background:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" preserveAspectRatio="none"><path fill="white" d="M64 64 C56 30 8 48 0 0 L0 64 Z"/></svg>');
      background-size:100% 100%;
    }
    .box:after {
      right:0;
      left:50%;
      transform:scaleX(-1);
    }
    
    body {
      background:linear-gradient(to right, purple, blue);
    }
    <div class="box">
    
    </div>

    Here is a good online tool to edit the SVG: http://jxnblk.com/paths/. Simply append the path to the url at the end and edit it;

    http://jxnblk.com/paths/?d=M64 64 C56 30 8 48 0 0 L0 64 Z
    
    0 讨论(0)
提交回复
热议问题