关于HTML页面内容水平垂直居中的6种方法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
		   // 知不知道box-center宽高都可以 
		   // 第一种
		   .box {
     
     
				width: 200px;
				height: 200px;
				position: relative;
				background-color: red;
			}
			
			.box-center {
     
     
				position: absolute;
				left: 50%;
				top: 50%;
				width: 100px;
				height: 100px;
				transform: translate(-50%, -50%);
				background-color: blue;
			} 
			// 第二种
			.box {
     
     
				height: 200px;
				width: 200px;
				text-align: center;
				background-color: red;
			}
			
			.box::after,
			.box-center {
     
     
				display: inline-block;
				vertical-align: middle;
			}
			
			.box-center {
     
     
				background-color: blue;
				height: 100px;
				width: 100px;
			}
			
			.box::after {
     
     
				content: '';
				height: 100%;
			}
			//第三种
			.box {
     
     
				height: 200px;
				width: 200px;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: red;
			}
			
			.box-center {
     
     
				width: 100px;
				height: 100px;
				background-color: yellow;
			}
			
			// 必须知道box-center宽高
			// 第一种
			.box {
     
     
				width: 200px;
				height: 200px;
				position: relative;
				background-color: red;
			}
			
			.box-center {
     
     
				width: 100px;
				height: 100px;
				position: absolute;
				left: 50%;
				top: 50%;
				margin-top: -50px;
				margin-left: -50px;
				background-color: blue;
			}
			// 第二种
			.box {
     
     
				width: 200px;
				height: 200px;
				position: relative;
				background-color: red;
			}
			
			.box-center {
     
     
				width: 100px;
				height: 100px;
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				margin: auto;
				background-color: blue;
			} 
		</style>
	</head>
	<body>
		<div class="box">
			<div class="box-center">1111111111</div>
		</div>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_44997147/article/details/104350335
今日推荐