天天看點

CSS元素在父元素居中顯示總結

一.水準居中(margin:0 auto;)

注意:被包裹的元素不能有浮動屬性,否則會失效。

示例代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	        }
	        .item{
	            margin:0 auto;
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>
           

效果圖:

CSS元素在父元素居中顯示總結

二.水準居中(text-align:center;)

注意:沒有浮動的情況下,設定display:inline-block

示例代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	            text-align:center;
	        }
	        .item{
	            display:inline-block;
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>
           

三.水準垂直居中(方式1)

注意:子元素相對于父元素絕對定位,并且margin值減去自己寬高的一半(必須要知道子元素本身的寬高)

示例代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	            position: relative;
	        }
	        .item{
	            position: absolute;
            	top: 50%;
            	left: 50%;
            	margin-top: -50px;
            	margin-left: -50px;
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>
           

效果圖:

CSS元素在父元素居中顯示總結

四.水準垂直居中(方式2)

注意:子元素相對于父元素絕對定位,并且margin值位auto(不受元素寬高所限制,比較好用)

示例代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	            position: relative;
	        }
	        .item{
	            position: absolute;
            	top: 0;
            	left: 0;
            	right: 0;
            	bottom: 0;
            	top:0;
            	margin: auto;
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>
           

五.水準垂直居中(方式3)

注意:将元素轉換成表格樣式,再利用表格的樣式來進行居中,設定diplay:table-cell(推薦使用)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	            display: table-cell;
            	vertical-align: middle;
	        }
	        .item{
            	margin:0 auto;
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>
           

六.水準垂直居中(方式4)

注意: 用絕對定位和transfrom,css3變形,有相容性問題

示例代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	            position:relative;
	        }
	        .item{
	        	position: absolute;
            	left: 50%;
            	top: 50%;
            	transform: translate(-50%,-50%);
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>
           

七.水準垂直居中(方式5)

注意:css3中的flex屬性,有相容性問題

示例代碼:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			body{margin: 0;}
	        .box{
	            width: 400px;
	            height: 400px;
	            border:1px solid red;
	            display: flex;
            	justify-content: center;
            	align-items: center;
	        }
	        .item{
	            width: 100px;
	            height: 100px;
	            background: green;
	        }

		</style>
	</head>
	<body>
		<div class="box">
        	<div class="item"></div>
     	</div>
	</body>
</html>