在嵌套网格中包含图像

Contain Image in nested grid

在我尝试理解网格的过程中,我未能完全理解如何将较大的图像适合网格的预定大小,这会减小图像的大小。我的图像反而放大了网格 Box1,默认情况下 box2、3、4 和 5 被推得更远。缩小时可以看到。 我已经在像我这样的类似项目上潜伏了 2 天,并使用了他们的解决方案,但是,似乎并没有坚持我的 我读过的几个解决方案:

已编辑,用于语法。

Variable image height in Nested Grid UWP Control size of images in nested grid layouts containing the image inside a css grid

此致

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

问题是 img 是一个实际元素,占用了 space。

此代码段删除了 div 和 img,而是将图像作为带有尺寸封面的背景。这确保 box1 保持网格设置给定的大小。

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

为避免图像调整网格大小(或溢出),您可以将其大小设置为 0 并将最小大小设置为 100%

例子

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

你也可以在旁边看看https://developer.mozilla.org/en-US/docs/Web/CSS/object-positionobject-fit

片段:

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