CSS网格布局用于1个大图像和3个小图像。

huangapple go评论74阅读模式
英文:

CSS Grid layout for 1 large image and 3 small

问题

我试图重新排列这段代码,以获得像屏幕截图一样的网格视图。

我需要第一张图片占据整个宽度的100%,然后第二行需要3张等宽的图片排列在下方。

DEMO:
http://jsfiddle.net/gycmp21k/

body {
  padding: 40px;
}

* {
  margin: 0px;
  padding: 0px;
}

ul {
  list-style: none;
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 1rem;
}

li:first-child {
  grid-row: span 2;
}

img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<ul>
  <li>
    <img src="https://via.placeholder.com/400" />
  </li>
  <li>
    <img src="https://via.placeholder.com/400" />
  </li>
  <li>
    <img src="https://via.placeholder.com/400" />
  </li>
  <li>
    <img src="https://via.placeholder.com/400" />
  </li>
</ul>

CSS网格布局用于1个大图像和3个小图像。

英文:

I'm trying to rearrange this code to get this grid view like the screenshot.

I need the first image in the row to be full width 100%, then the second row I need 3 images of equal width in a row underneath.

CSS网格布局用于1个大图像和3个小图像。

DEMO:
http://jsfiddle.net/gycmp21k/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  padding: 40px;
}

* {
  margin: 0px;
  padding: 0px;
}

ul {
  list-style: none;
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 1rem;
}

li:first-child {
  grid-row: span 2;
}

img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

<!-- language: lang-html -->

&lt;ul&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

答案1

得分: 2

图片2、3和4在您的图片中具有相同的宽度,因此使它们具有相同的宽度。

grid-template-columns: 1fr 1fr 1fr;

图像1和图像2、3和4在您的图片中高度不相同,因此不要使它们具有相同的高度。

grid-template-rows: auto auto;

在您的图片中,图像1跨越多个而不是多个行,并且它跨越了3列而不是2列。

grid-column: span 3;

英文:

Images 2, 3 and 4 are the same width in your picture, so make them the same width.

grid-template-columns: 1fr 1fr 1fr;

Image 1 and images 2, 3 and 4 are not the same hight in your picture, so don't make them the same height:

grid-template-rows: auto auto;

Image 1 spans across multiple columns not multiple rows in your picture, and it spans across 3 of them, not 2 of them

grid-column: span 3;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  padding: 40px;
}

* {
  margin: 0px;
  padding: 0px;
}

ul {
  list-style: none;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto auto;
  gap: 1rem;
}

li:first-child {
  grid-column: span 3;
}

img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

<!-- language: lang-html -->

&lt;ul&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;img src=&quot;https://via.placeholder.com/400&quot; /&gt;
  &lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月26日 20:36:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定