5个图像用对角线对称分离

时间:2018-03-14 20:29:36

标签: html css css3 css-transforms

我浏览互联网,我在reddit上偶然发现了这个主题

enter image description here

看到这个可爱的横幅后。我想尝试使用我选择的五个对称图像来模拟这样的东西,这些图像由对角线分开,就像上面的图片一样。也许另外尝试在各种图像的顶部放置一些文本。像这样:

enter image description here

我尝试使用猫图片

在css中重新编写类似的东西
.image-container {
  width: 90%;
  height: 200px;
  position: relative;
  margin: 30px auto;
  background: black;
  overflow: hidden;
}


.image-one {
  right: 20%;
}
.image-two {
  right: 20%;
}
.image-three {
  right: 20%;
}
.image-four {
  right: 20%;
}
.image-five {
  right: 20%;
}


.image-one,
.image-two,
.image-three,
.image-four,
.image-five {
  position: absolute;
  height: 100%;
  width: 40%;
  min-width: 20px;
  -ms-transform: skewX(-25deg);
  -webkit-transform: skewX(-25deg);
  transform: skewX(-25deg);
  background: white;
  overflow: hidden;
 top:0;
  margin-right: 50px;
  border: 5px solid black;
  border-top: 0;
  border-bottom: 0;
  box-shadow: 0 0 10px 0 rgba(0,0,0,0.9);
}

.image-one:after, .image-two:after,
.image-three:after, .image-four:after,
.image-five:after{
  -ms-transform: skewX(25deg);
  -webkit-transform: skewX(25deg);
  transform: skewX(25deg);
  position: absolute;
  width: 120%;
  height: 100%;
  display: block;
  top: 0;
  content: "";
}

.image-one:after{
  right: -93px;
  background: url("http://lorempixel.com/500/400/cats") no-repeat center center;
  background-size: cover;
}
.image-two:after {
  left: -93px;
  background: url("http://lorempixel.com/500/401/cats") no-repeat center center;
  background-size: cover;
}
.image-three:after {
  left: -93px;
  background: url("http://lorempixel.com/500/401/cats") no-repeat center center;
  background-size: cover;
}
.image-four:after {
  left: -93px;
  background: url("http://lorempixel.com/500/401/cats") no-repeat center center;
  background-size: cover;
}
.image-five:after {
  left: -93px;
  background: url("http://lorempixel.com/500/401/cats") no-repeat center center;
      background-size: cover;
}

然而,并非所有的猫图片都在盒子内,而且分布不均匀

<div class='image-container'>
  <div class='image-left'></div>
  <div class='image-right'></div>
</div>


<div class='image-container'>
  <div class='image-one'></div>
    <div class='image-two'></div>
        <div class='image-three'></div>
            <div class='image-four'></div>
                <div class='image-five'></div>
</div>

enter image description here

1 个答案:

答案 0 :(得分:3)

无需使用定位元素,您可以像这样简化并使用background-position将元素居中:

&#13;
&#13;
.container {
  display: flex;
  height: 150px;
  margin: 0 30px;
}

.box {
  flex: 1;
  border: 1px solid;
  transform: skew(-25deg);
  position: relative;
  overflow: hidden;
}

.box:after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: -50%;
  right: -50%;
  transform: skew(25deg);
  background-image: var(--i);
  background-position: center;
}
&#13;
<div class="container">
  <div class="box" style="--i:url(https://lorempixel.com/400/200/)"></div>
  <div class="box" style="--i:url(https://lorempixel.com/400/300/)"></div>
  <div class="box" style="--i:url(https://lorempixel.com/300/200/)"></div>
  <div class="box" style="--i:url(https://lorempixel.com/400/300/)"></div>
  <div class="box" style="--i:url(https://lorempixel.com/200/300/)"></div>
</div>
&#13;
&#13;
&#13;

<强>更新

这是另一个更受支持的代码版本(特别是对于IE):

&#13;
&#13;
.container {
  font-size:0;
  height: 150px;
  margin: 0 30px;
}

.box {
  font-size:initial;
  width:calc(100% / 5);
  height:100%;
  border: 1px solid;
  box-sizing:border-box;
  transform: skew(-25deg);
  position: relative;
  overflow: hidden;
  display:inline-block;
}

.box span {
  position: absolute;
  top: 0;
  bottom: 0;
  left: -50%;
  right: -50%;
  transform: skew(25deg);
  background-position: center;
  background-size:cover;
}
&#13;
<div class="container">
  <div class="box">
    <span style="background-image:url(https://lorempixel.com/400/200/)"></span>
  </div>
  <div class="box">
    <span style="background-image:url(https://lorempixel.com/400/300/)"></span>
  </div>
  <div class="box">
    <span style="background-image:url(https://lorempixel.com/200/200/)"></span>  
  </div>
  <div class="box">
      <span style="background-image:url(https://lorempixel.com/300/200/)"></span>
  </div>
  <div class="box">
     <span style="background-image:url(https://lorempixel.com/400/400/)"></span>
  </div>
</div>
&#13;
&#13;
&#13;