You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

网站响应式布局问题:文本未适配导致覆盖图片求助

响应式布局文本覆盖图片问题解决方案

问题根源

  1. 固定高度限制文本容器:.section、.section2等容器内的p标签设置了height:50px,文本超出后会溢出并覆盖图片
  2. 固定边距挤压空间:h1和p的margin-left:100px在小屏幕下占用过多水平空间,导致文本区域不足
  3. 未设置flex换行:各section使用display:flex但未添加flex-wrap:wrap,小屏幕下元素无法自动换行
  4. 图片无响应式处理:仅固定图片height,未限制最大宽度,导致图片在窄屏中超出容器

修复后的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;
}
.overlay{
    width: 100%;
    height: 100%;
    color: white;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    position: relative;
    left: -30%;   
}
.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%;
    padding-top: 20px;
    margin: 0 auto;
    word-wrap: break-word;
}

body{
    margin: 0;
    font-family: sans-serif;
}
.section, .section2, .section3, .section4{
    background-color: #F5F5F5;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 40px;
    width: 80%;
    margin-top: 50px;
    flex-wrap: wrap;
}
.section{
    float: left;
}
.section2{
    float: right;
}
.section3{
    float: left;
}
.section4{
    float: right;
}
.section img, .section2 img, .section3 img, .section4 img{
    max-width: 100%;
    height: auto;
}
.ClassText{
    flex: 1;
    padding-left: 40px;
}
.ClassText h1{
    color: black;
}
.ClassText p{
    width: 100%;
    margin-top: 20px;
    word-wrap: break-word;
}
.section2 sup{
    font-size: 10px;
    opacity: 0.5;
}
.section3 sup{
    font-size: 10px;
    opacity: 0.5;
}

.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;
}

/* 媒体查询适配小屏幕 */
@media (max-width: 768px) {
    .section, .section2, .section3, .section4{
        width: 100%;
        float: none;
        flex-direction: column;
        padding: 20px;
    }
    .ClassText{
        padding-left: 0;
        margin-top: 20px;
    }
    .overlay{
        left: 0;
        text-align: center;
    }
    .overlay p{
        font-size: 24px;
    }
    .main h1{
        font-size: 32px;
    }
    .text{
        width: 90%;
    }
    nav .logo{
        padding-left: 20px;
        font-size: 22px;
    }
    nav ul{
        margin-right: 20px;
    }
    nav ul li a{
        font-size: 16px;
        padding: 8px 10px;
    }
}

关键修复说明

  1. 统一section样式:将重复的section样式合并,减少冗余,添加flex-wrap:wrap实现自动换行
  2. 移除固定高度:删除所有p标签的height:50px,让文本容器自适应内容高度
  3. 优化文本边距:创建.ClassText统一管理文本区域的内边距,替换固定margin-left为padding-left,小屏幕下重置为0
  4. 图片响应式处理:给图片添加max-width:100%和height:auto,确保图片在容器内自适应缩放
  5. 媒体查询适配:针对768px以下屏幕,将section改为垂直布局,调整宽度为100%,移除float,优化字体大小和边距

内容的提问来源于stack exchange,提问作者El33m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 10:27:47