CSS设置Font Awesome图标font-size不生效如何解决
问题描述
尝试使用font-size: 45px;放大站点内的Font Awesome图标,但样式未生效,以下是相关CSS代码、正常效果与实际效果对比:
现有CSS代码
{ padding: 0; margin: 0; box-sizing: border-box; font-family: 'poppins', sans-serif; } .topbar{ position: fixed; background: #fff; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08); width: 100%; height: 60px; padding: 0 20px; display: grid; grid-template-columns: 2fr 10fr 0.4fr 1fr; align-items: center; z-index: 1; } .logo h2{ color: #d34d4d; } .search{ position: relative; width: 60%; justify-self: center; } .search input{ width: 100%; height: 40px; padding: 0 40px; font-size: 16px; outline: none; border: none; border-radius: 10px; background: #f5f5f5; } .search >label { position: absolute; right: 15px; top: 50%; transform: translateY(-50%); } .user{ position: relative; width: 50px; height: 50px; } .user img{ position: absolute; top: 0; left: 0; height: 100%; width: 100%; object-fit: cover; } /* sidebar starts here */ .sidebar{ position: fixed; top: 60px; width: 260px; height: calc(100% - 60px); background: #e26666; overflow-x: hidden; } .sidebar ul { margin-top: 20px; } .sidebar ul li{ width: 100%; list-style: none; } .sidebar ul li a { width: 100%; text-decoration: none; color: rgb(255, 255, 255); height: 60px; display: flex; padding-left: 30px; justify-content: flex-start; align-items: center } .sidebar ul li a i { min-width: 60px; font-size: 24px; text-align: center; align-self: flex-start; } .sidebar ul li a svg{ margin-right: 15px; width: 25px !important; } /* main section starts here*/ .main { position: absolute; top: 60px; width: calc(100% - 260px); left: 260px; min-height: calc(100vh - 60px); background: #f3f3f3; } .cards{ width: 100%; padding: 35px 20px; display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 20px; } .cards .card{ padding: 20px; display: flex; align-items: center; justify-content: space-between; background: #fff; border-radius: 10px; box-shadow: 0 7px 25px 0 rgb(0, 0, 0, 0.08); } .number{ font-size: 35px; font-weight: 500; color: #e26666; } .card-name{ color: #888; font-weight: 600; } .icon-box i { font-size: 45px; color: #e26666; }
效果对比
- 正常生效示例(图标可正常调整大小):

- 个人站点效果(图标大小未调整):

故障原因
font-size属性仅对字体格式的Font Awesome图标生效。你当前站点引入的是SVG版本的Font Awesome:页面加载完成后,你写的<i>图标标签会被自动替换为内联<svg>元素,字体尺寸规则无法控制SVG元素的大小。
你的代码中已经存在.sidebar ul li a svg{width: 25px !important;}这条针对SVG的样式规则,也能佐证页面图标实际是以SVG形式渲染的。
解决方法
根据使用场景二选一即可:
- 继续使用SVG版本Font Awesome:将控制图标大小的属性从
font-size改为针对SVG的宽高设置,卡片区域图标样式修改为:
.icon-box svg { width: 45px; height: 45px; color: #e26666; }
注意你侧边栏写了全局svg宽度的!important规则,需要把侧边栏的选择器写得更精准,或者提高卡片区域样式的优先级,避免样式互相覆盖。
- 换回Web Font版本Font Awesome:引入Font Awesome的字体模式CSS资源,移除SVG版本对应的JS加载文件,此时图标会以字体字符形式渲染,原有的
font-size: 45px规则可以直接生效。
内容的提问来源于stack exchange,提问作者pomoworko.com
相关产品推荐
相关产品推荐

