Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an angled corner in CSS?

Tags:

css

css-shapes

I am wondering if there is any way to create this shape with pure CSS. To extend this problem further, this shape needs to clip the image inside (think of it as a mask - but the grey border has to be visible).

enter image description here

Or am I better off creating this in canvas/svg?

like image 739
user2307706 Avatar asked Oct 08 '13 13:10

user2307706


People also ask

How do you make a triangle corner on a div in CSS?

You can use position: absolute on triangle element and set top and right properties to 0. You can also just use pseudo-element with absolute position for triangle.


1 Answers

It's a little difficult keeping the border, but I managed to achieve a close effect using :before and :after elements with a parent container (:before and :after don't work on an img tag)

  1. Add a border to the container

  2. Add a before to block out a corner and offset by -1 to cover the border

  3. Add an after that's slightly offset from the before to create the line inside the cut off

As you can see, the thickness of the 45deg line is a bit of an issue:

.cutCorner {      position:relative; background-color:blue;       border:1px solid silver; display: inline-block;  }    .cutCorner img {      display:block;  }    .cutCorner:before {      position:absolute; left:-1px; top:-1px; content:'';      border-top: 70px solid silver;      border-right: 70px solid transparent;  }    .cutCorner:after {      position:absolute; left:-2px; top:-2px; content:'';      border-top: 70px solid white;      border-right: 70px solid transparent;  }
<div class="cutCorner">      <img class="" src="https://www.google.co.uk/logos/doodles/2013/william-john-swainsons-224th-birthday-5655612935372800-hp.jpg" />  </div>

JSFiddle

like image 53
Matt.C Avatar answered Sep 18 '22 10:09

Matt.C