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

CSS元素在父元素居中显示总结

程序员文章站 2022-05-31 20:57:17
...

一.水平居中(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>