按钮垂直居中首选flex布局(display: flex; align-items: center; justify-content: center),兼容多行、图标等复杂场景;单行文本可简化用line-height等于height,但换行即失效。
按钮内容不垂直居中,通常是因为默认的行内元素(如文字)在块级容器中缺乏明确的对齐控制。最常用、最可靠的两种方式是 l
ine-height(适用于单行文本)和 flex(兼容多行、图标+文字等复杂场景)。
当按钮高度固定、且只含单行文字时,把 line-height 设为和按钮高度一致,文字会自然垂直居中。
display: inline-block 或 block,且有明确的 height
line-height: [相同数值]px,例如 height: 40px; line-height: 40px;
给按钮设 display: flex,再用 align-items: center 垂直居中,justify-content: center 水平居中,一步到位,无副作用。
min-height 控制最小尺寸)padding 和 border 影响,避免视觉偏差有时看着“明明设了 flex 却还是偏上”,很可能是 box-sizing: content-box(默认值)下,padding 加在 height 外部,导致实际可布局区域变小。
box-sizing: border-box,让 padding/border 包含在 width/height 内vertical-align(对 inline-flex 元素仍有影响)line-height 和 flex,二者混用容易冲突基本上就这些。line-height 简单快,flex 更通用——选哪个,看你的按钮是不是只放一行字。