How to reach the sloping right edge?

I searched for a few days for the code to make the right edge of the div tilt 45 degrees. Here is an example image of what I am especially trying to get ...

Enter a description of the image here.

There seem to be many examples of “sloping borders” of the div, but I cannot find them with a specific right slope.

I spent a lot of time trying to change the codes of others, but it ends up in a mess.

This is the original CSS code that I experimented 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>
+4
source share
1 answer

Create your div, then impose an absolutely positioned, rotated pseudo-element to create an oblique 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>
Run codeHide result
+2

Source: https://habr.com/ru/post/1679926/


All Articles