利用css实现水平垂直居中

HTML:

<div></div>

在这里插入图片描述

利用定位

	div{
			height: 100px;
			width: 100px;
			border: 1px solid;
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%,-50%);
		}

利用flex

		body{
			margin: 0;
			height: 100vh;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		div{
			height: 100px;
			width: 100px;
			border: 1px solid;
		}

利用定位加上margin

	div{
			height: 100px;
			width: 100px;
			border: 1px solid;
			position: absolute;
			left: 0;
			right: 0;
			bottom: 0;
			top: 0;
			margin: auto;
		}

利用margin

body{
			margin: 0;
		}
		div{
			box-sizing: border-box;
			height: 100px;
			width: 100px;
			border: 1px solid;
			margin: calc(50vh - 50px) auto;
		}
发布了28 篇原创文章 · 获赞 2 · 访问量 2904

猜你喜欢

转载自blog.csdn.net/weixin_45725020/article/details/103963234