如何使用网格使图像适合浏览器 window?

How to make an image fit the browser window using grid?

我想将图片放入缩放后仍能正常工作的网站,但我的图片太大了,我似乎放不下。我试图对图片本身的包装 div(此处称为 "innpakning")和周围的 div 施加限制。似乎没有任何效果。即使我能让它工作,那也会破坏我希望它扩展到网站 window 的目的。我如何让它适合 window?

我尝试使用

max-width: 100%;
max-height: 100%;

它修复了图像,但没有修复网站其余部分的间距。我最终在 .overskrift.innhold 之间发现了巨大的差距。

<body>
  <div class="innpakning">

    <div class="overskrift">
      <p id="text"> Hvaler </p>
    </div>

    <div class="innhold">

      <div class="meny">


        <div class="alt"><a href="minnettside.html"> Info</a> </div>
        <div class="alt"><a href="minnettside2.html">Bilde </a> </div>
        <div class="alt"> <a href="minnettside3.html">Lyd </a> </div>
        <div class="alt">  <a href="minnettside4.html">Video</a>  </div>
      </div>

      <div class="faktisk">
<img src="css/media/hval.jpg" alt="">

      </div>
    </div>

  </div>
</body>
innpakning{
  margin: 20px;
  padding-left:  10px;
  padding-right: 10px;
  display: grid;
  grid-template-rows: 1fr 1fr;
}
.overskrift{
  background-color: none;
  color: black;
  font-weight: bold;
}
.innhold {
background-color: #29648A;

}
.meny {
  background-color: none;
  display: grid;
  margin: auto;
  grid-template-columns: 1fr 1fr 1fr 1fr;
}
 a {
  text-decoration: none;
  margin-left: 25px;
  color: white;
}
.alt {
  color: #FAFAFA;
  text-decoration: none;
  background-color: #25274D;
  color: white;
  padding: 10px;
  margin: 2px;
}
.alt:hover {
  background-color: #464866;
}
img {
  margin-left: auto;
  border: solid black;
  max-width: 100%;
  max-height: 100%;
}
.faktisk {
color: #DEDEDE;
padding: 15px;
margin: auto;
display: grid;
grid-template-columns: 1fr
}
@import url('https://fonts.googleapis.com/css?family=Asset');
#text {
    font-family: Asset, cursive;
    text-align: center;
    font-size: 35px;
    color: rgba(245, 246, 255, 0.96);
    background-color: rgba(168, 168, 168, 0.11);
    text-shadow: rgba(0, 0, 0, 0.99) 2px 2px 2px;
}

我建议您使用 CSS 单位类型 vw 来设置图像的最大宽度,如下所示:

img {
  margin-left: auto;
  border: solid black;
  max-height: 100%;
  max-width: 100vw;
  height: auto;
}

在 MDN 上阅读更多内容:https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units#Numbers_lengths_and_percentages