CSS网格无法填充整个视口

时间:2018-08-13 23:25:50

标签: css css-grid

我不熟悉CSS网格。我用网格布局和三个div布局了一个简单的页面。我将网格(请参阅css代码中的“ .grid”类)设置为100vh和100vw,但是当我在Firefox(版本56)中查看此网格时,它在左右两侧都放置了垂直和水平滚动条,并且网格实际上,它不会填满整个视图屏幕。

这是html代码:

<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Welcome to Project</title>
<link href="css/main.css" rel="stylesheet" type="text/css">
</head>

<body class="site">

<div class="grid">

<!-- ______________ -->

<div class="a">

<div class="a_left">

<div>Logo for Project</div>

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

</div>
</div>

<!-- ______________ -->

<div class="b">This is grid-template-row b</div>

<div class="c">This is grid-template-row c</div>

</div>

<!-- ______________ -->

</body>
</html> 

这是CSS代码:

.grid {
display: grid;
grid-template-rows: 10% 35% 55%;
width: 100vw;
height: 100vh;
}

.grid > * {
background-color: darkgray;
color: white;
padding: 2em;
}

.a{
    display: grid;
    font-family: sans-serif;
    color: green;
    font-size: 16pt;
}

.a_left{
    display: flex;
    text-align: left;
    vertical-align: left;
    flex-wrap: nowrap;
    justify-content: space-between;
}

.a_right{
    display: flex;
    height: 100%;
    flex-wrap: nowrap;
    justify-content: right;
    vertical-align: right;
}

.b{
    display: grid;
    font-family: sans-serif;
    color: blue;
    font-size: 16pt;
}

.c{
    display: grid;
    font-family: sans-serif;
    color: black;
    font-size: 16pt;
}

li {
    display: inline;
}

site-nav{
    margin-top: 0px;
}

.topnav {
    align-content: right;
    justify-content: center;
    overflow: hidden;
    background-color: #333;
    height: 100%
}

.topnav a {
    float: left;
    color: #f2f2f2;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}   

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #4CAF50;
  color: white;
}

.site{
    max-width: none;
    display: grid;
}

我还需要做些什么来消除滚动条并使网格充满整个视口?

感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

根据上面jhpratt的评论,这是解决问题的方法。将body CSS代码更改为此:

body{
    background-color: black;
    margin-top: 0;
    margin-bottom: 0;
    margin-left: 0;
    margin-right: 0;
}

在车身上增加边距可以解决问题。

相关问题