如何解决浏览器缩小时网页文本覆盖其他组件的响应式问题
响应式网站文本覆盖组件问题解决
问题描述
开发健身网站时,缩小浏览器窗口后文本会覆盖其他组件,虽具备基础响应式设置,但仍出现布局错乱。

HTML代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- font awesome cdn link --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"> <link rel="stylesheet" href="https://unpkg.com/swiper@7/swiper-bundle.min.css" /> <link rel="stylesheet" href="jason.css"> </head> <body> <nav> <div class="logo">Revolutionary Fitness</div> <ul> <div class="items"> <li><a href="HomePage.php">Home</a></li> <li><a href="#">Classes</a></li> <li><a href="shop.php">Products</a></li> <li><a href="login.php">Login</a></li> <li><a href="feedback.php">Feedback</a></li> </div> </ul> </nav> <div class="background"> <div class="overlay"> <h3>Classes</h3> <p>Insert Something Here...</p> </div> </div> <div class="main"> <h1>Classes, coaches and community</h1> <div class="main text"> <p>At Virgin Active, we do health and fitness differently. We have expertly crafted exercise experiences that are the perfect blend of intelligent programming and feel-good movement. We've got everything from Yoga to HIIT, so you can move your body any way you want. </p> </div> </div> <section class="no.1" id="no.1"> <div class="section"> <img src="Yoga.jpg" alt="Yoga"> <div class="ClassText"> <h1>Yoga</h1> <p> Choose from Classes with dynamism,energy and athleticism, to an authentic and peaceful experience. <br><br> Classes include: Align,Flow and Calm SkyPark Yoga <br><br> <a href="login.php">Sign Up</a><span> to book this class</span> </p> </div> </div> </section> <footer class="footer"> <div class="social"> <a href="#"><i class="fab fa-facebook-f"></i></a> <a href="#"><i class="fab fa-whatsapp"></i></a> <a href="#"><i class="fab fa-twitter"></i></a> <a href="#"><i class="fab fa-instagram"></i></a> </div> <ul class="list"> <li><a href="about.php">About Us</a></li> <li><a href="contactus.php">Contact Us</a></li> <li><a href="faq.html">FAQs</a></li> </ul> <p class="copyright"> <small>©2022 Revolutionary Fitness</small> </p> </footer> </body> </html>
CSS代码
@import url("https://fonts.googleapis.com/css2?family=Nunito:wght@200;300;400;600&display=swap"); * { font-family: 'Nunito', sans-serif; margin: 0; padding: 0; box-sizing: border-box; } nav { background: #1b1b1b; } nav:after { content: ''; clear: both; display: table; } nav .logo { float: left; color: white; font-size: 27px; line-height: 70px; padding-left: 60px; } nav ul { display: flex; justify-content: center; align-items: center; list-style: none; float: right; margin-right: 40px; } nav ul li { display: inline-block; background: #1b1b1b; margin: 0 5px; } nav ul li a { color: white; text-decoration: none; line-height: 70px; font-size: 18px; padding: 8px 15px; } nav ul li a:hover { color: cyan; } nav ul ul li a:hover { color: cyan; box-shadow: none; } nav ul ul { position: absolute; top: 90px; opacity: 0; visibility: hidden; transition: top .3s; } .background { background-color: #212529; width: 100%; height: 200px; position: relative; /* USE FLEXBOX */ display: flex; align-items: center; justify-content: flex-start; /* ADD SOME PADDING FOR BETTER UI */ padding-inline: 16px; /* LEFT and Right */ } .overlay { height: 100%; color: white; display: flex; justify-content: center; flex-direction: column; align-items: center; position: relative; } .overlay h3 { margin-bottom: 20px; color: crimson; font-size: 20px; } .overlay p { font-size: 35px; } .main h1 { display: flex; text-align: center; justify-content: center; margin-top: 20px; font-size: 50px; } .text { width: 50%; height: 50px; padding-top: 20px; white-space: initial; margin: 0 auto; word-wrap: break-word; } body { margin: 0; font-family: sans-serif; } .section { background-color: #F5F5F5; display: flex; justify-content: space-between; padding: 40px; width: 80%; margin-top: 50px; float: left; } .section img { height: 250px; } .section h1 { margin-left: 100px; color: black; } .section p { margin-left: 100px; width: 55%; height: 50px; white-space: initial; margin-left: 100px; margin-top: 20px; word-wrap: break-word; } .footer { background-color: #000; padding: 40px; clear: both; } .footer .social { text-align: center; padding-bottom: 25px; color: #4b4c4d; } .footer .social a { display: inline-block; height: 40px; width: 40px; background: #424242; margin: 0 10px 10px 0; text-align: center; line-height: 40px; border-radius: 50%; color: #ffffff; transition: all 0.5s ease; } .footer .social a:hover { color: #24262b; background-color: #ffffff; } .footer ul { margin-top: 0; padding: 0; list-style: none; font-size: 18px; line-height: 1.6; margin-bottom: 0; text-align: center; } .footer ul li a { color: #fff; text-decoration: none; } .footer ul li { display: inline-block; padding: 0 15px; } .footer .copyright { margin-top: 15px; text-align: center; font-size: 20px; color: #fff; }
问题解决方法
- 移除固定高度限制:删掉
.text和.section p中的height: 50px,让容器高度随文本内容自动调整,避免文本溢出。 - 优化导航栏适配小屏:添加媒体查询,缩小导航文本和内边距,防止导航项挤在一起:
@media (max-width: 768px) { nav .logo { padding-left: 20px; font-size: 22px; } nav ul { margin-right: 10px; } nav ul li a { font-size: 14px; padding: 8px 10px; } } - 调整课程板块布局:在小屏设备上将
.section的Flex方向改为垂直排列,同时重置图片和文本的边距、宽度:@media (max-width: 992px) { .section { flex-direction: column; width: 100%; padding: 20px; margin: 50px auto; float: none; } .section img { width: 100%; height: auto; margin-bottom: 20px; } .section h1, .section p { margin-left: 0; width: 100%; } } - 缩小大标题字体:添加媒体查询,在小屏上减小
.main h1和.overlay p的字体大小,避免标题溢出:@media (max-width: 768px) { .main h1 { font-size: 30px; } .overlay p { font-size: 24px; } } - 修复浮动布局问题:将
.section的float: left替换为margin: 50px auto; display: block;,避免浮动导致的布局错乱。
内容的提问来源于stack exchange,提问作者El33m
相关产品推荐
相关产品推荐

