1. 设置viewport-fit=cover
  1. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0,viewport-fit=cover">
  1. 设置安全区域
  1. .u-safeAreaInset-top {
  2. height: constant(safe-area-inset-top) !important;
  3. height: env(safe-area-inset-top) !important;
  4. }
  5. .u-safeAreaInset-bottom {
  6. height: constant(safe-area-inset-bottom) !important;
  7. height: env(safe-area-inset-bottom) !important;
  8. }
  1. 实例如下
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0,viewport-fit=cover">
  6. <title>Document</title>
  7. <style>
  8. .u-safeAreaInset-top {
  9. height: constant(safe-area-inset-top) !important;
  10. height: env(safe-area-inset-top) !important;
  11. }
  12. .u-safeAreaInset-bottom {
  13. height: constant(safe-area-inset-bottom) !important;
  14. height: env(safe-area-inset-bottom) !important;
  15. }
  16. html, body {
  17. padding: 0;
  18. margin: 0;
  19. width: 100%;
  20. height: 100%;
  21. overflow: hidden;
  22. }
  23. .container {
  24. width: 100vw;
  25. height: 100vh;
  26. overflow: hidden;
  27. box-sizing: border-box;
  28. padding-bottom: 12vw;
  29. background-color: bisque;
  30. position: relative;
  31. }
  32. .body {
  33. width: 100%;
  34. height: 100%;
  35. overflow-y: auto;
  36. }
  37. .content {
  38. height: 3000px;
  39. background-color: chocolate;
  40. }
  41. .footer {
  42. position: fixed;
  43. width: 100%;
  44. height: 12vw;
  45. left: 0;
  46. bottom: 0;
  47. background-color: #369;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="container">
  53. <div class="body">
  54. <div class="content"></div>
  55. </div>
  56. <div class="footer">
  57. </div>
  58. </div>
  59. </body>
  60. </html>

如需局部使用,如下

  1. .safeAreaTop {
  2. padding-top: constant(safe-area-inset-top);
  3. padding-top: env(safe-area-inset-top);
  4. }
  5. .safeAreaBottom {
  6. padding-bottom: constant(safe-area-inset-bottom);
  7. padding-bottom: env(safe-area-inset-bottom);
  8. }