将body和html得高度设置为100%,再设置div高度为100%
<!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"> <title>document</title> </head> <body> <div class="box"></div> </body> </html> <style> body, html { height: 100% } .box { width: 100%; height: 100%; background-color: red; } </style>
方法二:
将要铺满全屏的元素脱离文档流
<!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"> <title>document</title> </head> <body> <div class="box"></div> </body> <style> .box { width: 100%; height: 100%; position: fixed; background-color: red; } </style>
方法三:
使用vh,vh相对视口高度
<!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"> <title>document</title> </head> <body> <div class="box"></div> </body> <sytle> .box { width: 100vw; height: 100vh; background-color: red } </style>