You need to enable JavaScript to run this app. 如何在Material-UI Accordion中实现按钮不受内容长度影响统一对齐
优惠活动
大模型
产品
解决方案
定价
更多

如何在Material-UI Accordion中实现按钮不受内容长度影响统一对齐

解决方案

核心利用Flex布局的空间分配特性实现按钮固定对齐,无需依赖动态边距设置:

  1. 调整Accordion.js中标题样式,移除固定百分比右边距,新增flex: 1属性让标题容器自动填充AccordionSummary的剩余可用空间,按钮会被自动挤到所有标题的右侧同一位置,完全不受标题长度影响。
  2. 若存在按钮垂直对齐偏移问题,可给自定义按钮容器新增垂直居中属性即可。

修改后代码片段

Accordion.js 样式部分修改

const useStyles = makeStyles((theme) => ({
  root: {
    width: '100%',
  },
  heading: {
    flex: 1, // 新增核心属性,占满剩余横向空间
    marginTop: "0.75rem",
    // 删除原有的 marginRight: "20%" 配置
    fontSize: theme.typography.pxToRem(15),
    fontWeight: theme.typography.fontWeightRegular,
  },
}));

可选:IconButton.js 垂直对齐优化(如果按钮和标题没有垂直居中)

const useStyles = makeStyles((theme) => ({
  root: {
    alignSelf: 'center', // 新增垂直居中属性
    '& > *': {
    //   margin: theme.spacing(1),
    },
  },
}));

修改后所有跳转按钮都会固定在展开箭头左侧的同一位置,无论菜品名称长短都能完全对齐。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 21:24:02