新手求助:如何调整HTML/CSS中Schutzklasse 1-3的Figure及文字尺寸?
解决方案:修复Schutzklasse 1-3区域的响应式尺寸问题
作为刚接触HTML/CSS的新手,你遇到的问题主要是HTML结构不规范加上CSS布局细节没适配响应式导致的,咱们一步步来解决:
1. 先修复HTML的结构错误
你用了<figure id="gallery">作为容器包裹子<figure>,但HTML规范里figure标签不能嵌套figure,这会让浏览器渲染混乱,样式无法正常生效。把外层的figure改成div就好:
修改后的核心HTML代码:
<h1>Der VDE Guide</h1> <p> </p> <h3>Bitte wählen Sie eine der Schutzklassen:</h3> <div class="gallery"> <!-- 把外层的figure替换成div,符合HTML语义规范 --> <div id="gallery"> <figure> <a href="messungSK1.1.html"> <img id="sk1" src="schutzklasse1.png"> </a> <figcaption>Schutzklasse 1</figcaption> </figure> <figure> <a href="messungSK2.1.html"> <img id="sk2" src="schutzklasse2.png"> </a> <figcaption>Schutzklasse 2</figcaption> </figure> <figure> <a href="messungSK3.1.html"> <img id="sk3" src="schutzklasse3.png"> </a> <figcaption>Schutzklasse 3</figcaption> </figure> </div> </div>
2. 调整CSS实现响应式缩放
接下来修改CSS部分,解决图片不缩放、文字适配的问题,我会保留你原有其他样式,只修改关键部分:
/* 重置基础样式,确保盒模型统一 */ figure, figcaption { margin: 0; padding: 0; box-sizing: border-box; /* 避免内边距/边框撑大元素 */ } .gallery { display: block; text-align: center; padding: 0 1rem; /* 小屏幕下避免内容贴边 */ } #gallery { display: grid; /* 用auto-fit实现自适应列数,minmax确保卡片不会太小 */ grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 2rem; /* 用rem替代百分比,间距更稳定 */ margin: 0 auto; /* 让容器居中,替代左右margin百分比 */ max-width: 900px; /* 限制最大宽度,避免大屏幕内容过宽 */ } #gallery figure { position: relative; color: black; background: white; overflow: hidden; /* 确保figcaption不会超出卡片范围 */ border-radius: 4px; /* 可选:给卡片加圆角,视觉更友好 */ } /* 关键:让图片保持宽高比,完全适配容器 */ figure img { width: 100%; height: auto; /* 确保缩放时图片不变形 */ display: block; } #gallery>figure>figcaption { position: absolute; bottom: 0; width: 100%; line-height: 2.1em; color: white; background: rgba(0, 0, 0, 0.7); padding: 0 0.5rem; /* 给文字加内边距,避免贴边 */ box-sizing: border-box; /* 用transform实现更流畅的过渡效果 */ opacity: 0.4; transform: translateY(100%); transition: all 0.3s ease; } #gallery>figure:hover>figcaption { opacity: 1; transform: translateY(0); } /* 保留你原有的其他样式,这里也优化了一个图片的响应式 */ .m1 { border: 3px solid #4CAF50; padding: 5px; margin-left: 15%; margin-right: 15%; margin-top: 5%; margin-bottom: 5%; } .img1 { float: right; } .m1::after { content: ""; clear: both; display: table; } .img1 { height: auto; width: 30%; /* 把固定宽度改成百分比,让这个图片也能响应式 */ margin-top: 2%; margin-bottom: 4%; margin-left: 4%; } .ans1 { display: inline-block; width: 320px; padding: 20px; border: 5px solid gray; margin: 10px; } .a1 { margin-left: 15%; margin-right: 15%; } h2 { margin-left: 3%; } ul { margin-left: 5%; margin-top: 8%; } body { color: rgba(50, 50, 50, 1.0); background: rgba(200, 200, 200, 0.825); } h1 { text-decoration: underline; text-decoration-color: blueviolet; text-align: center; margin-bottom: 2em; } b { text-decoration: underline; } h3 { text-shadow: 2px 2px rgb(255, 255, 255); text-align: center; margin-bottom: 2em; } h4 { text-decoration: underline; text-align: center; margin-bottom: 2em; }
关键修改点说明
- 修复嵌套错误:
figure不能嵌套,改成div后样式才能正常作用到子元素 - 图片响应式:给
img加height: auto,确保缩放时保持宽高比,不会变形 - Grid布局优化:
auto-fit让小屏幕自动减少列数,minmax保证卡片不会太小 - figcaption优化:用
transform替代bottom调整位置,过渡更流畅;overflow: hidden避免文字超出卡片 - 单位优化:把部分百分比改成
rem,不同屏幕下间距更稳定;给.img1也改成百分比宽度,让它也能响应式缩放
现在你可以测试一下,页面缩放时三个Schutzklasse的卡片(图片+文字)会一起适配尺寸,文字也会跟着容器正常显示啦。
内容的提问来源于stack exchange,提问作者sdgsdgsdf
相关产品推荐
相关产品推荐

