You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

侧边栏FontAwesome图标无法对齐问题求助

解决FontAwesome图标基线对齐问题

问题根源有两点:一是不同FontAwesome图标的设计基线本身存在差异,二是当前布局未给列表项内元素设置统一对齐规则,加上display: table的用法干扰了垂直对齐逻辑。

直接修改CSS即可解决,以下是调整后的完整代码:

.side_bar {
    font-family: 'Montserrat', sans-serif;
    position: fixed;
    height: 100%;
    background-color: #fff;
    overflow: hidden;
    transition: width 0.2s linear;
    box-shadow: 0 20px 35px rgba(0, 0, 0, 0.1);
    border-right: 1px solid rgba(0, 0, 0, 0.5);
    width: 310px;
}

.side_bar ul {
    list-style: none;
    padding: 0; /* 清除默认内边距 */
    margin: 0; /* 清除默认外边距 */
}

.side_bar li {
    padding: 1rem;
    width: 300px;
    display: flex; /* 用flex实现垂直对齐 */
    align-items: center; /* 让图标和文字垂直居中对齐 */
    gap: 0.7rem; /* 统一元素间距,替代零散的margin/padding */
}

.side_bar i {
    width: 24px; /* 给图标设置固定宽度,确保水平对齐 */
    text-align: center; /* 图标居中,抵消自身宽度差异 */
}

/* 保留原有下拉菜单样式 */
li:has(div.down) {
    position: relative;
    overflow: hidden;
    height: 0px;
    transition: height 1s linear;
}

.down {
    font-size: 0.7rem;
    font-weight: bold;
    margin-left: 4rem;
    margin-top: 1rem;
    border-left: 3px solid rgb(195, 190, 190);
    padding-left: 2rem;
    padding-bottom: 2rem;
    position: absolute;
}

.down > p {
    margin-top: 1rem;
    color: grey;
}

li:has(div.down):hover {
    height: var(--prog);
}

关键调整说明:

  • 移除ul的display: table,同时清除默认内外边距,避免不必要的布局偏移。
  • 给li设置display: flex和align-items: center,强制图标与文字在垂直方向居中对齐,抹平不同图标基线差异。
  • 给图标设置固定宽度并居中对齐,确保所有图标占据相同水平空间,彻底解决视觉错位问题。

内容的提问来源于stack exchange,提问作者gaito Uzumaki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:07:12