CSS定位元素居中显示

一、利用marginios

div {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -50px;
}
分析:orm

top: 50%; left: 50%;让元素的左上在父元素中垂直水平居中
margin-top: -50px; margin-left: -50px;让元素向上向右偏移自身一半的距离
二、利用translateget

div {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
分析:it

top: 50%; left: 50%;让元素的左上在父元素中垂直水平居中
transform: translate(-50%, -50%);让元素向上向右移动自身一半的距离
三、四个方位所有为0,用margin定位io

div {
    width: 100px;
    height: 100px;
    background-color: skyblue;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right : 0;
    margin: auto;
}
分析:form

四个方位所有为0时,相互抵消,盒子会在初始位置显示
margin: auto;让盒子垂直水平居中class

 <div class="bottom_soft_link">
<a target="_blank" class="bottom_soft_link_ios" href="xxx.com" title="客户端苹果手机APP下载">iOS下载</a>
<a target="_blank" class="bottom_soft_link_ios" href="http://jinan.changtu.com/" title="济南长途汽车总站电话">济南长途汽车总站电话</a>
</div>transform

相关文章
相关标签/搜索