欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现div居中 div水平居中

程序员文章站 2022-05-01 22:51:02
...

需要的主要css代码有两个,

(1)一个为text-align:center(内容居中),

(2)另外一个为margin:0 auto;其两个样式需要配合使用才能实现div盒子的居中显示排版。

 首先我们对body设置text-align:center,再对需要居中的div盒子设置css样式,margin:0 auto,这样就可以实现div水平居中。

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>div居中</title>
		<style>
			body {
				text-align: center
			}
			
			.div {
				margin: 0 auto;
				width: 400px;
				height: 100px;
				border: 1px solid #F00
			}
			/* css注释:为了观察效果设置宽度 边框 高度等样式 */
		</style>
	</head>

	<body>
		<div class="div">
			DIV实现居中
		</div>
	</body>

</html>
注:此居中方法是让div居中效果完美兼容各大平台、兼容各大浏览器,无论高版本ie还是高版本的ie均兼容。


相关标签: html css