如何用CSS歪斜图像?

时间:2015-07-17 09:29:09

标签: jquery html5 css3 transform skew

我想扭曲像这样的图像

enter image description here

我申请div时正常工作

enter image description here

现在我想将此代码用于img标记,但它无法正常工作。

enter image description here

这是我的实时代码:

https://jsfiddle.net/dbkvdq3u/5/



.steps-widget .step-img-box {
  position: relative;
  text-align: center;
  padding: 12px;
  margin-bottom: 6px;
  height: 60px;
}
.steps-widget .step-img-box:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 51%;
  z-index: -1;
  -webkit-transform: skew(0deg, 6deg);
  -moz-transform: skew(0deg, 6deg);
  -ms-transform: skew(0deg, 6deg);
  -o-transform: skew(0deg, 6deg);
  transform: skew(0deg, 6deg);
}
.steps-widget .step-img-box:after {
  z-index: -1;
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  width: 50%;
  -webkit-transform: skew(0deg, -6deg);
  -moz-transform: skew(0deg, -6deg);
  -ms-transform: skew(0deg, -6deg);
  -o-transform: skew(0deg, -6deg);
  transform: skew(0deg, -6deg);
}

<div class="step-img-box">
  <img class="img-responsive" src="http://i.imgur.com/E7atspO.jpg" alt="Image">
</div>
<!-- end step-img-box -->
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:5)

您可以使用伪元素,使用背景位置属性来对齐图像的两个“半部分”:

div {
  height: 300px;
  width: 300px;
  position: relative;
  margin: 100px auto;
}
div:before,
div:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 50%;
  background-size: 300px 300px;
}
div:before {
  transform: skewY(10deg);
}
div:after {
  left: 50%;
  background-position: -100% 0;
  transform: skewY(-10deg);
}
.sk:before, .sk:after{  background-image: url(http://lorempixel.com/300/300);}
.sk2:before, .sk2:after{  background-image: url(http://lorempixel.com/400/400);}
<div class="sk"></div>
<div class="sk2"></div>