0、原始代码
<style>.box{
border: 1px solid red;
}</style>
<body>
<div class="box">
<img src="./icon.jpg" alt="">
<span>我是头像</span>
</div>
</body>
1、横排居中
1.1 vertical-align: middle
<style>
.box{
border: 1px solid red;
}
.box img{
vertical-align: middle;
}
.box span{
background-color: green;
}
1.2 display: flex
<style>
.box{
border: 1px solid red;
display: flex;
align-items: center;
}
.box span{
background-color: green;
}
2、水平居中
2.1 text-align: center;
<style>
.box{
border: 1px solid red;
text-align: center;
}
.box img{
vertical-align: middle;
}
.box span{
background-color: green;
}
2.2 display: flex;
<style>
.box{
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
.box span{
background-color: green;
}
3、垂直居中
3.1 display: flex;
<style>
.box{
border: 1px solid red;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.box span{
background-color: green;
}
3.2 margin: 0 auto;
<style>
.box{
border: 1px solid red;
text-align: center;
}
.box img {
margin: 0 auto;
display: block;
}
.box span{
background-color: green;
}