问题
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