如何让卡片始终处于背景图片范围内?响应式布局求助
问题:小屏幕下技能卡片溢出便签板背景图
需求与问题
- 需求:无论屏幕尺寸如何,技能卡片始终位于便签板样式的背景图片内部,呈现复古风格
- 问题:当前实现下,小屏幕中卡片会从背景图上下溢出

相关代码
{% block content %} <style> .custom-skills-section { background: url('{{ background_image }}') center center / contain no-repeat; border-radius: 10px; padding: 20px; margin: 0 auto; width: 100%; max-width: 1600px; height: 1200px; } .custom-skill-card-shadow { height: 150px; display: flex; align-items: center; justify-content: center; } .custom-skill-card-content { border-radius: 8px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); background-color: #f0f4f8; display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; } .custom-skill-title { font-size: 1em; font-weight: 600; color: #333; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: 100%; } </style> <div class="d-flex justify-content-center align-items-center" style="min-height: 100vh;"> <div class="custom-skills-section"> <h2 class="text-center text-white py-4">My Skills</h2> <div class="container"> <div class="row"> {% for skill in skills %} <div class="col-lg-4 col-md-6 col-12 mb-4 custom-skill-card"> <div class="custom-skill-card-shadow"> <div class="custom-skill-card-content"> <h4 class="custom-skill-title">{{ skill.name }}</h4> </div> </div> </div> {% endfor %} </div> </div> </div> </div> {% endblock content %}
解决方案
核心问题是背景容器用了固定高度height:1200px,小屏幕下背景图按比例缩小,但容器高度未随宽度适配,导致内部卡片溢出。调整方式如下:
- 替换固定高度为比例适配:去掉
height:1200px,改用padding-bottom: 75%(对应原宽高比1600:1200=4:3),让容器高度随宽度自动按比例调整,匹配背景图显示比例 - 添加内容溢出处理:给
.custom-skills-section加上overflow-y: auto和box-sizing: border-box,小屏幕内容超出时显示滚动条,同时避免padding额外增加容器尺寸 - 优化复古风格细节:调整卡片和背景的颜色、阴影、字体,强化复古质感
- 响应式间距优化:用
clamp()函数设置标题和卡片间距,适配不同屏幕
修改后的完整代码
{% block content %} <style> .custom-skills-section { background: url('{{ background_image }}') center center / contain no-repeat; border-radius: 10px; padding: 20px; margin: 0 auto; width: 100%; max-width: 1600px; /* 用padding-bottom维持4:3的宽高比 */ padding-bottom: 75%; height: auto; overflow-y: auto; box-sizing: border-box; /* 复古风格增强:泛黄底色叠加 */ background-color: #f8f5e6; background-blend-mode: overlay; box-shadow: 0 8px 16px rgba(0,0,0,0.15); } .custom-skill-card-shadow { height: 150px; display: flex; align-items: center; justify-content: center; /* 复古卡片阴影 */ box-shadow: 0 3px 5px rgba(0,0,0,0.2); } .custom-skill-card-content { border-radius: 8px; background-color: #fff9e6; display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; /* 复古卡片边框纹理 */ border: 1px solid #e0d8b0; } .custom-skill-title { font-size: clamp(0.9em, 2vw, 1.1em); font-weight: 600; color: #5a4a2e; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: 100%; /* 复古字体 */ font-family: 'Georgia', serif; } /* 响应式标题间距 */ .skill-heading { padding: clamp(1rem, 3vw, 2rem) 0; } </style> <div class="d-flex justify-content-center align-items-center" style="min-height: 100vh;"> <div class="custom-skills-section"> <h2 class="text-center text-white skill-heading">My Skills</h2> <div class="container"> <div class="row g-3"> {% for skill in skills %} <div class="col-lg-4 col-md-6 col-12 custom-skill-card"> <div class="custom-skill-card-shadow"> <div class="custom-skill-card-content"> <h4 class="custom-skill-title">{{ skill.name }}</h4> </div> </div> </div> {% endfor %} </div> </div> </div> </div> {% endblock content %}
内容的提问来源于stack exchange,提问作者Omar Saleem Haji
相关产品推荐
相关产品推荐

