So i want to allign my images in 2x1x2x1x2 grid format as shown in the image, but then the image repeated but i cant seem to figure out how to get them like this. I'm not really familiar with how grid works, and i cant seem to figure it out. I made them flex, and messed around a bit with position: absolute and stuff but they either get send all the way to the top of my page where my navigation and stuff is. With what i have now the all of the images are stuck to eachother which is good but its only in a column. The code below goes on with a couple more images of the same format.
img {
width: 50%;
}
#img-layout {
display: flex;
}
.img-lion {
float: left;
}
.img-water {
float: right;
}
<div >
<div id="img-layout">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1459800606415-5B8BDTCZ866H7OTNQMYL/portrait-black.jpg?format=1500w" alt="" />
</div>
<div id="img-layout">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452896039354-F8P5FPCM3V9HMWHWZ0FE/main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1456767403805-KN8Z62OADRLLPKN8YA4P/nytimes-main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1453940592760-5OWKFJCJKG133RHSDOMK/main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452634113588-M32DA4VU1QCEBLOKWCBV/main.jpg?format=1500w" alt="" />
</div>
</div>
So as i said i already messed around with some of the options such as float values, flex proporties but i just cant seem to figure it out how it works. I looked it up on the internet but i either dont really understand it, or it doesnt seem to work. Hope someone can help ne
CodePudding user response:
This is fairly easy to do with grid. I've put in an example below and a comment on each relevant section to explain how it works.
Also each id attribute should be unique.
.container-images {
/* set up a grid */
display:grid;
/* tell it that we want 2 columns and each column is equal width */
grid-template-columns: repeat(2, 1fr);
}
img {
/* make the image fill the entire container then clip the image as best the browser can to fill it */
width: 100%;
height:100%;
object-fit: cover;
}
.container-images > div:nth-child(3n 3) {
/* every 3rd element start the image at the left hand track but span the two columns */
grid-column: 1 / span 2;
}
<div >
<div id="img-layout1">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1459800606415-5B8BDTCZ866H7OTNQMYL/portrait-black.jpg?format=1500w" alt="" />
</div>
<div id="img-layout2">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452896039354-F8P5FPCM3V9HMWHWZ0FE/main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout3">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1456767403805-KN8Z62OADRLLPKN8YA4P/nytimes-main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout4">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1453940592760-5OWKFJCJKG133RHSDOMK/main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout5">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452634113588-M32DA4VU1QCEBLOKWCBV/main.jpg?format=1500w" alt="" />
</div>
<div id="img-layout6">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1459800606415-5B8BDTCZ866H7OTNQMYL/portrait-black.jpg?format=1500w" alt="" />
</div>
<div id="img-layout7">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452896039354-F8P5FPCM3V9HMWHWZ0FE/main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout8">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1456767403805-KN8Z62OADRLLPKN8YA4P/nytimes-main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout9">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1453940592760-5OWKFJCJKG133RHSDOMK/main.jpg?format=1000w" alt="" />
</div>
<div id="img-layout10">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452634113588-M32DA4VU1QCEBLOKWCBV/main.jpg?format=1500w" alt="" />
</div>
<div id="img-layout11">
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1459800606415-5B8BDTCZ866H7OTNQMYL/portrait-black.jpg?format=1500w" alt="" />
</div>
</div>
CodePudding user response:
Here's a simple solution using CSS flex property which is quite simple and easy to deal with:
.container-images {
display: flex;
width: 100%;
flex-wrap: wrap;
}
.container-images img {
width: 50%;
height: 200px;
object-fit: cover;
}
.container-images img:nth-child(3n) {
width: 100%;
}
<div >
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1459800606415-5B8BDTCZ866H7OTNQMYL/portrait-black.jpg?format=1500w" alt="" />
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452896039354-F8P5FPCM3V9HMWHWZ0FE/main.jpg?format=1000w" alt="" />
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1456767403805-KN8Z62OADRLLPKN8YA4P/nytimes-main.jpg?format=1000w" alt="" />
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1453940592760-5OWKFJCJKG133RHSDOMK/main.jpg?format=1000w" alt="" />
<img src="https://images.squarespace-cdn.com/content/v1/563bdd38e4b0283238934c89/1452634113588-M32DA4VU1QCEBLOKWCBV/main.jpg?format=1500w" alt="" />
</div>