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

Flex列文本水平居中及Grid布局导航、汉堡菜单居中问题咨询

你存在3处核心配置缺失/错误,修改后即可实现需求:

  1. flex布局方向属性拼写错误,你写的direction: row是无效属性,正确写法为flex-direction: row
  2. 没有清除ul标签自带的默认内外边距,同时没有给header容器加居中配置,导致导航内容偏移
  3. 汉堡菜单所在的.menu容器没有加水平、垂直居中的flex配置

修改后的完整CSS代码

.grid-container {
     display: grid;
     grid-template-areas: "header menu";
     grid-template-columns: repeat(8, 1fr);
}

.header {
    grid-area: header;
    grid-column: span 7;
    margin: 2px;
    background-color: black;
    /* 新增:让内部导航列表垂直水平居中 */
    display: flex;
    align-items: center;
    justify-content: center;
}

.nav-test {
     display: flex;
     /* 修正:属性名拼写错误 */
     flex-direction: row;
     justify-content: space-around;
     list-style-type: none;
     /* 新增:清除ul默认边距 */
     margin: 0;
     padding: 0;
     /* 新增:撑满宽度实现均匀分布 */
     width: 100%;
}

.nav-test a {
    color: white;
    /* 可选:增加上下内边距,优化垂直居中效果和点击区域 */
    padding: 12px 0;
 }

.menu {
    display: flex;
    grid-area: menu;
    grid-column: span 1;
    margin: 2px;
    background-color: black;
    /* 新增:让汉堡图标在当前列居中 */
    justify-content: center;
    align-items: center;
}

你后续往.menu容器里添加汉堡菜单的图标/按钮后,就会自动在最后一列居中显示,导航栏的链接也会均匀分布且垂直居中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 14:54:03