网页制作 
首页 > 网页制作 > 浏览文章

css 利用 position + margin 实现固定盒子横向纵向居中的方法

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

margin:auto; + position: absolute; 上下左右:0
一看 demo 就懂的啦

<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<title>利用 position + margin 实现固定盒子横向纵向居中</title>
	<style>
		body * {
			outline: 1px #ff0000 solid;
		}

		.father {
			width: 500px;
			height: 300px;
			position: relative;
		}

		.father .son {
			width: 180px;
			height: 140px;
			/*  */
			position: absolute;
			right: 0;
			bottom: 0;
			top: 0;
			left: 0;
			margin: auto;
		}
	</style>
</head>

<body>
	<div class="father">
		<div class="son">margin:auto + 上下左右:0</div>
	</div>
</body>

</html>

//end

上一篇:css实现弹窗上下左右居中且背景透明锁定窗口效果
下一篇:html5+css3之制作header实例与更新
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网