【问题标题】:CSS Grid - how to have 2, 3, 4, 5, or 6 columnsCSS Grid - 如何拥有 2、3、4、5 或 6 列
【发布时间】:2018-06-30 05:55:12
【问题描述】:

我正在尝试弄清楚如何将 CSS 网格用于一些不对称布局,如下所示:

<h3>Two-Fifths - Three-Fifths</h3>
<div class="cols">
  <div class="two-fifths">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
  <div class="three-fifths">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
</div>
<hr />
<h3>One-Sixth - Five-Sixths</h3>
<div class="cols">
  <div class="one-sixth">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
  <div class="five-sixths">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
</div>

我认为主要问题是在子元素上定义列。我希望有人能帮忙。谢谢。

【问题讨论】:

  • 你的 CSS 在哪里?如果您自己没有尝试过,那么这不是真正要问的地方,那里有很多很好的网格布局文档可以帮助您将它们放在一起,然后如果您有特定问题,请在此处发布人们可以提供帮助:)
  • 你需要什么输出。?

标签: css css-grid


【解决方案1】:

您可以使用称为line-based placement 的网格功能创建布局。

此功能允许您调整网格项目的大小并将其放置在容器中的任何位置。

由于第一个网格有五列,第二个网格有六列,因此您可以为每个网格容器创建两组不同的规则(列数正确)。

或者,您可以通过使用一个公分母来创建一个涵盖两种布局以及可能的其他列数的规则。在这种情况下,一个 30 列的网格适用于这两种情况。

.cols {
  display: grid;
  grid-template-columns: repeat(30, 1fr);
  grid-column-gap: 10px;
}

.two-fifths {
  grid-column: 1 / span 12;   /* 2 x 6 */
}

.three-fifths {
  grid-column: 13 / span 18;  /* 3 x 6 */
}

.one-sixth {
  grid-column: 1 / span 5;    /* 1 x 5 */
  grid-row: 2;
}

.five-sixths {
  grid-column: 6 / span 25;   /* 5 x 5 */
  grid-row: 2;
}
<h3>Two-Fifths - Three-Fifths</h3>
<div class="cols">
  <div class="two-fifths">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
  <div class="three-fifths">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
</div>
<h3>One-Sixth - Five-Sixths</h3>
<div class="cols">
  <div class="one-sixth">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
  <div class="five-sixths">
    <p>This is an example of a WordPress post, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many posts as you like in order to share with your readers what is on your mind.</p>
  </div>
</div>

【讨论】:

  • 感谢您的输入 - 我考虑过使用公分母 - 如果我也想使用四分之一,我猜分母是 60。虽然看你的代码我不明白如下:您提供了一个起始列,然后是跨度,例如: grid-column: 1 / span 12 定义 .two-fifths 类 - 但是,.two-fifths 可以是 .three-fifths 的左侧或右侧- 玩弄这些东西,在我看来 Flexbox 会更容易实现,不是吗??
  • grid-column 中的第一个值是起始行。您可以将其更改为您想要的任何内容。如果需要,请切换起始编号。没问题。或者将它们完全排除在外 (grid-column: span 12),然后自动放置控制。试试看。如果还有其他不清楚的地方,请发布另一个问题。
  • 谢谢 Michael - 我用 grid-template-columns 试过了:repeat(60, 1fr) - 它的网格间隙为 20px;如果我喜欢使用 40px 作为网格间隙,它会破坏布局并且不再工作 - 可能是 Safari 的问题或者我遗漏了什么?
  • 嗯,一个 40px 的网格列间距乘以 59 倍会产生 2360px 的固定宽度。这是容器的最小宽度。它甚至不包括任何可能添加到每一列的内容。这将在许多屏幕上为您提供水平滚动条。
猜你喜欢
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 2018-08-26
相关资源
最近更新 更多