4

I have been searching for a few days now for code to make the right edge of a div slant 45 degrees. Here is an image example of what I am particularly attempting to get...

Enter image description here

There seems to be a lot of examples of 'slanted-edge' divs, but I can't find any with the particular right-side slanted.

I have spend a great deal of time trying to alter the codes of the others, but it ends up in a mess.

This is the original CSS code I was experimenting with to get the results I need...

    div {
        position: relative;
        display: inline-block;
        padding: 1em 5em 1em 1em;
        overflow: hidden;
        color: #fff;
    }

    div:after {
        content: '';
        position: absolute;
        top: 0; left: 0;
        width: 100%; height: 100%;
        background: #000;
        -webkit-transform-origin: 100% 0;
        -ms-transform-origin: 100% 0;
        transform-origin: 100% 0;
        -webkit-transform: skew(-45deg);
        -ms-transform: skew(-45deg);
        transform: skew(-45deg);
        z-index: -1;
    }

    body {
        background:
        url('https://farm3.staticflickr.com/2878/10944255073_973d2cd25c.jpg');
        background-size: cover;
    }

Here is the HTML....

    <div>Slanted div text</div>
    <div>
        Slanted div text<br/>
        on several lines<br/>
        Another line
    </div>
    <div>Wider slanted div text with more text inside</div>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamie Sexton
  • 157
  • 1
  • 4
  • 16
  • You would need to combine 2 or 3 divs, the inside one having a negative right offset and rotated via something like `transform: rotate(45deg)` – Keith M Jun 22 '17 at 23:36
  • Keith... I was I understood 'offsets' enough to experiment with your suggestion. – Jamie Sexton Jun 22 '17 at 23:38

1 Answers1

4

Create your div, then overlay an absolutely-positioned, rotated pseudo-element to create the slanted impression.

div {
  height: 50px;
  width: 300px;
  background-color: black;
  position: relative;
  overflow: hidden;
}

div:after {
  height: 100%;
  width: 100%;
  background-color: white;
  position: absolute;
  content: "";
  transform: rotate(45deg);
  transform-origin: bottom right;
}
<div></div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701