【居中】相对定位 + 绝对定位 或 Flexbox 居中

方法一:相对定位 + 绝对定位

关键代码:

bash 复制代码
父:position: relative"
子:  position: absolute;
     top: 50%;
     left: 50%;
     transform: translate(-50%, -50%);
bash 复制代码
 <button style="background: none;border: none;position: relative">     
      <img src="https://youke1.picui.cn/s1/2025/10/24/68fb0cabaa3af.png" width="233px" height="51px"/>
     <span style="
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-weight: bold;
    pointer-events: none; /* 不阻止按钮点击 */
  ">Repay now</span>
    </button>

方法二:Flexbox 居中

说明:

按钮用 Flexbox 容器居中内容

图片绝对定位覆盖按钮背景

文字自动居中

bash 复制代码
<button>
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 233px;
  height: 51px;
">
  <img src="https://youke1.picui.cn/s1/2025/10/24/68fb0cabaa3af.png" style="position: absolute; width: 100%; height: 100%; " />
  <span style="color: white; font-weight: bold; z-index: 1">Cancel</span>
</button>

3、总结

方法 特点
相对 + 绝对定位 精准控制文字位置,适合单行文字
Flexbox 容易适配多行文字,响应式方便
相关推荐
yBmZlQzJ2 天前
财运到内网穿透域名解析技术机制与中立评估
运维·经验分享·docker·容器·1024程序员节
yBmZlQzJ2 天前
内网穿透工具通过端口转发实现内外网通信
运维·经验分享·docker·容器·1024程序员节
数据皮皮侠AI2 天前
数字经济政策工具变量数据(2008-2023)
大数据·数据库·人工智能·笔记·1024程序员节
网安_秋刀鱼3 天前
【java安全】shiro反序列化1(shiro550)
java·开发语言·安全·web安全·网络安全·1024程序员节
unable code5 天前
攻防世界-Misc-Wire1
网络安全·ctf·misc·1024程序员节
开开心心就好6 天前
版本转换工具,支持Win双系统零售批量版
linux·运维·服务器·pdf·散列表·零售·1024程序员节
开开心心就好6 天前
免费卸载工具,可清理残留批量管理启动项
linux·运维·服务器·windows·随机森林·pdf·1024程序员节
unable code6 天前
攻防世界-Misc-4-1
网络安全·ctf·misc·1024程序员节
yBmZlQzJ8 天前
免费内网穿透-端口转发配置介绍
运维·经验分享·docker·容器·1024程序员节