如何基于视口宽度调整表单与图片的响应式布局?
表单移动端适配解决方案
可以通过CSS @media 查询实现移动端上下排列的布局,先修复现有CSS里的语法问题,再添加响应式规则就能解决图片不缩放、表单重叠截断的问题。
问题分析
现有CSS存在几处语法错误(比如width: 90% overflow: auto缺分号),且未针对移动端调整flex布局方向,图片容器的宽度限制未生效,导致移动端布局错乱。
修改后的完整CSS代码
@font-face { font-family: museo-sans; src: url(/fonts/museosans_300.otf) format("opentype"); font-display: auto; font-style: normal; font-weight: 300; font-stretch: normal } @font-face { font-family: museo-sans-bold; src: url(/fonts/museosans_700.otf) format("opentype"); font-display: auto; font-style: normal; font-weight: 700; font-stretch: normal } @font-face { font-family: bwstretch; src: url(/fonts/BWSTRETCH-BLACK.OTF) format("opentype"); font-display: auto; font-style: normal; font-weight: 800; font-stretch: normal } a { font-family: museo-sans-bold; color: #ffc860; } h2 { font-family: bwstretch; text-align: center; text-transform: uppercase; font-size: 3vh !important; } body { font-size: 1.5vh; background-color: #191f43; font-family: museo-sans; color: #191f43; } #mc_embed_signup { width: 90%; overflow: auto; height: 90%; display: flex; align-items: center; justify-content: center; gap: 2rem; vertical-align: middle; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%); color: #191f43; background-color: #191f43; color: #ffc860; font-family: museo-sans; } div.mc-field-group { width: 90%; overflow: auto; } div.mc-field-group label { font-size: 2vh; font-family: museo-sans-bold; width: auto; } div.mc-field-group input { font-size: 1.5vh; font-family: museo-sans-bold; width: calc(100% - 20px); color: #191f43; border: 0; height: 30px; border-radius: 5px; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; margin-left: 10px; } #image { max-width:30%; overflow: auto; background-color: #191f43; } #image img { width: 100%; height: auto; } .content__gdpr { display: flex; justify-content: center; align-items: center; } #mc-embedded-subscribe-form input[type=checkbox]{ display: flex; width: auto; margin-right: 0px; margin-left: -15px; margin-top: -2px; } #mergeRow-gdpr { margin-top: 20px; font-size: 1.5vh; } #mergeRow-gdpr fieldset label { font-weight: normal; } #mc-embedded-subscribe-form .mc_fieldset { border:none; min-height: 0px; padding-bottom:0px; } input.button { color: #191f43; background-color: #ffc860; padding-top: 10px; padding-bottom: 10px; padding-left: 45px; padding-right: 45px; font-family: bwstretch; text-transform: uppercase; font-size: 1.5em; border: 0; border-radius: 40px; } span { display: flex; align-items: center; } /* 移动端响应式规则 */ @media (max-width: 768px) { #mc_embed_signup { flex-direction: column; width: 95%; gap: 1rem; } #image { max-width: 100%; } div.mc-field-group input { width: calc(100% - 20px); margin-left: 0; } input.button { padding-left: 30px; padding-right: 30px; font-size: 1.2em; } }
关键修改点说明
- 修复CSS中
width属性后缺分号的语法错误,确保样式正常生效 - 给
#mc_embed_signup添加gap属性,避免桌面端元素挤在一起 - 给图片设置
width:100%;height:auto,实现图片自适应容器宽度 - 添加
@media (max-width:768px)查询,在移动端将flex布局改为垂直排列,调整图片宽度为100%,优化表单元素宽度和按钮尺寸 - 用
calc()计算输入框宽度,避免padding导致的溢出问题
内容的提问来源于stack exchange,提问作者GM Rod
相关产品推荐
相关产品推荐

