gpt4 book ai didi

html - CSS - Center Font-awesome 图标

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:45 25 4
gpt4 key购买 nike

如何让 FontAwesome 图标在方 block 内居中?

此时,除 facebook 图标外,所有图标都或多或少地放置在右侧,尤其是 soundcloude 图标。

我一直在使用“w3schools - Social Media Buttons”示例,当正方形按比例缩小时似乎会出现此问题。

.fa {
padding: 15px;
text-align: center;
vertical-align:middle;
text-decoration: none;
margin: 15px 3px;
border-radius: 50%;
width: 7.5px;
height: 7.5px;
font-size: 15px;
line-height: 7.5px !important;
}

.fa {
background: #F0F0F0;
color: #282828;
}


.fa-facebook:hover {
background: #3B5998;
color: white;
line-height: inherit;
}

.fa-youtube:hover {
background: #bb0000;
color: white;
line-height: inherit;
}

.fa-instagram:hover {
background: #125688;
color: white;
line-height: inherit;
}

.fa-soundcloud:hover {
background: #ff5500;
color: white;
line-height: inherit;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<a href="#" class="fa fa-facebook"></a>
<a href="#" class="fa fa-youtube"></a>
<a href="#" class="fa fa-instagram"></a>
<a href="#" class="fa fa-soundcloud"></a>

最佳答案

去掉不必要的填充(不给图标留下空间),让元素的width/height/line-height确定它们的布局。

之前:

padding: 15px;
width: 7.5px;
height: 7.5px;
line-height: 7.5px !important;

之后:

width: 30px;
height: 30px;
line-height: 30px !important;

您还可以从 :hover 状态中删除一些冗余样式。

实例:

.fa {
text-align: center;
vertical-align: middle;
text-decoration: none;
margin: 15px 3px;
border-radius: 50%;
width: 30px;
height: 30px;
font-size: 15px;
line-height: 30px !important;
}

.fa {
background: #F0F0F0;
color: #282828;
}

.fa:hover {
color: #fff;
}

.fa-facebook:hover {
background: #3B5998;
}

.fa-youtube:hover {
background: #bb0000;
}

.fa-instagram:hover {
background: #125688;
}

.fa-soundcloud:hover {
background: #ff5500;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<a href="#" class="fa fa-facebook"></a>
<a href="#" class="fa fa-youtube"></a>
<a href="#" class="fa fa-instagram"></a>
<a href="#" class="fa fa-soundcloud"></a>

关于html - CSS - Center Font-awesome 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com