居中
方法1:父容器开启flex 布局,随后使用justify-content 和align-items 实现水平垂直居中
方法2:父容器开启flex 布局,随后子元素margin: auto
vs 使用不使用flex的方法
块元素
? ? ? ? 水平 :margin:0 auto ;
? ? ? ? 垂直:margin-top(父元素H-子元素H)/2)px
行内,行内块:
? ? ? ? 水平:text-align:center;
? ? ? ? 垂直:height = line-height,? vertical-align:middle;若想绝对居中的话,父元素font-size:0,再重置子元素的font-size
方法1:父容器开启flex 布局,随后使用justify-content 和align-items 实现水平垂直居中
.outer1{
width: 800px;
height: 500px;
background-color: antiquewhite;
display: flex;
align-items: center;
justify-content: center;
}
.inner1{
width: 300px;
height: 150px;
background-color: aqua;
}
<div class="outer1">
<div class="inner1">
haha
</div>
</div>
方法2:父容器开启flex 布局,随后子元素margin: auto
.outer1{
width: 800px;
height: 500px;
background-color: antiquewhite;
display: flex;
}
.inner1{
width: 300px;
height: 150px;
background-color: aqua;
margin:auto;
}