css將兩個元素水平對齊,兼容IE8

css實現元素水平對齊

css實現水平對齊,如圖
圖片描述css

有人會說css實現這種水平對齊要兼容ie8還不簡單嗎?使用float: left,或者display: inline-block,不就能夠了嗎?是的,最經常使用的最簡單方式就是上面這兩種,但還有一種方式也能夠實現,那就是使用display: table-cell;spa

示例代碼

<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>
相關文章
相關標籤/搜索