css将两个元素水平对齐的方法(兼容IE8)

2019-10-24,

css实现水平对齐,如图

有人会说css实现这种水平对齐要兼容ie8还不简单吗?使用float: left,或者display: inline-block,不就可以了吗?是的,最常用的最简单方式就是上面这两种,但还有一种方式也可以实现,那就是使用display: table-cell;

示例代码

<style type="text/css">
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    .container{
        width: 1000px;
        height: 1000px;
        margin: 100px;
        background-color: #f60;
    }
    .left{
       /*关键点在于将两个元素设置为display:table-cell*/
        display: table-cell;
        vertical-align: top;
        width: 20%;
        min-width: 200px;
        height: 400px;
        background-color: #ccc;
    }
    .right{
        display: table-cell;
        vertical-align: top;
        /*即使宽度设为2000px,元素的内容还是可以正常显示*/
        width: 2000px;
        height: 600px;
        background-color: #f10;
    }
</style>
<div class="container">
    <div class="left">left</div>
    <div class="right">right</div>
</div>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼。

《css将两个元素水平对齐的方法(兼容IE8).doc》

下载本文的Word格式文档,以方便收藏与打印。