1 需求
CSS让height100%生效的解决方案
方案一:所有父节点高度都需要设置为100%
方案二:删除
注意事项:切记,一定是所有父节点都要设置为100%
2 语法
3.1 示例:body的height为100%
错误实现:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title></title>
- <style>
- body {
- border: 1px black solid;
- height: 100%;
- }
- </style>
- </head>
- <body>
- 123
- </body>
- </html>
正确实现1:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title></title>
- <style>
- html {
- height: 100%;
- }
-
- body {
- border: 1px black solid;
- height: 100%;
- }
- </style>
- </head>
- <body>
- 123
- </body>
- </html>
正确实现2:
- <html>
- <head>
- <meta charset="UTF-8" />
- <title></title>
- <style>
- body {
- border: 1px black solid;
- height: 100%;
- }
- </style>
- </head>
- <body>
- 123
- </body>
- </html>
正确实现3:
- <!DOCTYPE>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title></title>
- <style>
- body {
- border: 1px black solid;
- height: 100%;
- }
- </style>
- </head>
- <body>
- 123
- </body>
- </html>
3.2 示例:
错误实现
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title></title>
- <style>
- /* html {
- height: 100%;
- } */
-
- body {
- /* border: 1px black solid; */
- height: 100%;
- }
-
- #container {
- /* border: 1px black solid; */
- text-align: center;
- /* height: 100%; */
- }
-
- #header {
- /* border: 1px black solid; */
- background-color: lightblue;
- height: 5%;
- }
-
- #menu {
- /* border: 1px black solid; */
- background-color: aliceblue;
- float: left;
- height: 90%;
- width: 15%;
- }
-
- #content {
- /* border: 1px black solid; */
- float: left;
- height: 90%;
- width: 85%;
-
- }
-
- #footer {
- /* border: 1px black solid; */
- background-color: lightblue;
- clear: both;
- height: 90%;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <div id="header">header</div>
- <div id="menu">menu</div>
- <div id="content">content</div>
- <div id="footer">footer</div>
- </div>
- </body>
- </html>
正确实现
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" />
- <title></title>
- <style>
- html {
- height: 100%;
- }
-
- body {
- /* border: 1px black solid; */
- height: 100%;
- }
-
- #container {
- /* border: 1px black solid; */
- text-align: center;
- height: 100%;
- }
-
- #header {
- /* border: 1px black solid; */
- background-color: lightblue;
- height: 5%;
- }
-
- #menu {
- /* border: 1px black solid; */
- background-color: aliceblue;
- float: left;
- height: 90%;
- width: 15%;
- }
-
- #content {
- /* border: 1px black solid; */
- float: left;
- height: 90%;
- width: 85%;
-
- }
-
- #footer {
- /* border: 1px black solid; */
- background-color: lightblue;
- clear: both;
- height: 90%;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <div id="header">header</div>
- <div id="menu">menu</div>
- <div id="content">content</div>
- <div id="footer">footer</div>
- </div>
- </body>
- </html>
4 参考资料
css如何让height:100%起作用?_shanql的博客-CSDN博客