在使用grid-template-rows:/ grid-template-columns声明模板之后,我尝试使用grid-column / grid-row分配在嵌套网格中对齐项目。但是,当我将这些物品放在我想要的地方时,它们最终会出现在意想不到的区域。 $ 50和Give Now项目应该出现在第3行,其中一个出现在第1列,另一个出现在第2列。相反,它们显示在第1和第2行的第2列中。我不知道为什么要这样显示。在这个问题上的任何帮助将不胜感激。
谢谢
.mockup {
display: grid;
max-width: 303px;
position: relative;
margin: auto;
grid-gap: 10px;
grid-template-areas: "infobar infobar" "progbar progbar" "main main" "button2 button3";
grid-template-columns: 50% 50%;
grid-template-rows: 65px 20px 250px 50px 50px;
}
.infobar {
grid-area: infobar;
padding: 5px 18px;
border-radius: 4px;
color: white;
background-color: #424242;
font-family: "rooney-web", "AmericanTypewriter";
white-space: nowrap;
text-align: center;
}
.infobar:before {
content: ' ';
display: block;
position: relative;
bottom: -48px;
width: 14px;
height: 18px;
right: -247px;
background-color: #424242;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
}
.progbar {
grid-area: progbar;
border: 1px solid #777;
margin: none;
background: linear-gradient(to right, #EF5F3C 75%, #eaeaea 25%);
}
.main {
grid-area: main;
border: 1px solid #777;
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: repeat(4, 25%)
}
.main .input p4{
border: 1px solid #777;
border-radius: 4px;
grid-column: 1;
grid-row: 3;
font-size: 20px;
z-index: 1;
}
.main .button1 p3 {
grid-column: 2;
grid-row: 3;
white-space: nowrap;
background-color: Green;
border: 1px solid #777;
border-radius: 4px;
text-align: center;
color: white;
z-index: 1;
}
.main p1 {
grid-column: 1 / 2;
grid-row: 1;
}
.main p2 {
grid-column:1 / 2;
grid-row: 2;
}
.main p5 {
grid-column: 1 / 2;
grid-row: 4;
color: #20A1D4;
}
.button2 {
grid-area: button2;
border: 1px solid #777;
border-radius: 4px;
text-align: center;
background-color: #eaeaea;
}
.button3 {
grid-area: button3;
white-space: nowrap;
border: 1px solid #777;
border-radius: 4px;
text-align: center;
background-color: #eaeaea;
}
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' type='text/css' href='style.css'>
</head>
<body>
<div class="mockup">
<section class="infobar">$167 still needed for this project</section>
<section class="progbar"></section>
<section class="main">
<p1>
<span style="color: #EF5F3C">Only 3 days left</span> to fund this project.
</p1>
<p2>
Join the <strong>42</strong> others who have already supported this project. Every dollar helps.
</p2>
<section class="input">
<p4>
$ <strong>50</strong>
</p4>
</section>
<section class="button1">
<p3>
Give Now
</p3>
</section>
<p5>
Why give $50?
</p5>
</section>
<section class="button2">
<p>
Save for later
</p>
</section>
<section class="button3">
<p>
Tell your friends
</p>
</section>
</div>
</body>
</html>
答案 0 :(得分:1)
您已经混淆了网格计数。您不想计数列,您想要计数行。拿起一张废纸,在垂直线上标记3条(对于两列),然后在水平线上标记3条(对于行)。当您标记p1
,p2
和p5
来指示您的列跨度时,您(很自然地)对第一和第二列说1/2。正确的方法是计算行数。看一下废纸,这些类应该从第一行开始,然后从第三行结束。因此,快速更改CSS,您应该是“黄金”。 (注意:下面的链接以获取更多详细信息)
.main p1 {
grid-column: 1 / 3;
grid-row: 1;
}
.main p2 {
grid-column:1 / 3;
grid-row: 2;
}
.main p5 {
grid-column: 1 / 3;
grid-row: 4;
color: #20A1D4;
}