html - CSS Grid 中的网格区域布局不正确

标签 html css css-grid

我想使用 CSS 网格系统制作我的网站,但它似乎无法正常工作。这是我的代码:

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas: "logo faq" "about-us";
}

.logo {
  background-color: blue;
  grid-area: logo;
}

.faq {
  background-color: red;
  grid-area: faq;
}

.aboutUs {
  background-color: cyan;
  grid-area: about-us;
}
<div class="grid">
  <div class="logo">
    LOGO
  </div>
  <div class="faq">
    FAq
  </div>
  <div class="aboutUs">
    About-us
  </div>
</div>

最佳答案

使用 grid-template-areas 属性时,字符串值必须具有相同的列数。

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas: "logo faq" "about-us about-us";
}

.logo {
  background-color: blue;
  grid-area: logo;
}

.faq {
  background-color: red;
  grid-area: faq;
}

.aboutUs {
  background-color: cyan;
  grid-area: about-us;
}
<div class="grid">
  <div class="logo">
    LOGO
  </div>
  <div class="faq">
    FAq
  </div>
  <div class="aboutUs">
    About-us
  </div>
</div>

您可以使用句点或不间断的句点行来表示空单元格 ( spec reference )。

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas: "logo faq" " ... about-us";
}

.logo {
  background-color: blue;
  grid-area: logo;
}

.faq {
  background-color: red;
  grid-area: faq;
}

.aboutUs {
  background-color: cyan;
  grid-area: about-us;
}
<div class="grid">
  <div class="logo">
    LOGO
  </div>
  <div class="faq">
    FAq
  </div>
  <div class="aboutUs">
    About-us
  </div>
</div>

来自网格规范:

7.3. Named Areas: the grid-template-areas property

All strings must have the same number of columns, or else the declaration is invalid.

If a named grid area spans multiple grid cells, but those cells do not form a single filled-in rectangle, the declaration is invalid.

Non-rectangular or disconnected regions may be permitted in a future version of this module.

注意:如规范中所述,除了相等数量的列外,网格区域还必须是矩形 ( see this post for more details )。

关于html - CSS Grid 中的网格区域布局不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45647833/

相关文章:

javascript - JQuery - 右下图像不上升

html - 将 css 网格行限制为特定数量的元素

html - 将网格元素与网格模板列对齐

css - 使用 `display: flex` 时, react 拆分 Pane 显示在其他组件上方

javascript - 倒数计时器加载器动画

javascript - Google Analytics/标签管理器 - 事件跟踪 - 我很困惑

css - 如何将 Uibinder Widgets 的属性引入 GWT 中的 CSS?

css - 如何设置 Bootstrap 文本和卡片大小的大小

html - 为什么我的 HTML Bootstrap pill 没有显示出来?

html - IE8 - 在链接的 div 中单击图像时,链接不起作用