当前位置:首页 > 千层云服务器 > 正文

html怎么设置div居中

html设置div居中的方法:1.创建html文件;2.添加html架构代码到html文件中;3.在html架构中的body标签里面使用div标签设置一个div盒子;4.在html架构中的html标签里面添加script标签并设置居中样式;5.通过浏览器方式查看设置效果。

html怎么设置div居中

html设置div居中的方法:

通过设置“左右margin”值为“auto”来实现居中。

操作步骤:

1.首先创建一个html文件。

2.将html架构代码添加到html文件中。

<!DOCTYPEhtml>

<html>

<head>

<title>div居中示例</title>

</head>

<body>

</body>

</html>

3.在html架构中的body标签里面使用div标签设置一个div盒子。

<div>定宽块状元素水平居中</div>

4.在html架构中的html标签里面添加script标签并设置居中样式。

<style>

div{

border:1pxsolidred;

width:200px;/*定宽*/

margin:20pxauto;/*margin-left与margin-right设置为auto*/

}

</style>

5.最后可通过浏览器方式阅读html文件查看设置效果。

完整示例代码:

<!DOCTYPEhtml>

<html>

<head>

<title>div居中示例</title>

</head>

<style>

div{

border:1pxsolidred;

width:200px;/*定宽*/

margin:20pxauto;/*margin-left与margin-right设置为auto*/

}

</style>

<body>

<div>定宽块状元素水平居中</div>

</body>

</html>

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

在线客服