Search code examples
htmlcssshapescss-shapes

Use css to create a custom arrow shape


I have a css arrow box overlaying an image slider but I need to make the tail indented and transparent so the image is seen behind it. Need the white portion to be transparent. See attached image and my css below. Thanks.css arrow box over image

#flag { 
width: 400px; height: 80px; background: #231f20; position: relative;
}

#flag:before { 
content: ""; 
position: absolute; 
top: 0; 

width: 0; 
height: 0; 
border-top: 40px solid transparent; 

border-bottom: 40px solid transparent; 

border-left: 35px solid white;

}

#flag:after { 
content: ""; 
position: absolute; 
left: 400px; 
bottom: 0; 
width: 0; 
height: 0; 
border-top: 40px solid transparent;

border-bottom: 40px solid transparent;

border-left: 45px solid #231f20;

}

Solution

  • This is pretty simple. You just need to set the color of top/bottom borders of the ::before pseudo-element to the background color and change the color of left border to transparent.

    Then you could use margins/offsets to position the pseudo-element properly.

    body { background-color: gold; }
    
    #flag { 
      width: 400px; height: 80px; background: #231f20; position: relative;
      margin-left: 35px;
      color: white;
      line-height: 80px;
      text-align: center;
    }
    
    #flag:before { 
      content: ""; 
      position: absolute; 
      top: 0; 
      left: -35px;
      width: 0; 
      height: 0; 
      border-top: 40px solid #231f20; 
      border-bottom: 40px solid #231f20; 
      border-left: 35px solid transparent;
    
    }
    
    #flag:after { 
      content: ""; 
      position: absolute; 
      left: 400px; 
      bottom: 0; 
      width: 0; 
      height: 0; 
      border-top: 40px solid transparent;
    
      border-bottom: 40px solid transparent;
    
      border-left: 45px solid #231f20;
    }
    <div id="flag">This is it!</div>