首页 > 解决方案 > 如何使用 CSS 网格正确制作页面布局?

问题描述

下面是我尝试使用网格进行页面布局。但是,滚动条位于标题下方。

在此处输入图像描述

而不是占据页面的整个高度。

在此处输入图像描述

发生这种情况是因为我添加overflow:auto部分标签。由于我对使用网格布局不够熟悉,我不确定如何使用网格正确实现页面布局。有没有解决的办法?有没有更好的解决方案来实现我想要做的事情?

body {
            font-size: 24px;
            height: 100vh;
            margin: 0;
            display: grid;
            grid-template-columns: minmax(150px, 200px) auto;
            grid-template-rows:  54px auto;
            grid-template-areas: "header header"
                                "nav    section";
        }

        header {
            grid-area: header;
            background: red;
        }

        nav {
            display:flex;
            flex-direction: column;
            justify-content: space-between;
            grid-area: nav;
            background: blue;
        }
        section {
            grid-area: section;
            background: yellow;
            overflow:auto;
        }
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
    <meta charset="utf-8">
    <title></title>
<body>
    <header>
        header
    </header>
    <nav>
        <div>
            navigation
            <br> I'm at the top
        </div>
        <div>
            I'm at the bottom
        </div>
    </nav>
    <section>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
    </section>
</body>

</html>

标签: htmlcsscss-grid

解决方案


要显示其他可能性,请查看以下示例:

body {
  font-size: 24px;
  min-height: 100vh;
  margin: 0;
  display: grid;
  grid-template-columns: minmax(150px, 200px) auto;
}

header {
  background: red;
  position: fixed;
  height: var(--header-height);
  width: 100%;
}

nav {
  grid-column: 1;
  display:flex;
  flex-direction: column;
  justify-content: space-between;
  background: blue;
  margin-top: var(--header-height);
}
section {
  background: yellow;
  overflow:auto;
  grid-column: 2;
  margin-top: var(--header-height);
}

:root {
  --header-height: 54px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
    <meta charset="utf-8">
    <title></title>
<body>
    <header>
        header
    </header>
    <nav>
        <div>
            navigation
            <br> I'm at the top
        </div>
        <div>
            I'm at the bottom
        </div>
    </nav>
    <section>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
        section
        <br>
    </section>
</body>

</html>


推荐阅读