求助:无法将FontAwesome图标居中于圆形按钮内的解决办法
解决FontAwesome图标在圆形按钮内居中的问题
你的问题出在**display: inline-block和flex属性不兼容**——justify-content、align-items是flex容器专属样式,inline-block元素不会识别这些属性,所以图标无法居中。
直接修改两处就能解决:
- 把
.fa-brands的display: inline-block替换成display: inline-flex - 保留
justify-content: center和align-items: center,这两个属性现在就能生效
修改后的CSS代码:
.socialbuttons{ position: absolute; top: 50%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } .fa-brands{ text-decoration: none !important; color: white !important; background: #1a1a1a; width: 40px; height: 40px; flex-direction: column; display: inline-flex; /* 修改为inline-flex */ justify-content: center; align-items: center; border-radius: 100%; margin: 5px; }
另外给你一个备选方案(无需flex):
如果习惯用传统对齐方式,可以删掉flex相关属性,改用行高实现居中:
.fa-brands{ text-decoration: none !important; color: white !important; background: #1a1a1a; width: 40px; height: 40px; display: inline-block; line-height: 40px; /* 行高与高度一致 */ text-align: center; /* 水平居中 */ border-radius: 100%; margin: 5px; }
这两种方法都能让图标完美居中在圆形按钮内。
内容的提问来源于stack exchange,提问作者Nebula
相关产品推荐
相关产品推荐

