2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
How to keep the footer of the page at the bottom of the page when the middle content is not high enough to be stretched by the screen
- <body>
- <header>头部</header>
- <main>主区域</main>
- <footer>底部</footer>
- </body>
- <style>
- html, body {
- margin: 0;
- padding: 0;
- width: 100%;
- height: 100%;
- }
- body {
- min-height: 100vh;
- display: grid;
- grid-template-rows: auto 1fr auto;
- }
-
- header {
- background: aquamarine;
- height: 40px;
- line-height: 40px;
- text-align: center;
- }
-
- footer {
- background: aquamarine;
- height: 50px;
- line-height: 50px;
- text-align: center;
- }
-
- main {
- margin: 0 auto;
- background: aqua;
- width: 80%;
- }
- </style>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>gridbox布局</title>
- <style>
- html, body {
- margin: 0;
- padding: 0;
- width: 100%;
- height: 100%;
- }
- body {
- min-height: 100vh;
- display: grid;
- grid-template-rows: auto 1fr auto;
- }
-
- header {
- background: aquamarine;
- height: 40px;
- line-height: 40px;
- text-align: center;
- }
-
- footer {
- background: aquamarine;
- height: 50px;
- line-height: 50px;
- text-align: center;
- }
-
- main {
- margin: 0 auto;
- background: aqua;
- width: 80%;
- }
- </style>
- </head>
- <body>
- <header>头部</header>
- <main>主区域</main>
- <footer>底部</footer>
- </body>
- </html>
1. Retained
min-height: 100vh
This method, but then we usegrid-template-rows
To space out.2. The trick of this method is to use special grid cells
fr
。fr
Indicates "number of copies", using it requires the browser to calculate the available "number of copies" of the remaining space to be allocated to this column or row. In this case, it fills all the available space between the header and footer, which also solves the "defect" of the flexbox method, where the main area cannot be automatically filled.