Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cutting image diagonally with CSS

How to cut away part of an image or container diagonally using CSS?

The part that needs to be cut away has the form of a triangle

Example of image

To be more specific: if the above picture is the image the blue part should be cut out, not the yellow

The html should be:

<figure>
 <img src="img_pulpit.jpg" alt="The Pulpit Rock">
</figure>

or:

<div class="container">
  content
</div>

There is from my own investigation a lot of ways to do this, but most of them is hacky, so looking for a best approach

Min browser support: IE11, latest webkits etc.

like image 916
Doff3n Avatar asked Mar 25 '15 15:03

Doff3n


People also ask

How do I cut an image diagonally in CSS?

-You can use http://cssplant.com/clip-path-generator for this. It's just a "dirty solution", the best way is placing a svg above the img. Maybe in a future, the "clip css property" would support another kind of shapes than just "rect" and things like this could be done!

How do I create a diagonal div in CSS?

You could use CSS3 transform skewY() function with positive value for the parent and negative value for the child wrapper element to achieve the effect.

How do you make a diagonal border in CSS?

So, to draw a diagonal line in CSS, we have to simply rotate a normal horizontal line at an angle of + or – 45 degrees. The rotation in CSS is done using the transform property. To create a horizontal line you can either use the border-top or the border-bottom property whichever best suits you.


1 Answers

Use CSS3 -clip-path and polygon like this. You can specify whatever path you want to.

img {
  width: 100px;
  height: 100px;
  -webkit-clip-path: polygon(0 0, 0 100px, 100px 80px, 100px 0);
  clip-path: polygon(0 0, 0 100px, 100px 80px, 100px 0);
}
<img src="https://picsum.photos/id/0/100/100">

http://jsfiddle.net/r3p9ph5k/

For some extra bits you might want to take a look at e.g. this. Also, for more information about IE, see this.

like image 188
Roope Avatar answered Sep 23 '22 09:09

Roope