1

I browsing around the internet and I stumbled upon this theme on reddit

enter image description here

After seeing this lovely banner. I wanted to try and emulate something like this using five symmetric images of my choice that is separated by diagonal lines just like the picture above. Maybe additionally try and put in some text on top of the assorted images. Something like this:

enter image description here

I tried re-writing something similarly online in css using cats picture

.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;
}

However, not all the cat pictures are inside of box and are also not evenly distributed

<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

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
iron2man
  • 1,787
  • 5
  • 27
  • 39

1 Answers1

4

No need to use positioned element, you can simplify like this and use background-position to center the element:

.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;
}
<div class="container">
  <div class="box" style="--i:url(https://picsum.photos/id/16/400/300)"></div>
  <div class="box" style="--i:url(https://picsum.photos/id/18/400/300)"></div>
  <div class="box" style="--i:url(https://picsum.photos/id/11/400/300)"></div>
  <div class="box" style="--i:url(https://picsum.photos/id/35/400/300)"></div>
  <div class="box" style="--i:url(https://picsum.photos/id/42/400/300)"></div>
</div>

UPDATE

Here is another version of the code which is more supported (especially for IE):

.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;
}
<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>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Wow, I can't believe it that much simpler. Thank you! – iron2man Mar 14 '18 at 20:48
  • @RokoC.Buljan i added another more supported method in case he came back frustrated but let's hope no :p – Temani Afif Mar 14 '18 at 20:57
  • @TemaniAfif No worries! It works. Even though you have already answered my question, I'm curious on you can put text over this? I'm looking at examples that nest it with `container`, but I have nothing show up. – iron2man Mar 14 '18 at 22:22
  • @Bhuwan the main issue is the use of img in this case ... with img you are obliged to specify dimension in order to make it fit your content like you want and that's why i used them as background which is a lot easier. So yes it can be done with img but it will become very trikcy – Temani Afif Apr 17 '18 at 09:38
  • @TemaniAfif, the same thing is possible with the banner image? I mean, there is a big banner image that is slanted at the bottom, and then the bottom div also has some background image, I was trying to create this using your approach, but no luck, any help on this: https://i.ibb.co/z55kG4z/Screenshot-2020-12-14-164933.png , Check the banner and program section – Atul Rajput Dec 14 '20 at 11:21
  • @TemaniAfif, and the thing is I can not use clip path for this – Atul Rajput Dec 14 '20 at 11:22
  • @AtulRajput check these: https://stackoverflow.com/a/60025185/8620333 / https://stackoverflow.com/a/59127314/8620333 – Temani Afif Dec 14 '20 at 14:51