使用HTML/CSS/Bootstrap 4时页面底部滚动条问题求助
页面底部横向滚动条问题修复
问题情况
用HTML、CSS和Bootstrap 4开发页面时,底部出现了不必要的横向滚动条。尝试删除jumbotron--play模块后问题仍存在,最终定位到带有justify-content-md-center类的row元素是问题根源,但不清楚修复方法。
代码重现
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <link rel="stylesheet" href="bootstrap.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha512-z3gLpd7yknf1YoNbCzqRKc4qyor8gaKU1qmn+CShxbuBusANI9QpRohGBreCFkKxLhei6S9CQXFEbbKuqLg0DA==" crossorigin="anonymous" referrerpolicy="no-referrer" /> <title>About Us</title> </head> <div class="jumbotron jumbotron--aboutus"> <h1 class="display-4">ABOUT US</h1> </div> <div class="container"> <div class="row"> <div class="col col-lg-8"> <a href="#"> <img src="presidents-day-furniture-amazon-tout-b05768db6945443b9a2190e5b099a777.webp" class="img-fluid zoom-image" alt="Image 1"> </a> </div> <div class="col col-lg"> <h2 class="display-6">Modular Sofa, Light Gray Linen</h2> <p>Modern sectional sofa with an inverted L-shaped seat that can be placed on either side,…</p> <button>Shop Now</button> </div> </div> <div class="row"> <div class="col col-lg"> <h2 class="display-6">Bedside Cabinet, Oak Material</h2> <p>Store gadgets, books, or magazines on this easy-sliding two-tier nightstand or end table.</p> <button>Shop Now</button> </div> <div class="col col-lg-7"> <a href="#"> <img src="tabdaugiuong-2.jpg" class="img-fluid zoom-image" alt="Image 2"> </a> </div> </div> </div> <div class="jumbotron jumbotron--play mx-auto"> <button> Play</button> </div> <div class="row justify-content-md-center"> <div class="col-md-auto"> <a href=""> <img src="brand.jpg" alt=""> </a> </div> <div class="col-md-auto"> <a href=""> <img src="brand-2.jpg" alt=""> </a> </div> <div class="col-md-auto"> <a href=""> <img src="brand-3.jpg" alt=""> </a> </div> <div class="col-md-auto"> <a href=""> <img src="brand-5.jpg" alt=""> </a> </div> <div class="col-md-auto"> <a href=""> <img src="brand-4.jpg" alt=""> </a> </div> </div> <div class="jumbotron jumbotron--email"> <h1 class="display-4">Our Newsletter</h1> <p class="lead">Enjoy our newsletter to stay updated with the latest news!</p> <form action="" class="email"> <div class="input-container"> <input aria-required="true" aria-invalid="false" placeholder="Your email..." value="" type="email" name="your-email" required> <button type="submit" class="button-arrow"> <i class="fa-solid fa-arrow-right"></i> </button> </div> </form> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html>
CSS代码
// Extra small devices (portrait phones, less than 576px) // No media query since this is the default in Bootstrap // Small devices (landscape phones, 576px and up) @media (min-width: 576px) { ... } // Medium devices (tablets, 768px and up) @media (min-width: 768px) { ... } // Large devices (desktops, 992px and up) @media (min-width: 992px) { ... } // Extra large devices (large desktops, 1200px and up) @media (min-width: 1200px) { ... } .jumbotron--aboutus { background-image: url(ContraMedia-Free-Zoom-Bg-49.jpg); background-size: cover; background-position: left 40% bottom 18%; height: 50vh; filter: brightness(90%); margin-bottom: 100px; } .jumbotron--play { background-image: url(bed123.jpg); background-size: cover; background-position: center; height: 50vh; width: 75%; margin-top: 100px; margin-left: 12%; } .jumbotron h1 { text-align: center; color: white; margin-top: 130px; filter: brightness(200%); font-size: 40px; font-weight: bold; } .row .col { left: -10%; margin: 10px; margin-bottom: 50px; } .row .col-lg { justify-content: center; top: 100px; left: 20px; } .row .col-lg h2 { font-size: 40px; margin-bottom: 20px; } .row .col-lg-7 { left: 160px; } .zoom-image { transition: transform 0.3s ease; } .zoom-image:hover { transform: scale(1.05); } .container h2 { font-size: 27px; } .container p { margin-bottom: 30px; font-size: 14px; color: grey; } .container button { background-color: white; border: 0.1px black solid; width: 140px; height: 45px; transition: background-color 0.5s; } .container button:hover { background-color: #ED5D43; cursor: pointer; color: white; border: none; } .jumbotron button { margin-left: 48%; margin-top: 10%; border-radius: 50%; background: none; border: 1px white solid; color: white; font-size: 16px; padding: 30px; transition: transform 0.3s ease; outline: none; } .jumbotron button:hover { background-color: none; cursor: pointer; transform: scale(1.05); outline: none; } .row .col-md-auto { margin: 65px; transition: transform 0.3s ease; opacity: 0.5; } .row .col-md-auto:hover { transform: scale(1.1); cursor: pointer; opacity: 1; } .jumbotron--email { background-image: url(back-2.jpg); background-size: cover; background-position: center center; padding: 65px 0 70px 0px; height: 300px; margin-top: 50px; display: flex; flex-direction: column; align-items: center; text-align: center; position: relative; } .jumbotron--email h1 { text-align: center; color: black; margin-top: -1%; margin-bottom: 20px; } .jumbotron--email p { text-align: center; justify-content: center; color: grey; font-size: 15px; } .jumbotron--email .email input[type=email] { width: 235%; border: none; border-bottom: 1.5px solid #e5e5e5; padding: 5px 6px; height: 40px; background: transparent; border-radius: 0; outline: none; margin-left: -70%; } .jumbotron--email .button-arrow { color: black; border: none; margin-left: 100%; border-radius: 0; font-size: 20px; z-index: 1; position: absolute; top: -140%; transition: transform 0.3s ease; outline: none; } .jumbotron--email .button-arrow:hover { outline: none; transform: scale(1.1); } .jumbotron--email .email { position: relative; width: 17%; }
问题原因
- row脱离容器:Bootstrap的row默认自带左右负margin,用来抵消container的padding。出问题的row没有放在container里,负margin会让row超出视口宽度,直接触发横向滚动条。
- col的margin过大:自定义CSS里给
.row .col-md-auto设置了margin:65px,小屏幕下多个col元素的总宽度加margin会超过视口,进一步放大滚动条问题。
修复方法
方法1(最推荐):给row套container
把带有justify-content-md-center的row放进Bootstrap的container中,抵消row的负margin:
<div class="container"> <div class="row justify-content-md-center"> <!-- 所有col-md-auto元素 --> </div> </div>
方法2:调整小屏幕下的col margin
如果不想修改HTML结构,可以针对小屏幕设备减小col的margin,避免宽度溢出:
@media (max-width:767px) { .row .col-md-auto { margin:15px; /* 根据布局需求调整,保证总宽度不超过视口 */ } }
方法3:全局修正row的margin(不推荐)
如果不想逐个处理row,可全局去掉row的负margin,但可能影响其他布局,需谨慎使用:
.row { margin-left:0; margin-right:0; }
内容的提问来源于stack exchange,提问作者C3ddyyy
相关产品推荐
相关产品推荐

