You need to enable JavaScript to run this app. Material UI V5 选中列表项样式设置:如何添加5px宽度左侧边框
优惠活动
大模型
产品
解决方案
定价
更多

Material UI V5 选中列表项样式设置:如何添加5px宽度左侧边框

解决方案

你配置不生效的原因有两个:

  1. 实际代码中使用的可点击列表项是ListItemButton组件,并非你配置的ListItem,样式覆盖的目标组件匹配错误
  2. Material V5 已废弃$selected这种状态引用写法,直接使用.Mui-selected类选择器匹配选中状态即可

正确的全局主题配置代码:

const theme = createTheme({
  palette: {
    primary: {
      main: colors.primary
    }
  },
  components: {
    MuiListItemButton: {
      styleOverrides: {
        root: {
          "&.Mui-selected": {
            borderLeft: `5px solid ${colors.primary}`,
            // 调整左侧内边距,抵消边框宽度保证内容和未选中项对齐
            paddingLeft: 11
          }
        }
      }
    }
  }
});

如果不想全局配置,也可以直接在ListItemButton组件上通过sx属性局部设置样式:

<ListItemButton
  selected={selectedIndex === 0}
  onClick={(event) => handleListItemClick(event, 0)}
  sx={{
    '&.Mui-selected': {
      borderLeft: '5px solid #你的主题色',
      paddingLeft: 11
    }
  }}
>
  {/* 组件内容 */}
</ListItemButton>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 14:00:03