1、首先打开vscode编辑器,新建一个html文档,里面写入一个外层的div,再加入一行table表格:
2、然后在上方的style标签中加入css样式,设置table标签的样式,table的元素具有长度自适应性,其长度根据其内文本长度决定,只要设置margin属性,让左右设置为auto就回自动分配剩余空间,也就是元素两侧的区域各占50%,那么元素就左右居中了:
3、最后打开浏览器就可以看到居中的一行表格了:
html中让表格在浏览器中左右居中,可以设置表格的align属性align有三个属性:
1、居中:center;
2、靠左:left;
3、靠右:right。
html中是没办法实现上下居中的,如果设置上下居中,需要通过js程序来设置。
左右居中代码如下:
<!DOCTYPE html PUBLIC "-//W3C//<a href="https://www.baidu.com/s?wd=DTD&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mWwWuWR4rHbzmyDYnjbY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1R4PH0LnjTv" target="_blank" class="baidu-highlight">DTD</a>XHTML 1.0 <a href="https://www.baidu.com/s?wd=Tr&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mWwWuWR4rHbzmyDYnjbY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1R4PH0LnjTv" target="_blank" class="baidu-highlight">Tr</a>ansitional//EN" "http://www.w3.org/<a href="https://www.baidu.com/s?wd=TR&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mWwWuWR4rHbzmyDYnjbY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1R4PH0LnjTv" target="_blank" class="baidu-highlight">TR</a>/xhtml1/<a href="https://www.baidu.com/s?wd=DTD&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mWwWuWR4rHbzmyDYnjbY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1R4PH0LnjTv" target="_blank" class="baidu-highlight">DTD</a>/xhtml1-<a href="https://www.baidu.com/s?wd=tr&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mWwWuWR4rHbzmyDYnjbY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1R4PH0LnjTv" target="_blank" class="baidu-highlight">tr</a>ansitional.<a href="https://www.baidu.com/s?wd=dtd&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9mWwWuWR4rHbzmyDYnjbY0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1R4PH0LnjTv" target="_blank" class="baidu-highlight">dtd</a>">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<table width="400" height="400" border="1" align="center">
<tr align="center" valign="middle">
<td>居中</td>
<td>居中</td>
<td>居中</td>
</tr>
<tr align="center" valign="middle">
<td>居中</td>
<td>居中</td>
<td>居中</td>
</tr>
</table>
</body>
</html>
可以用“text-align”属性控制du文字的位置,“center”属性值让文字居中显示。
1、新建html文档,在body标签中添加一个div标签,在div标签中添加p标签,这时文字将会在div标签中靠左显示:
2、为div标签设置“text-align”属性,属性值为“center”,这时文本将会居中显示:
3、想让文本靠右显示,可以为“text-align”属性设置“right”属性值,这时文本将会靠右显示: