div with triangle at the bottom with background image

后端 未结 3 1788
野趣味
野趣味 2020-12-11 04:02

I want to make a div, with a triangle at the bottom. But I need the background image on the triangle to appear, I\'ve tried us

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 04:34

    Triangle over a plain color

    If the triangle is displayed over a plain color, you can use this approach with an absolutely positioned pseudo element :

    div{
        position:relative;
        background:url('http://i.imgur.com/W27LCzB.jpg');
        background-size:cover;
        min-height:100px;
        padding-bottom:100px;
        overflow:hidden;
    }
    div:after{
        content:'';
        position:absolute;
        bottom:0; left:0;
        border-left:50vw solid #fff;
        border-right:50vw solid #fff;
        border-top:100px solid transparent;
    }

    The left and right parts of the triangle are hidden by the left and right borders of the pseudo element. That is why this approach won't work over a gradient or image.


    Triangle over an image or gradient

    In these cases, you can use an inline svg with clipPath and a polygon element :

    body, html{
      height:100%;
      background:url('https://farm8.staticflickr.com/7187/6895047173_d4b1a0d798.jpg')no-repeat center center;
      background-size:cover;
    }
    svg{
      display:block;
      width:100%;
      }
    
      
        
      
      
    

    There are other possible approaches for the same result. You can find some here : CSS Transparent arrow/triangle

提交回复
热议问题