web前端複習第五課

                                                 固定定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.box1{
			width: 600px;
			height: 500px;
			background-color: #f00;
			margin: 0 auto;
		}
		.box2{
			width: 600px;
			height: 500px;
			background-color: #00f;
			margin: 0 auto;
		}
		.box3{
			width: 600px;
			height: 500px;
			background-color: #0f0;
			margin: 0 auto;
		}
		.box4{
			width: 200px;
			height: 200px;
			background-color: #ff0;
			
			/*定位沒有去不了的地方*/
			/*特點:脫離文檔流,根據瀏覽器窗口進行定位*/
			position: fixed;
			/*固定定位  可以使用百分比*/
			top: 100px;
			left: 50%;
			margin-left: -100px;

			/*配合定位屬性使用的改變層級的方法*/
			/*默認層級是0*/
			z-index: 2;
		}
		.box5{
			width: 200px;
			height: 200px;
			background-color: #000;
			
			/*定位沒有去不了的地方*/
			/*特點:脫離文檔流,根據瀏覽器窗口進行定位*/
			position: fixed;
			/*固定定位*/
			top: 50%;
			left: 120px;
			margin-top: -100px;

			z-index: 1;
		}
	</style>
</head>
<body>
	<div class="box1"></div>
	<div class="box2"></div>
	<div class="box3"></div>
	<div class="box4"></div>
	<div class="box5"></div>
</body>
</html>

                                                  相對定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.box1{
			width: 200px;
			height: 200px;
			background-color: #f00;
			margin: 100px auto 0px;
		}
		.box2{
			width: 200px;
			height: 200px;
			background-color: #0f0;
			margin: 0 auto;

			/*相對定位:主要是用來輔助絕對定位的,
			不脫離文檔流,根據自己原來的位置進行定位的*/
			position: relative;
			top: 0;
			left: 0;
		}
		.box3{
			width: 200px;
			height: 200px;
			background-color: #00f;
			margin: 0 auto;
		}
	</style>
</head>
<body>
	<div class="box1"></div>
	<div class="box2"></div>
	<div class="box3"></div>
</body>
</html>

                                                 絕對定位

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		.box1{
			width: 600px;
			height: 600px;
			border: 1px solid #000;
			margin: 0 auto;
			/*相對定位,添加上以後對自己沒有任何影響*/
			position: relative;
		}
		.box2{
			width: 400px;
			height: 400px;
			border: 1px solid #000;
			margin: 0 auto;
		}
		.box3{
			width: 200px;
			height: 200px;
			border: 1px solid #000;
			margin: 0 auto;
			background-color: #ccc;

			/*絕對定位:脫離文檔流,根據已有定位元素的父元素進行定位*/
			/*子絕父相*/
			position: absolute;
			top: 0;
			left: 0;
		}
	</style>
</head>
<body>
	<div class="box1">
		<div class="box2">
			<div class="box3"></div>
		</div>
	</div>
</body>
</html>

 

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