How do I unskew background image in skewed layer (CSS)?

后端 未结 1 1887
礼貌的吻别
礼貌的吻别 2020-12-15 23:07

I\'m trying to display a profile photo like this / - / (the slashes represent slants using skewX, the hyphen represents a horizontally-aligned background image).

The

1条回答
  •  温柔的废话
    2020-12-15 23:38

    I'd rather use a pseudo element that's holding the background-image. The key to the solution is using transform-origin:

    Example

    .photo {
        transform: skewX(35deg); 
        -ms-transform: skewX(35deg); /* IE 9 */
        -webkit-transform: skewX(35deg); /* Safari and Chrome */
        width: 100px; 
        height: 92px; 
        border-right: 1px solid black; 
        border-left: 1px solid black; 
    
        /* new styles */
        position: relative;
        overflow: hidden;
        -webkit-transform-origin: top left;
        -ms-transform-origin: top left;
        transform-origin: top left;
    }
    
    .photo::before {
        content: "";
        transform: skewX(-35deg); 
        -ms-transform: skewX(-35deg); /* IE 9 */
        -webkit-transform: skewX(-35deg); /* Safari and Chrome */
        background-image: url('http://placekitten.com/200/200'); 
        background-repeat: no-repeat; 
        background-position: top left; 
    
        /* new styles */
        position: absolute;
        -webkit-transform-origin: top left;
        -ms-transform-origin: top left;
        transform-origin: top left;
        width: 1000%; /* something ridiculously big */
        height: 1000%; /* something ridiculously big */
    }
    

    0 讨论(0)
提交回复
热议问题