4

How can I mask an image with a css shape?

reference Image

enter image description here

I already made a shape with css border style but not able to overlay an image.

CSS

#triangle-topleft {
  width: 0;
  height: 0; 
  border-top: 100px solid red;
  border-right: 100px solid transparent; 
}
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Twinxz
  • 303
  • 5
  • 16

2 Answers2

3

Option 1 - SVG

<svg width="100" height="100">
  <defs>
    <pattern id="a" patternUnits="userSpaceOnUse" width="100" height="100">
      <image xlink:href="http://lorempixel.com/300/300" x="0" y="0" width="100" height="100" />
    </pattern>
  </defs>
  <polygon points="0,0 100,0 50,100" fill="url(#a)" />
</svg>

Tested on Chrome, Firefox, IE9+


Option 2 - clip-path

.element {
  display:inline-block;
  -webkit-clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
<div class="element">
  <img src="https://s3.amazonaws.com/uifaces/faces/twitter/ok/128.jpg" />
</div>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
2

For that you can use gradients. Here is an example code:

div {
  width: 100px;
  height: 100px;
  background-image: linear-gradient(to bottom left, white 50%, transparent 0),        
    url('http://lorempixel.com/100/100');
}
<div></div>

Working fiddle

Mr_Green
  • 40,727
  • 45
  • 159
  • 271