如何实现文本+按钮响应式布局,屏幕宽度≤664px时按钮位于文本下方
实现文本加按钮响应式布局的方案
你原来使用的table布局不支持元素换行,因此无法实现小屏纵向排列的效果,我们可以替换为flex布局来实现需求,修改后的完整代码如下:
完整CSS代码
.main { flex: 63%; background-color: white; } .main-box { background-color: #ffffff; min-height: 130px; display: flex; align-items: center; width: 100%; max-width: 798px; border: solid #E6E6E6; border-width: 0 1px 1px 1px; box-sizing: border-box; } .text-in-main-box{ font-weight: bold; font-family: rob; width: 60%; padding: 2% 7%; box-sizing: border-box; } .container-for-button-in-main-box{ width: 40%; box-sizing: border-box; padding-right: 7%; text-align: right; } .button-in-main-box{ display:inline-block; padding:0.8em 1.2em; margin:0 0.3em 0.3em 0; border-radius:4em; box-sizing: border-box; text-decoration:none; font-family:rob; font-weight:bold; color:#ffffff; background-color:#34E034; text-align:center; transition: all 0.2s; cursor: pointer; } .button-in-main-box:hover{ transform: scale(1.1); } /* 小屏断点适配 */ @media screen and (max-width: 664px) { .main-box { flex-direction: column; padding: 20px 15px; justify-content: center; } .text-in-main-box { width: 100%; text-align: center; padding: 0 0 15px 0; } .container-for-button-in-main-box { width: 100%; text-align: center; padding: 0; } }
修正后的HTML代码
仅修复了原代码中class属性缺少引号的小问题:
<div class="main-box"> <div class="text-in-main-box">Sage “Hallo, Welt" mit Python</div> <div class="container-for-button-in-main-box"><span class="button-in-main-box">Löse Aufgabe</span></div> </div>
修改后桌面端会保持文本和按钮横向排列的布局,当屏幕宽度≤664px时会自动切换为文本在上、按钮在下的纵向排列,符合你的需求。
内容的提问来源于stack exchange,提问作者AIIMOOR
相关产品推荐
相关产品推荐

