如何通过自定义CSS或修改HTML让自定义Liquid按钮在容器中水平居中?
解决Payfast订阅按钮水平居中问题
你当前的代码里,虽然<td>已经加了align="center"让按钮在单元格内居中,但整个<table>本身没有在页面容器里居中,所以按钮看起来还是没居中。下面提供两种简单的解决方法:
方法一:直接修改HTML代码
给外层的<table>添加margin: 0 auto的内联样式,让table在父容器中水平居中:
<table style="margin: 0 auto;"> <tbody> <tr> <td colspan="2" align="center"><input type="image" src="https://my.payfast.io/images/buttons/Subscribe/Dark-Large-Subscribe.png" alt="Subscribe" title="Subscribe with Payfast" /></td> </tr> </tbody> </table>
方法二:使用自定义CSS区块(不修改HTML)
如果不想改动HTML代码,可以在自定义CSS区块中添加以下样式,精准定位到这个Payfast按钮所在的table:
/* 定位到Payfast订阅按钮的table并设置居中 */ table:has(input[type="image"][src*="payfast.io"]) { margin-left: auto; margin-right: auto; }
如果你的主题支持给元素加自定义类名,也可以给<table>加上class="payfast-subscribe-table",然后用更简洁的CSS:
.payfast-subscribe-table { margin: 0 auto; }
原理:块级元素设置margin-left: auto和margin-right: auto时,会自动在父容器中水平居中,table默认属于块级元素,这个方法兼容性很好,能适配大多数场景。
内容的提问来源于stack exchange,提问作者Charne Crouse
相关产品推荐
相关产品推荐

