响应式图像网格:缩放行高而不是放大图像

问题描述 投票:0回答:1

在响应式网格上工作。问题是在调整大小时,图像会“放大/缩小”一点,而网格行的高度保持不变。

代码必须如何更改,以便在调整图像大小时不放大/缩小,而是更改网格行高,以便图像可以以其原始纵横比显示?

.container {
  padding: 40px 5%;
}

ul {
  list-style: none;
}

.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.image-gallery>li {
  flex: 1 1 auto;
  height: 300px;
  cursor: pointer;
  position: relative;
}

.image-gallery li img {
  object-fit: cover;
  width: 100%;
  height: 100%;
  vertical-align: middle;
  border-radius: 5px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Static Template</title>
</head>

<body>
  <div class="container">
    <ul class="image-gallery">
      <li>
        <img src="https://source.unsplash.com/VWcPlbHglYc" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/e6FMMambeO4" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/klCiPmzUw0Y" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/IdNOTjPeHrE" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/O0N9MF--hK4" alt="" />
      </li>
    </ul>
  </div>
</body>

</html>

html css gridview layout responsive
1个回答
0
投票

.container {
  padding: 40px 5%;
}

ul {
  list-style: none;
}

.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.image-gallery>li {
  flex: 1 1 auto;
  height: 100%;
  cursor: pointer;
  position: relative;
}

.image-gallery li img {
  width: 100%;
  height: 300px;
  vertical-align: middle;
  border-radius: 5px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Static Template</title>
</head>

<body>
  <div class="container">
    <ul class="image-gallery">
      <li>
        <img src="https://source.unsplash.com/VWcPlbHglYc" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/e6FMMambeO4" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/klCiPmzUw0Y" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/IdNOTjPeHrE" alt="" />
      </li>
      <li>
        <img src="https://source.unsplash.com/O0N9MF--hK4" alt="" />
      </li>

    </ul>
  </div>
</body>

</html>

更改 Css 一些问题,但这段代码是正确的工作....

© www.soinside.com 2019 - 2024. All rights reserved.