17.定位的盒子居中顯示

效果:


代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>定位的盒子居中顯示</title>
		<style type="text/css">
			body{
				margin:0;
				padding:0;
			}
			.box{
				height:500px;
				background:gray;
				position:relative;
			}
			.nav{
				height:50px;
				width:800px;
				background:red;
				position:absolute;
				/*定位的盒子居中:先左右走父元素盒子的一半50%,
				 * 在向左走子盒子的一半(margin-left:負值。)*/
				bottom:0;
				/*這是關鍵*/
				left:50%;
				margin-left:-400px;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="nav"></div>
		</div>
	</body>
</html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章