1.子元素是行内元素
父元素设置text-align:center; line-height设置为父元素的高度
<style>
.container {
text-align: center;
line-height: 200px;
height: 200px;
}
</style>
<body>
<div class="container">
<span>666</span>
</div>
</body>
2.子元素是块级元素
<div class="container">
<div class="inner"></div>
</div>
(1).定位+margin,父相子绝
<style>
.container {
position: relative;
width: 100%;
height: 1000px;
}
.inner{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 100px;
height: 100px;
background-color: blue;
}
</style>
(2).定位+transform,父相子绝
<style>
.container {
position: relative;
width: 100%;
height: 1000px;
}
.inner{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
background-color: pink;
}
</style>
(3).flex布局
<style>
.container {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 1000px;
}
.inner{
width: 100px;
height: 100px;
background-color: pink;
}
</style>