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

使用Flex垂直居中:设置高度与align-items仍无法居中的问题

解决文本垂直居中问题

问题出在你没给flex容器添加垂直对齐属性。你用了justify-content: center实现水平居中,但flex布局里控制垂直方向对齐的是align-items: center,加上这一行就能让文本垂直居中。

另外,text-align: center在这里其实多余——justify-content: center已经能让flex子元素水平居中,a标签作为行内元素在flex容器里会被当作flex项,直接受flex属性控制。

修改后的CSS代码如下:

.subscribe-btn {
  display: flex;
  margin: 0.5em;
  width: 290px;
  height: 50px;
  background-image: linear-gradient(#4968b0, #3d559e);
  border-radius: 20px;
  cursor: pointer;
  border-style: outset;
  justify-content: center;
  align-items: center; /* 新增这行实现垂直居中 */
}

.subscribe-btn a {
  font-weight: bold;
  font-size: 16px;
  color: white;
  text-decoration: none;
}

不需要使用position属性,flex布局本身就能简洁实现居中效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 17:17:04