CSS diagonal section divider with background image

老子叫甜甜 提交于 2019-12-13 08:35:47

问题


I'm trying to create diagonal lines between sections of my page using transforms. It works perfectly with a background colour, see: http://jsfiddle.net/jme11/D9M2L/ but not with a background image: http://jsfiddle.net/D9M2L/224/ Why is this?

section.diagonal {
    background-image: url('image.jpg');
}

.diagonal:before {
    -webkit-transform: rotate(-3deg);
    transform: rotate(-3deg);
    -webkit-transform-origin: 3% 0;
    transform-origin: 3% 0;
    top: 0;
    left: -25%;
    z-index: -1;
    width: 150%;
    height: 75%;
    background: inherit;
}

回答1:


Your JSFiddle is almost working, if you replace http://placekitten.com/200/300 with an url to an image, se this JSFiddle. I made some changes to the HTML and CSS.

HTML:

<header>
    <p>Header</p>
</header>
<section class="diagonal">
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
</section>
<footer>
    <p>Footer</p>    
</footer>

CSS:

body {
    background-color: #000;
    margin: 0px;
}
p {
    color: white;
}

section {
    position: relative;
    color: #fff;
    text-align: center;
}

section:before {
    position: absolute;
    content:'';
}

footer {
    position: relative;
    color: #fff;
    text-align: center;
}
footer:before {
    position: absolute;
    content:'';
}
footer.diagonal {
    background: black;
}
.diagonal {
    z-index: 1;
    padding: 3em;
}
.diagonal:before {
    -webkit-transform: rotate(-3deg);
    transform: rotate(-3deg);
    -webkit-transform-origin: 3% 0;
    transform-origin: 3% 0;
    top: 0;
    left: -25%;
    z-index: -1;
    width: 150%;
    height: 100%;
    background-image: url('http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg');
    background-repeat: no-repeat;
    background-size: cover;
}



回答2:


Your background image is set on your div having class="diagonal". Your rotation is made on this div pseudo-element before.

If you want your background image to rotate, you should add your background image on your pseudo-element and remove it from your div.

Something like below:

section.diagonal {
}

.diagonal:before {
    -webkit-transform: rotate(-3deg);
    transform: rotate(-3deg);
    -webkit-transform-origin: 3% 0;
    transform-origin: 3% 0;
    top: 0;
    left: -25%;
    z-index: -1;
    width: 150%;
    height: 75%;
    background-image: url('image.jpg');
}


来源:https://stackoverflow.com/questions/33783833/css-diagonal-section-divider-with-background-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!