【问题标题】:CSS Grid doesn't work as expected (grid areas)CSS Grid 无法按预期工作(网格区域)
【发布时间】:2017-11-18 13:40:13
【问题描述】:

有人能帮我弄清楚为什么下面的 CSS Grid 示例不能按预期工作吗?这是Codepen的链接:

.cards {
  margin: 0;
  padding: 1em;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 20px;
  grid-template-areas:
    "a a b"
    "c d d"
    "c e e";
}

.card {
  background-color: #1E88E5;
  padding: 2em;
  color: #FFFFFF;
  line-height: 1.4;
  border-radius: 4px;
}

.card1 { grid-area: "a"; }
.card2 { grid-area: "b"; background-color: #4CAF50; }
.card3 { grid-area: "c"; background-color: #FFCA28; }
.card4 { grid-area: "d"; background-color: #F06292; }
.card5 { grid-area: "e"; background-color: #FF8A80; }

由于某些原因,网格区域的结构不正确。可能我忘记了什么,但我无法弄清楚到底是什么。 任何帮助表示赞赏。谢谢!

【问题讨论】:

  • 预期结果是根据“grid-template-areas”参数放置卡片。例如,卡片#1 应该在第一行占据 2 列单元格,卡片 #4 应该包括第 2 行和第 3 行的第一列单元格,等等。
  • 类似这样的东西:prntscr.com/hc34gn

标签: css css-grid


【解决方案1】:

只需从grid-area 中删除引号

.card1 { grid-area: a; }
.card2 { grid-area: b; background-color: #4CAF50; }
.card3 { grid-area: c; background-color: #FFCA28; }
.card4 { grid-area: d; background-color: #F06292; }
.card5 { grid-area: e; background-color: #FF8A80; }

body {
  text-align: center;
}

.wrap {
  max-width: 70em;
  text-align: left;
  margin: 0 auto;
}

.cards {
  margin: 0;
  padding: 1em;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 20px;
  grid-template-areas:
    "a a b"
    "c d d"
    "c e e";
}

.card {
  background-color: #1E88E5;
  padding: 2em;
  color: #FFFFFF;
  line-height: 1.4;
  border-radius: 4px;
  
  h3 {
    margin-top: 0;
  }
}

.card1 { grid-area: a; }
.card2 { grid-area: b; background-color: #4CAF50; }
.card3 { grid-area: c; background-color: #FFCA28; }
.card4 { grid-area: d; background-color: #F06292; }
.card5 { grid-area: e; background-color: #FF8A80; }
<div class="wrap">
  <div class="cards">
    <div class="card card1"><h3>Card 1</h3>Leverage agile frameworks to provide a robust synopsis for high level overviews. Iterative approaches to corporate strategy foster collaborative thinking to further the overall value proposition. Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment.</div>
    <div class="card card2"><h3>Card 2</h3>Bring to the table win-win survival strategies to ensure proactive domination. At the end of the day, going forward, a new normal that has evolved from generation X is on the runway heading towards a streamlined cloud solution.</div>
    <div class="card card3"><h3>Card 3</h3>At the end of the day, going forward, a new normal that has evolved from generation X is on the runway heading towards a streamlined cloud solution. User generated content in real-time will have multiple touchpoints for offshoring. Capitalize on low hanging fruit to identify a ballpark value added activity to beta test.</div>
    <div class="card card4"><h3>Card 4</h3>Override the digital divide with additional clickthroughs from DevOps. Nanotechnology immersion along the information highway will close the loop on focusing solely on the bottom line. At the end of the day, going forward, a new normal that has evolved from generation X is on the runway heading towards a streamlined cloud solution.</div>
    <div class="card card5"><h3>Card 5</h3>Organically grow the holistic world view of disruptive innovation via workplace diversity and empowerment. Bring to the table win-win survival strategies to ensure proactive domination.</div>
  </div>
</div>

【讨论】:

  • 此外,grid-template-columns 变得无用,因为 grid-template-areas 接管并实际执行网格布局描述codepen.io/anon/pen/qVprPO
  • @G-Cyr,实际上,grid-template-columns 仍然很重要并且有所作为。使用1fr 1fr 1fr,列在行上均匀分布空间。没有它,列将解析为auto 宽度,从而呈现不同的布局。
  • @Michael_B 谢谢或指出这一点!是的,我注意到无论有没有grid-template-columns,布局都会发生变化
猜你喜欢
  • 2021-12-28
  • 2020-08-18
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
相关资源
最近更新 更多