2017-11-18 203 views
0

有人能帮我弄清楚为什么下面的CSS网格示例不能按预期工作?这里有一个链接到Codepen:CSS网格不能按预期工作

https://codepen.io/Nekto/pen/yPpaBE

.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; } 

出于某种原因,电网区域不正确的结构。可能我忘了一些东西,但我无法弄清楚究竟是什么。 任何帮助表示赞赏。谢谢!

+0

预期的结果是有放置在按照“并网卡模板区域“参数。例如,卡#1应该在第一行占据2个列单元,卡#4应该包括第2行和第3行的第一列单元等。 – Nekto

+0

类似这样的:http://prntscr.com/hc34gn – Nekto

回答

3

刚从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>

+2

另外, grid-template-columns变得没用,因为grid-template-areas接管并且实际上做了网格布局描述https://codepen.io/anon/pen/qVprPO –

+2

@G-Cyr,实际上是'grid-template-columns'仍然重要并且有所作为。使用'1fr 1fr 1fr'这些列均匀地分配行中的空间。没有它,这些列将解析为“自动”宽度,这会呈现不同的布局。 –

+1

@Michael_B你是对的! :) –