Home > Back-end >  Contain Image in nested grid
Contain Image in nested grid

Time:10-18

In my attempts on understanding grids, I’ve failed to fully comprehend how to fit a larger image to the predetermined size of the grid, which would reduce size of the image. My image enlarges the grid Box1 instead and, by default box2,3,4, and 5 are pushed further out. Which can be seen when zoomed out. I've been lurking for 2 days now on similar project like mine and used their solutions, however, does not seem to stick to mine A couple of solutions I’ve read:

Edited, for grammar.

Variable image height in Nested Grid UWP Resize image in grid to fit div to smaller size Controlling the size of an image within a CSS Grid layout Control size of images in nested grid layouts containing the image inside a css grid

Best Regards

.grid {
  display: grid;
  grid-template-columns: 1fr 4fr 1fr;
  background-color: #8b9dc3;
  height: 100vh;
}
.box1 {
  background-color: #3b5998;

  grid-column: 1/4;
  grid-row: 1/2;
  z-index: 2;
  padding: 1em;
}
.box2 {
  grid-column: 1;
  grid-row-start: 2;
  grid-row-end: 12;
  color: white;
}
.box3 {
  background-color: #ffffff;
  grid-row: 2/12;
  grid-column: auto;
  color: black;
}
.box4 {
  align-self: stretch;
  grid-column: 3;
  grid-row: 2/12;
  color: white;
}
.box5 {
  background-color: #3b5998;
  grid-column: 1/4;
}
#headerImage {
  height: 100%;
  object-fit: cover;
  max-height: 100%;
  contain: content;
}
.nested {
  display: grid;
  grid-template-columns: repeat(1fr);
  grid-auto-rows: 100%;
  grid-gap: 1px;
}
.nested > div {
  border: #333 1px solid;
  padding: 1em;
}
html,
body {
  font-size: 14px;
  font-family: "Times New Roman", Times, serif;

  margin: 0;
  padding: 0;
  overflow: hidden;
}
.h1 {
  color: black;
  font: bold;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="mystyle3.css">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="grid">
        <div class="box box1">box1
            <div id="headerImage"><img src="https://wallpaperaccess.com/full/1713248.jpg" alt="a picture">BOX 1 </div>
        </div>
        <div class="box box2">
            <div class="nested">
            </div>

        </div>
        <div class="box box3">box3</div>
        <div class="box box4">box4</div>
        <div class="box box5">box5</div>

    </div>
</body>


</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

The problem is that the img is an actual element taking up space.

This snippet removes that div and img and instead puts the image as a background with size cover. This ensures that box1 remains the size given by the grid settings.

.grid {
  display: grid;
  grid-template-columns: 1fr 4fr 1fr;
  background-color: #8b9dc3;
  height: 100vh;
}
.box1 {
  background-color: #3b5998;

  grid-column: 1/4;
  grid-row: 1/2;
  z-index: 2;
  padding: 1em;
  overflow: hidden;
}
.box2 {
  grid-column: 1;
  grid-row-start: 2;
  grid-row-end: 12;
  color: white;
  background-color: magenta;
}
.box3 {
  background-color: #ffffff;
  grid-row: 2/12;
  grid-column: auto;
  color: black;
}
.box4 {
  align-self: stretch;
  grid-column: 3;
  grid-row: 2/12;
  color: white;
}
.box5 {
  background-color: #3b5998;
  grid-column: 1/4;
}
.nested {
  display: grid;
  grid-template-columns: repeat(1fr);
  grid-auto-rows: 100%;
  grid-gap: 1px;
}
.nested > div {
  border: #333 1px solid;
  padding: 1em;
}
html,
body {
  font-size: 14px;
  font-family: "Times New Roman", Times, serif;

  margin: 0;
  padding: 0;
  overflow: hidden;
}
.h1 {
  color: black;
  font: bold;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="mystyle3.css">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="grid">
        <div class="box box1" style="background-image: url(https://wallpaperaccess.com/full/1713248.jpg); background-size: cover;">box1
        </div>
        <div class="box box2">
            <div class="nested">
            </div>

        </div>
        <div class="box box3">box3</div>
        <div class="box box4">box4</div>
        <div class="box box5">box5</div>

    </div>
</body>


</html>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

To avoid the image to resize the grid (or overflow it) you can set its size to 0 and min-size to 100%

example

img {
  height:0;
  width:0;
  min-height:100%;
  min-width:100%;
  object-fit:cover
}

You may also look at https://developer.mozilla.org/en-US/docs/Web/CSS/object-position aside object-fit

snippet:

.grid {
  display: grid;
  grid-template-columns: 1fr 4fr 1fr;
  background-color: #8b9dc3;
  height: 100vh;
}

.box1 {
  background-color: #3b5998;
  grid-column: 1/4;
  grid-row: 1/2;
  z-index: 2;
  padding: 1em;
}

.box2 {
  grid-column: 1;
  grid-row-start: 2;
  grid-row-end: 12;
  color: white;
}

.box3 {
  background-color: #ffffff;
  grid-row: 2/12;
  grid-column: auto;
  color: black;
}

.box4 {
  align-self: stretch;
  grid-column: 3;
  grid-row: 2/12;
  color: white;
}

.box5 {
  background-color: #3b5998;
  grid-column: 1/4;
}

#headerImage {
  height: 100%;
  object-fit: cover;
  max-height: 100%;
  contain: content;
}

.nested {
  display: grid;
  grid-template-columns: repeat(1fr);
  grid-auto-rows: 100%;
  grid-gap: 1px;
}

.nested>div {
  border: #333 1px solid;
  padding: 1em;
}

html,
body {
  font-size: 14px;
  font-family: "Times New Roman", Times, serif;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.h1 {
  color: black;
  font: bold;
}

#headerImage img {
  height: 0;
  width: 0;
  min-height: 100%;
  min-width: 100%;
  object-fit: cover
}
<div class="grid">
  <div class="box box1">box1
    <div id="headerImage"><img src="https://wallpaperaccess.com/full/1713248.jpg" alt="a picture">BOX 1 </div>
  </div>
  <div class="box box2">
    <div class="nested"> </div>
  </div>
  <div class="box box3">box3</div>
  <div class="box box4">box4</div>
  <div class="box box5">box5</div>

</div>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related