Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut Corners using CSS

I'm looking to "cut" the top left corner of a div, like if you had folded the corner of a page down.

I'd like to do it in pure CSS, are there any methods?

like image 424
Rixius Avatar asked Sep 06 '11 18:09

Rixius


People also ask

How do I cut a corner in CSS?

A combination of 3 gradient images (given below) is used: One linear gradient (angled towards bottom left) to produce the cut corner effect. This gradient has a fixed 25px x 25px size. One linear gradient to provide a solid color to the left of the triangle that causes the cut effect.

How do you cut border radius in CSS?

CSS Syntaxborder-radius: 1-4 length|% / 1-4 length|%|initial|inherit; Note: The four values for each radius are given in the order top-left, top-right, bottom-right, bottom-left. If bottom-left is omitted it is the same as top-right. If bottom-right is omitted it is the same as top-left.


2 Answers

If the parent element has a solid color background, you can use pseudo-elements to create the effect:

div {      height: 300px;      background: red;      position: relative;  }    div:before {      content: '';      position: absolute;      top: 0; right: 0;      border-top: 80px solid white;      border-left: 80px solid red;      width: 0;  }
<div></div>

http://jsfiddle.net/2bZAW/


P.S. The upcoming border-corner-shape is exactly what you're looking for. Too bad it might get cut out of the spec, and never make it into any browsers in the wild :(

like image 193
Joseph Silber Avatar answered Oct 12 '22 20:10

Joseph Silber


If you need a transparent cut out edge, you can use a rotated pseudo element as a background for the div and position it to cut out the desired corner:

Transprent cut out edge on a div

body {    background: url(http://i.imgur.com/k8BtMvj.jpg);    background-size: cover;  }  div {    position: relative;    width: 50%;    margin: 0 auto;    overflow: hidden;    padding: 20px;    text-align: center;  }  div:after {    content: '';    position: absolute;    width: 1100%; height: 1100%;    top: 20px; right: -500%;    background: rgba(255,255,255,.8);    transform-origin: 54% 0;    transform: rotate(45deg);    z-index: -1;  }
<div>    ... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>  </div>

Note that this solution uses transforms and you need to add the required vendor prefixes. For more info see canIuse.

To cut the bottom right edge, you can change the top, transform and transform-origin properties of the pseudo element to:

body {    background: url(http://i.imgur.com/k8BtMvj.jpg);    background-size: cover;  }  div {    position: relative;    width: 50%;    margin: 0 auto;    overflow: hidden;    padding: 20px;    text-align: center;  }  div:after {    content: '';    position: absolute;    width: 1100%; height: 1100%;    bottom: 20px; right: -500%;    background: rgba(255,255,255,.8);    transform-origin: 54% 100%;    transform: rotate(-45deg);    z-index: -1;  }
<div>    ... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>... content ...<br/>  </div>
like image 39
web-tiki Avatar answered Oct 12 '22 20:10

web-tiki