Clip div with SVG path

前端 未结 1 1889
自闭症患者
自闭症患者 2020-12-21 19:10

I have two overlapping divs and I am trying to achieve the following effect:

In order to do that my logic is to get the two divs to overlap, c

相关标签:
1条回答
  • 2020-12-21 19:50

    You can use the SVG command clipPath

    <clipPath id="svgPath" >
                <path d="M0,20 C100,80 350,0 500,30 L500,00 L0,0 Z" style="stroke: none; fill:red;"/> 
          </clipPath>
    

    <style>
    .banner_1 {
      min-height: 200px;
      background-color: #41ffffdb;
    }
    .banner_2 {
      min-height: 200px;
      background-color: #ddc141;
      margin-top: -100px;
    }
    </style>
    <div class="banner_1">
    </div>
    
    <div class="banner_2">
        <svg viewBox="0 0 500 500" preserveAspectRatio="xMinYMin meet"> 
    	<defs>
    	<clipPath id="svgPath" >
    	<path d="M0,20 C100,80 350,0 500,30 L500,00 L0,0 Z" style="stroke: none; fill:red;"/> 
      </clipPath>
    	</defs>
    	
    	<rect width="100%" height="100%" fill="#41ffffdb"  clip-path="url(#svgPath)"  />
            
        </svg>
    </div>

    DEMO

    UPD

    Additionally, on the proposal in the comments

    only te bottom image needs to be clipped and it will overlap the top one.

    .banner_1 {
      min-height: 100px;
      background-color: #41ffffdb;
     
    }
    .banner_2 {
      background:url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/hawaii-beach.jpg);
       background-size:cover;
    }
    <div class="banner_1">
    </div>
    
    <div class="banner_2">
        <svg viewBox="0 0 500 500" preserveAspectRatio="xMinYMin meet"> 
    	<defs>
    	<clipPath id="svgPath" >
    	<path d="M0,20 C100,80 350,0 500,30 L500,00 L0,0 Z" style="stroke: none; fill:red;"/> 
      </clipPath>
    	</defs>
    	
    	<rect width="100%" height="100%" fill="#41ffffdb"  clip-path="url(#svgPath)"
    	
            
        </svg>
    </div>

    To replace the lower picture, change the background:url

    DEMO

    0 讨论(0)
提交回复
热议问题