提问者:小点点

当网格高度大于页面(设备)高度时,使用css网格容器发出问题


我对css网格有一个问题,我用css网格构建了一个包含两个div容器,我想把容器调整到页面中心。我使用以下代码:

null

html{
    width: 100vw;
    height : 100vh;
}
body{
    height : 100%;
}

.container-fluid{
    width:100%;
    height : 100%;
    display:grid;
    grid-template-columns: 300px;
    grid-template-rows: 200px auto;
    justify-content: center;
    align-content: center;
    border:1px solid red;
}

.logo-container{
    background-color: khaki;
}

.form-container{
    height :540px;
    background-color: lightblue;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/media-query.css">
    <title>Login & Register User With Profile</title>
</head>
<body>
    <div class="container-fluid">
        <div class="logo-container">
               <h1>Logo</h1>
        </div>
        <div class="form-container">
          <h1>Form</h1>
        </div>
    </div>

    
    <link  rel="stylesheet" href="assets/css/all.css">
</body>
</html>

null

如您所见,当网格容器高度大于页面高度时,就会出现问题(请参阅代码结果)。当使用高度为主体标签,网格高度溢出和当删除高度从主体标签,一切都是可以的,但在这个原位容器不能调整在页面中心的容器。什么是问题?


共1个答案

匿名用户

像下面这样简化代码:

null

body {
  margin: 0; /* remove default margin */
}

.container-fluid {
  min-height: 100vh; /* at least screen height */
  display: grid;
  grid-template-columns: 300px;
  grid-template-rows: 200px auto;
  justify-content: center;
  align-content: center;
  border: 1px solid red;
  box-sizing:border-box; /* to consider the border inside the height */
}

.logo-container {
  background-color: khaki;
}

.form-container {
  height: 540px;
  background-color: lightblue;
}
<div class="container-fluid">
  <div class="logo-container">
    <h1>Logo</h1>
  </div>
  <div class="form-container">
    <h1>Form</h1>
  </div>
</div>