Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to mask or clip an image's shape using CSS?

Tags:

I am trying to create this design in CSS. Is it possible?

the  design is it:

This is my code:

.triangle{     border-radius: 50%;     width: 120px;     height: 120px; } .triangle img {     width: 120px;     height: 120px; } .triangle::after{     right: 150px;     top: 50%;     border: solid transparent;     content:"";     height:  0px;     width:  0px;     position: absolute;     pointer-events: none;     border-color: white;     border-left-color: white;/*white is the color of the body*/     border-width: 60px;     margin-top: -60px }
<div class="triangle">     <img src="http://deskode.com/images/placeholder/team/07.jpg"> </div>

The triangle is formed, but not in the same way as the image.

jsFiddle

like image 511
grijalvaromero Avatar asked Nov 06 '14 09:11

grijalvaromero


People also ask

Can you mask in CSS?

The mask CSS shorthand property hides an element (partially or fully) by masking or clipping the image at specific points. Note: As well as the properties listed below, the mask shorthand also resets mask-border to its initial value.

How do I mask an image in HTML CSS?

Use an Image as the Mask Layer To use a PNG or an SVG image as the mask layer, use a url() value to pass in the mask layer image. The mask image needs to have a transparent or semi-transparent area. Black indicates fully transparent.

Can I use mask image CSS?

CSS masking gives you the option of using an image as a mask layer. This means that you can use an image, an SVG, or a gradient as your mask, to create interesting effects without an image editor. When you clip an element using the clip-path property the clipped area becomes invisible.

How do I clip an image in CSS?

The clip property lets you specify a rectangle to clip an absolutely positioned element. The rectangle is specified as four coordinates, all from the top-left corner of the element to be clipped. Note: The clip property does not work if "overflow:visible".


2 Answers

This can be achieved using just CSS. Pseudo elements :before and :after are used to make the triangles (positioned relatively to the container), while border-radius creates the rounded corners.

.triangle {      border-radius: 0 60px 60px 0;      height: 120px;      overflow: hidden;          position: relative;      width: 120px;  }  .triangle:before, .triangle:after {      content: "";      height: 0;      left: 0;      position: absolute;      width: 0;  }  .triangle:before {      border-right: 60px solid transparent;      border-top: 60px solid #FFFFFF;      top: 0;  }  .triangle:after {      border-bottom: 60px solid #FFFFFF;      border-right: 60px solid transparent;      bottom: 0;  }
<div class="triangle">      <img alt="" src="http://placehold.it/120x120" />  </div>

JS Fiddle: http://jsfiddle.net/rw7q2te2/1/

like image 65
Hidden Hobbes Avatar answered Oct 04 '22 14:10

Hidden Hobbes


With a single class.

http://jsfiddle.net/koh36dsz/1/

.wedge {   width: 0px;   height: 0px;   border-right: 60px solid transparent;   border-top: 60px solid red;   border-left: 0px solid red;   border-bottom: 60px solid red;  }  <div class='wedge'></div> 
like image 22
superUntitled Avatar answered Oct 04 '22 12:10

superUntitled