css将两个元素水平对齐,兼容IE8

css实现元素水平对齐

css实现水平对齐,如图
《css将两个元素水平对齐,兼容IE8》

有人会说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>
    原文作者:heath_learning
    原文地址: https://segmentfault.com/a/1190000016292754
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞