inline-level
<html>
<body>
<div class="parent-1">
<span>inline-level</span>
</div>
</body>
</html>
<style>
.parent-1 {
text-align: center;
}
</style>
块级元素一般居中
<html>
<body>
<div class="parent-2">
<div class="child-2">块级元素一般居中</span>
</div>
</body>
</html>
<style>
.child-2 {
margin: 0 auto;
width: 30%;
background: red;
}
</style>
子元素含float
<html>
<body>
<div class="parent-3">
<div class="child-3">子元素含float</div>
</div>
</body>
</html>
<style>
.parent-3 {
width: fit-content;
margin: 0 auto;
background: #fdcb6e;
}
.child-3 {
width: 100px;
float: right;
background: #ffeaa7;
}
</style>
flex弹性盒子
<html>
<body>
<div class="parent-4">
<div class="child-4">flex弹性盒子</div>
</div>
</body>
</html>
<style>
.parent-4 {
background: #fdcb6e;
display: flex;
justify-content: center;
}
.child-4 {
width: 100px;
background: #ff7675;
}
</style>
绝对定位-transform
<html>
<body>
<div class="parent-5-1">
<div class="child-5-1">绝对定位-transform</div>
</div>
</body>
</html>
<style>
.parent-5-1 {
position: relative;
width: 100%;
height: 50px;
background: #fdcb6e;
}
.child-5-1 {
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 150px;
background: #ffeaa7;
}
</style>
绝对定位-left:50%
<html>
<body>
<div class="parent-5-2">
<div class="child-5-2">绝对定位-left:50%</div>
</div>
</body>
</html>
<style>
.parent-5-2 {
position: relative;
width: 100%;
height: 50px;
background: #fdcb6e;
}
.child-5-2 {
position: absolute;
left: 50%;
margin-left: calc(-0.5 * 150px);
width: 150px;
background: #ffeaa7;
}
</style>
绝对定位-left/right:0
<html>
<body>
<div class="parent-5-3">
<div class="child-5-3">绝对定位-left/right:0</div>
</div>
</body>
</html>
<style>
.parent-5-3 {
position: relative;
width: 100%;
height: 50px;
background: #fdcb6e;
}
.child-5-3 {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
width: 150px;
background: #ffeaa7;
}
</style>
行内元素
<html>
<body>
<div class="parent-6">
<span class="child-6">行内元素</span>
</div>
</body>
</html>
<style>
.parent-6 {
height: 50px;
background: #fdcb6e;
}
.child-6 {
line-height: 50px;
background: #ffeaa7;
}
</style>
行内块级元素
<html>
<body>
<div class="parent-7">
<div class="child-7">行内块级元素</div>
</div>
</body>
</html>
<style>
.parent-7 {
width: 100%;
height: 50px;
background: #fdcb6e;
}
.child-7 {
width: 150px;
background: #ffeaa7;
}
.parent-7::after, .child-7 {
display: inline-block;
vertical-align: middle;
}
.parent-7::after {
content: '';
height: 100%;
}
</style>
table
<html>
<body>
<div class="parent-8">
<div class="child-8">table</div>
</div>
</body>
</html>
<style>
.parent-8 {
width: 100%;
height: 50px;
background: #fdcb6e;
display: table;
}
.child-8 {
display: table-cell;
vertical-align: middle;
height: 30px;
width: 150px;
background: #ffeaa7;
}
</style>
flex
<html>
<body>
<div class="parent-9">
<div class="child-9">flex</div>
</div>
</body>
</html>
<style>
.parent-9 {
width: 100%;
height: 50px;
background: #fdcb6e;
display: flex;
align-items: center;
}
.child-9 {
width: 150px;
background: #ffeaa7;
}
</style>
transform
<html>
<body>
<div class="parent-10">
<div class="child-10">transform</div>
</div>
</body>
</html>
<style>
.parent-10 {
width: 100%;
height: 50px;
background: #fdcb6e;
position: relative;
}
.child-10 {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 150px;
background: #ffeaa7;
}
</style>
top:50%
<html>
<body>
<div class="parent-11">
<div class="child-11">top:50%</div>
</div>
</body>
</html>
<style>
.parent-11 {
width: 100%;
height: 50px;
background: #fdcb6e;
position: relative;
}
.child-11 {
position: absolute;
top: 50%;
margin-top: calc(-0.5 * 30px);
height: 30px;
width: 150px;
background: #ffeaa7;
}
</style>
top/bottom
<html>
<body>
<div class="parent-12">
<div class="child-12">top/bottom</div>
</div>
</body>
</html>
<style>
.parent-12 {
width: 100%;
height: 50px;
background: #fdcb6e;
position: relative;
}
.child-12 {
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
height: 30px;
width: 150px;
background: #ffeaa7;
}
</style>