html中table标签及其它标签居中

1.style=" margin:auto"

使用margin外边距属性,让其上下左右自动均分剩余空间,就会使其标签居中显示
例如table标签:

<table border="1" cellpadding="15px" cellspacing="0" style=" margin:auto">

效果:
在这里插入图片描述

2.text-align: center

text-align 属性规定元素中的文本的水平对齐方式,结合margin来使用,使其table居中,table文本内容居中

<table border="1" cellpadding="15px" cellspacing="0" style=" margin:auto;text-align: center">

效果:
在这里插入图片描述

3.<center>

<center> 标签对其包围的标签进行水平居中处理。
注意:在 HTML 4.01 中,<center> 元素 已废弃。

在这里插入图片描述
如以上table标签被center标签包裹,其<center> 包裹的标签会水平居中。
但是其table内的文本不会居中,效果如下:
在这里插入图片描述

如下其它一些居中方法:

属性 作用
line-height 通过设置内联元素的高度(height)和行高(line-height)相等,从而使元素垂直居中。
vertical-align: middle 设置元素的垂直对齐方式
display: flex 利用弹性布局(flex),实现水平居中

猜你喜欢

转载自blog.csdn.net/MrYushiwen/article/details/107643809