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

侧边栏文本溢出问题求助:实现文本居中并修复溢出

问题分析与解决方案

问题原因

  1. .sidebar nav设置了padding: 40px,让导航内容固定偏移,无法实现真正的居中对齐
  2. .sidebar nav li的white-space: nowrap强制文本不换行,搭配overflow: hidden直接截断长文本,同时display: inline-block属于多余设置,干扰了块级元素的布局逻辑
  3. 未给导航列表添加明确的居中对齐样式规则

解决方案

根据需求提供两种可行方案:

方案1:文本自动换行+完全居中

适合需要完整显示所有文本的场景,修改后的CSS代码如下:

*{
    margin: 0;
    padding: 0;
}

.sidebar{
    background-color: rgb(189, 186, 186);
    width: 10vw;
    font-family: "Fira Sans", sans-serif;
    height: 100vh;
}

/* 调整nav的padding,上下保留间距,移除左右固定padding */
.sidebar nav{
    padding: 40px 0;
    width: 100%;
}

/* 给ul设置文本居中,让所有li内容对齐 */
.sidebar nav ul{
    text-align: center;
}

.sidebar nav li{
    list-style: none;
    font-size: 20px;
    padding: 33px 0;
    /* 移除inline-block、white-space、overflow等多余设置 */
}

.main{
    width: 90vw;
}

.container{
    display: flex;
}

.infoContainer{
    height: 58vh;
    width: 80vw;
    margin: 140px auto;
    display: flex;
    justify-content: space-around;
}

.devInfo{
    display: flex;
    justify-content: center;
    flex-direction: column;
    font-family: "Source Code Pro", monospace;
    margin: 10px;
}

.hello{
    font-size: 50px;
}
.name{
 font-size: 25px;
 font-weight: bold;
 font-family: "Fira Sans", sans-serif;
}
.about{
 font-size: 20px;
}

.moreAbout{
    font-size: 15px;
    font-family: "Fira Sans", sans-serif;
}

.buttons {
    margin-top: 34px;
}
.buttons button{
    padding: 9px 14px;
    border-radius: 22px;
    color: white;
    background-color: dodgerblue;
    font-weight: bold;
    font-size: 21px;
    margin: 0 3px;
    cursor: pointer;
}

.devPic img{
    height: 58vh;
}

方案2:文本单行显示+省略号提示+居中

适合希望保持单行布局,过长文本用省略号提示的场景,修改后的CSS代码如下:

*{
    margin: 0;
    padding: 0;
}

.sidebar{
    background-color: rgb(189, 186, 186);
    width: 10vw;
    font-family: "Fira Sans", sans-serif;
    height: 100vh;
}

/* 给nav添加左右小padding,避免文本贴边 */
.sidebar nav{
    padding: 40px 10px;
    width: calc(100% - 20px);
}

.sidebar nav ul{
    text-align: center;
}

.sidebar nav li{
    list-style: none;
    font-size: 20px;
    padding: 33px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis; /* 添加省略号效果 */
    width: 100%; /* 确保li宽度继承容器,触发省略号规则 */
}

.main{
    width: 90vw;
}

.container{
    display: flex;
}

.infoContainer{
    height: 58vh;
    width: 80vw;
    margin: 140px auto;
    display: flex;
    justify-content: space-around;
}

.devInfo{
    display: flex;
    justify-content: center;
    flex-direction: column;
    font-family: "Source Code Pro", monospace;
    margin: 10px;
}

.hello{
    font-size: 50px;
}
.name{
 font-size: 25px;
 font-weight: bold;
 font-family: "Fira Sans", sans-serif;
}
.about{
 font-size: 20px;
}

.moreAbout{
    font-size: 15px;
    font-family: "Fira Sans", sans-serif;
}

.buttons {
    margin-top: 34px;
}
.buttons button{
    padding: 9px 14px;
    border-radius: 22px;
    color: white;
    background-color: dodgerblue;
    font-weight: bold;
    font-size: 21px;
    margin: 0 3px;
    cursor: pointer;
}

.devPic img{
    height: 58vh;
}

内容的提问来源于stack exchange,提问作者Awais Mirza

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:32:48