我是CSS网格的新手,这是我的第一次尝试。
在下面的代码中,有一个带有grid-wrapper类的div容器。它包含两个子div,类名为grid-item。在CSS中,它们被引用为grid-item:nth-child(1)和grid-item:nth-child(2)。在网格包装器中,网格模板区域正确放置网格项:nth-child(1)和grid-item:nth-child(2)。
网格项目:nth-child(2)拥有自己的孩子,但我无法正确排列。
我有一个FSFiddle项目。
有人可以开导我吗?提前谢谢。
body {
font-family: "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif;
}
.grid-wrapper {
display: grid;
grid-template-columns: repeat(8, {1fr});
grid-template-areas:
". header header header header header header ."
". tabs tabs tabs tabs tabs tabs .";
}
.grid-item:nth-child(1) {
grid-area: header;
background-color: #003566;
padding-top: 30px;
padding-bottom: 30px;
text-align: center;
border-bottom: 2px solid #ff6a00;
}
.grid-item:nth-child(2) {
grid-area: tabs;
background-color: #b3cccc;
padding-top: 10px;
padding-bottom: 10px;
/*grid-template-areas: "tabs tabs";
grid-template-columns: 1fr 1fr;*/
}
.logo {
grid-area: logo;
font-size: 1.3em;
color:#fff;
}
.logo span {
font-weight: 100;
color: #ff6a00;
}
.logo i {
font-size: 5px;
vertical-align: middle;
color: #fff;
}
.grid-item:nth-child(2)>div {
/*grid-area: tabs;*/
color: #003566;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SurEyeDentity Host Services Manager</title>
</head>
<body>
<div class="grid-wrapper">
<div class="grid-item">
<div class="logo">
SurEyeDentity<span> Host<i class="fa fa-circle" aria-hidden="true"></i>Services<i class="fa fa-circle" aria-hidden="true"></i>Manager</span>
</div>
</div>
<div class="grid-item">
<div>
<i class="fa fa-camera"></i> Cameras
</div>
<div>
<i class="fa fa-user" aria-hidden="true"></i> Users
</div>
</div>
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
在内部网格项中使用display:grid
...并使用grid-auto-flow: column
按列对齐...使用justify-content: center
将项目水平对齐..
你也使用了grid-template-columns
的错误价值..因为我看到你不需要这个,所以删除它
Stack Snippet
body {
font-family: "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif;
}
.grid-wrapper {
display: grid;
grid-template-columns: repeat(8, {
1fr
}
);
grid-template-areas : ". header header header header header header ." ". tabs tabs tabs tabs tabs tabs .";
}
.grid-item:nth-child(1) {
grid-area: header;
background-color: #003566;
padding-top: 30px;
padding-bottom: 30px;
text-align: center;
border-bottom: 2px solid #ff6a00;
}
.grid-item:nth-child(2) {
grid-area: tabs;
background-color: #b3cccc;
padding-top: 10px;
padding-bottom: 10px;
display: grid;
justify-content: center;
grid-auto-flow: column;
}
.logo {
grid-area: logo;
font-size: 1.3em;
color: #fff;
}
.logo span {
font-weight: 100;
color: #ff6a00;
}
.logo i {
font-size: 5px;
vertical-align: middle;
color: #fff;
}
.grid-item:nth-child(2)>div {
color: #003566;
margin: 0 10px;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SurEyeDentity Host Services Manager</title>
</head>
<body>
<div class="grid-wrapper">
<div class="grid-item">
<div class="logo">
SurEyeDentity<span> Host<i class="fa fa-circle" aria-hidden="true"></i>Services<i class="fa fa-circle" aria-hidden="true"></i>Manager</span>
</div>
</div>
<div class="grid-item">
<div>
<i class="fa fa-camera"></i> Cameras
</div>
<div>
<i class="fa fa-user" aria-hidden="true"></i> Users
</div>
</div>
</div>
</body>
</html>
&#13;
答案 1 :(得分:0)
将.grid-item:nth-child(2)&gt; div更改为.grid-item:nth-child(2)div
body {
font-family: "Segoe UI", Frutiger, "Frutiger Linotype", "Dejavu Sans", "Helvetica Neue", Arial, sans-serif;
}
.grid-wrapper {
display: grid;
grid-template-columns: repeat(8, {1fr});
grid-template-areas:
". header header header header header header ."
". tabs tabs tabs tabs tabs tabs .";
}
.grid-item:nth-child(1) {
grid-area: header;
background-color: #003566;
padding-top: 30px;
padding-bottom: 30px;
text-align: center;
border-bottom: 2px solid #ff6a00;
}
.grid-item:nth-child(2) {
grid-area: tabs;
background-color: #b3cccc;
padding-top: 10px;
padding-bottom: 10px;
/*grid-template-areas: "tabs tabs";
grid-template-columns: 1fr 1fr;*/
}
.logo {
grid-area: logo;
font-size: 1.3em;
color:#fff;
}
.logo span {
font-weight: 100;
color: #ff6a00;
}
.logo i {
font-size: 5px;
vertical-align: middle;
color: #fff;
}
.grid-item:nth-child(2) div {
/*grid-area: tabs;*/
color: #003566;
margin: 10px;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SurEyeDentity Host Services Manager</title>
</head>
<body>
<div class="grid-wrapper">
<div class="grid-item">
<div class="logo">
SurEyeDentity<span> Host<i class="fa fa-circle" aria-hidden="true"></i>Services<i class="fa fa-circle" aria-hidden="true"></i>Manager</span>
</div>
</div>
<div class="grid-item">
<div>
<i class="fa fa-camera"></i> Cameras
</div>
<div>
<i class="fa fa-user" aria-hidden="true"></i> Users
</div>
</div>
</div>
</body>
</html>
&#13;