css 垂直 水平居中的方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			.div{
				width: 600px;
				height: 600px;
				background: darkred;
				position: relative;
			}
			.div div{
				width: 200px;
				height: 200px;
				background: #000;
				position: absolute;
			    top: 50%;
			    left: 50%;
			    -webkit-transform: translate(-50%, -50%);
			    -moz-transform: translate(-50%, -50%);
			    transform: translate(-50%, -50%);
			}
			.div2{
				height: 400px;
				line-height: 400px;
				width: 400px;
				background: #000;
				display: flex;
			    justify-content: center;
			    align-items: center;
			    text-align: center;
			}
			.div3{
				display: inline-block;
				width: 100px;
				height: 100px;
				vertical-align: middle;
				background: #fff;
			}
		</style>
	</head>                                                                                
	<body>
		<div class="div">
			<div class="div1">
				我在中间
			</div>
		</div>
		
		<div class="div2">
			<div class="div3">
				我在中间
			</div>
		</div>
		
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/lianxin19900610/article/details/82461207