如何用HTML/CSS实现社交图标响应式(移动端横向排列)
个人作品集社交图标响应式解决方案
要实现社交图标响应式,且在移动端横向排列,只需对现有CSS做以下修改:
修改后的完整CSS代码
.home{ display: flex; flex-wrap: wrap; background: url("/img/banner.jpg") no-repeat center; height: 100vh; color: #fff; min-height: 500px; background-size: cover; background-attachment: fixed; font-family: 'Ubuntu', sans-serif; position: relative; /* 让社交图标的绝对定位基于home容器 */ } .home .max-width{ width: 100%; display: flex; } .home .max-width .row{ margin-right: 0; } .home .home-content .text-1{ font-size: 27px; } .home .home-content .text-2{ font-size: 75px; font-weight: 600; margin-left: -3px; } .home .home-content .text-3{ font-size: 40px; margin: 5px 0; } .home .home-content .text-3 span{ color: #32de84; font-weight: 500; } .home .home-content a{ display: inline-block; background: #32de84; color: #fff; font-size: 25px; padding: 12px 36px; margin-top: 20px; font-weight: 400; border-radius: 6px; border: 2px solid #32de84; transition: all 0.3s ease; } .home .home-content a:hover{ color: #32de84; background: none; } .social{ position: absolute; top: 35%; right: 5%; color: #fff; } .social ul{ list-style: none; padding: 0; /* 清除默认内边距 */ } .social li{ margin-bottom: 20px; text-align: center; } .circle{ background-color: transparent; border: 2px solid white; border-radius: 200px; height: 35px; width: 35px; display: flex; align-items: center; justify-content: center; /* 图标垂直水平居中 */ } .social i{ color: #ffffff; height: auto; width: auto; /* 取消固定宽高,让图标自适应 */ } @media (max-width: 947px) { .home .home-content .text-2 { font-size: 60px; } /* 移动端社交图标布局调整 */ .social{ position: static; /* 取消绝对定位,回到文档流 */ margin-top: 30px; /* 和上方内容保持间距 */ width: 100%; } .social ul{ display: flex; /* 横向排列 */ justify-content: center; /* 水平居中 */ gap: 20px; /* 设置图标间横向间距 */ } .social li{ margin-bottom: 0; /* 移除纵向间距 */ } }
关键修改说明
- 父容器定位修正:给
.home添加position: relative,确保社交图标的绝对定位基于正确的父元素,避免布局偏移。 - 图标居中优化:用flex布局改造
.circle,让图标无需依赖padding-top即可完美居中,布局更稳定。 - 移动端布局调整:
- 取消
.social的绝对定位,让它跟随页面内容排列。 - 给
.social ul设置flex布局,实现图标横向居中排列。 - 用
gap替代margin-bottom,统一控制图标间的间距。
- 取消
内容的提问来源于stack exchange,提问作者Piyush Shrivastava
相关产品推荐
相关产品推荐

