Mogie
Mogie

Reputation: 55

scaling individual images into grid squares?

Hello I'm pretty new to this & I'm trying to figure out how to scale and clip these images to fit into each grid square without having a border...

I also don't know if this is an effective way to set up my images. I'd like to have a different image for each square, but how it's set up now I'd have to make a new .box .inner# for each one. If there is a better way to structure this that'd be really helpful.

.grid {
  margin: 0 auto;
  width: 240vw;
  max-width: 200vh;
  height: 240vw;
  max-height: 200vh;
  font-size: 2rem;
}

.row {
  display: flex;
}

.box {
  background: red;
  margin: 5px;
  color: white;
  font-weight: bold;
  flex: 1 0 auto;
  position: relative;
}

.box:after {
  content: "";
  float: left;
  display: block;
  padding-top: 100%;
}

.box .inner1 {
  background-image: url("https://c1.staticflickr.com/2/1763/29556413048_164120ccb5_b.jpg");
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-shadow: 0px 0px 8px rgb(36, 36, 36), 0px 0px 20px rgba(0, 0, 0, 0.9);
}

.box .inner2 {
  background-image: url("https://c1.staticflickr.com/1/922/43509246041_043aff0334_h.jpg");
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-shadow: 0px 0px 8px rgb(36, 36, 36), 0px 0px 20px rgba(0, 0, 0, 0.9);
}
<div class="grid">
  <div class="row">
    <div class="box">
      <div class="inner1">1</div>
    </div>
    <div class="box">
      <div class="inner1">2</div>
    </div>
    <div class="box">
      <div class="inner1">3</div>
    </div>
    <div class="box">
      <div class="inner1">4</div>
    </div>
  </div>
  <div class="row">
    <div class="box">
      <div class="inner2">5</div>
    </div>
    <div class="box">
      <div class="inner2">6</div>
    </div>
    <div class="box">
      <div class="inner2">7</div>
    </div>
    <div class="box">
      <div class="inner2">8</div>
    </div>
  </div>
</div>

Upvotes: 1

Views: 49

Answers (2)

Dylan Hayles
Dylan Hayles

Reputation: 1

I am not fully sure if this will fix your problem but maybe take off the margin in your .box class in your css file. not enough reputation yet so click this link

Instead of having your margin at 5px change it to 0px and see if that helps.

As for the different images you just need to create a new class for each image an link a new image to that class, go back and link it up like you have with the two previous.

Upvotes: 0

Kosh
Kosh

Reputation: 18444

You might do it like this:

.grid {
  margin: 0 auto;
  width: 240vw;
  max-width: 200vh;
  height: 240vw;
  max-height: 200vh;
  font-size: 2rem;
}

.row {
  display: flex;
}

.box {
  background: red;
  margin: 5px;
  color: white;
  font-weight: bold;
  flex: 1 0 auto;
  position: relative;
}

.box:after {
  content: "";
  float: left;
  display: block;
  padding-top: 100%;
}

.box > div {
  background-size:cover;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-shadow: 0px 0px 8px rgb(36, 36, 36), 0px 0px 20px rgba(0, 0, 0, 0.9);
}

.inner1 {
background-image: url("https://c1.staticflickr.com/2/1763/29556413048_164120ccb5_b.jpg");
}
.inner2 {
  background-image: url("https://c1.staticflickr.com/1/922/43509246041_043aff0334_h.jpg");
}
.inner3 {
  background-image: url("https://picsum.photos/200/200?3");
}
.inner4 {
  background-image: url("https://picsum.photos/200/200?4");
}
.inner5 {
  background-image: url("https://picsum.photos/200/300?5");
}
.inner6 {
  background-image: url("https://picsum.photos/200/300?6");
}
.inner7 {
  background-image: url("https://picsum.photos/200/200?7");
}
.inner8 {
  background-image: url("https://picsum.photos/200/300?8");
}
<div class="grid">
  <div class="row">
    <div class="box">
      <div class="inner1">1</div>
    </div>
    <div class="box">
      <div class="inner2">2</div>
    </div>
    <div class="box">
      <div class="inner3">3</div>
    </div>
    <div class="box">
      <div class="inner4">4</div>
    </div>
  </div>
  <div class="row">
    <div class="box">
      <div class="inner5">5</div>
    </div>
    <div class="box">
      <div class="inner6">6</div>
    </div>
    <div class="box">
      <div class="inner7">7</div>
    </div>
    <div class="box">
      <div class="inner8">8</div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions