文章出處

前面的話

  兩端對齊在導航Nav的制作中非常常用。本文將詳細介紹CSS兩端對齊的4種實現方式

 

flex

  彈性盒模型flex作為強大的彈性布局方式,可以hold住大部分的布局效果,當然也包括兩端對齊。可以使用主軸對齊justify-content的兩端對齊屬性space-between

justify-content: space-between;

  如果要考慮flex三個版本的兼容,則使用如下代碼

  [注意]IE9-瀏覽器不支持

.justify-content_flex-justify{
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    -webkit-justify-content: space-between;
    justify-content: space-between;
}


<style>
body{margin: 0;}    
ul{margin: 0;padding: 0;list-style: none;}
.list{width: 200px;overflow: hidden;border: 1px solid gray;background-color: lightgreen;line-height: 30px;}
.in{background-color: lightblue;padding: 0 10px;}
.display_flex{display: -webkit-box;display: -ms-flexbox;display: -webkit-flex;display: flex;}
.display_flex > *{display: block;}
.justify-content_flex-justify{-webkit-box-pack: justify;-ms-flex-pack: justify;-webkit-justify-content: space-between;justify-content: space-between;}
</style>
<ul class="list display_flex justify-content_flex-justify">
    <li class="in">內容</li>
    <li class="in">樣式</li>
    <li class="in">行為</li>
</ul>

 

text-align

  水平對齊text-align本身就有一個屬性值是兩端對齊justify。但是,要注意的是,使用它實現兩端對齊,需要注意在元素之間添加空白符(包括空格、換行符、制表符)才起作用。由于HTML結構中,<li>元素之間存在換行,所以不需要額外添加空白符

  但僅僅是這樣,元素也無法實現兩端對齊效果

  元素必須占滿一行才行,如下所示。占滿一行的元素可以實現兩端對齊,沒有占滿的則無法實現

【text-align-last】

  顯然,上面的情況都不符合要求,這時就需要使用屬性text-align-last,該屬性用來規定如何對齊文本的最后一行

  于是把text-align屬性替換成text-align-last。但是,要兼容IE瀏覽器需要同時設置text-align:justify

  [注意]safari瀏覽器、IOS、androis4.4-瀏覽器不支持

<style>
body{margin: 0;}    
ul{margin: 0;padding: 0;list-style: none;}
.list{width: 200px;overflow: hidden;border: 1px solid gray;background-color: lightgreen;line-height: 30px;text-align: justify;text-align-last: justify;}
.in{background-color: lightblue;padding: 0 10px;display:inline-block;}
</style>
<ul class="list ">
    <li class="in">內容</li>
    <li class="in">樣式</li>
    <li class="in">行為</li>  
</ul>

【after偽元素】

  使用text-align-last可以實現兩端對齊的效果,但是兼容性并不好。通過給父元素設置偽元素:after,并為偽元素設置inline-block,并設置寬度100%,相當于偽元素:after被擠到第二行。從而使原來的元素占滿了第一行,觸發了兩端對齊的效果

  這里要注意的是,因為空白會被解析為換行,所以可以通過設置父元素的高度height,并溢出隱藏,來解決多余的換行問題

<style>
body{margin: 0;}    
ul{margin: 0;padding: 0;list-style: none;}
.list{width: 200px;height: 30px;overflow: hidden;border: 1px solid gray;background-color: lightgreen;line-height: 30px;text-align: justify;}
.in{background-color: lightblue;padding: 0 10px;display:inline-block;}
.list:after{content:"";width:100%;display:inline-block;}
</style>
<ul class="list ">
    <li class="in">內容</li>
    <li class="in">樣式</li>
    <li class="in">行為</li>  
</ul>

 

column

  使用多列布局column也可以實現類似的效果。column-count定義了元素的列數,例子中有3個子元素,所以定義為3列。特別要注意的是,這時需要把子元素設置為block元素才會生效

  [注意]IE9-瀏覽器不支持

<style>
body{margin: 0;}    
ul{margin: 0;padding: 0;list-style: none;}
.list{width: 200px;overflow: hidden;border: 1px solid gray;background-color: lightgreen;line-height: 30px;text-align: center;}
.col3{-webkit-column-count:3;-moz-column-count:3;column-count:3;}
.in{background-color: lightblue;padding: 0 10px;display:block;}
</style>
<ul class="list col3">
    <li class="in">內容</li>
    <li class="in">樣式</li>
    <li class="in">行為</li>  
</ul>

  如果子元素之間需要使用豎線,且豎線高度與子元素高度相同時,使用column-rule可方便的實現需求

<style>
body{margin: 0;}    
ul{margin: 0;padding: 0;list-style: none;}
.list{width: 200px;overflow: hidden;border: 1px solid gray;background-color: lightgreen;line-height: 30px;text-align: center;}
.col3{-webkit-column-count:3;-moz-column-count:3;column-count:3;}
.col-rule{-webkit-column-rule: 1px solid black;-moz-column-rule: 1px solid black;column-rule: 1px solid black;}
.in{background-color: lightblue;padding: 0 10px;display:block;}
</style>
<ul class="list col3 col-rule">
    <li class="in">內容</li>
    <li class="in">樣式</li>
    <li class="in">行為</li>  
</ul>

 

grid

  柵格布局使用justify-content的兩端對齊屬性space-between

  [注意]IE10-瀏覽器不支持

<style>
body{margin: 0;}
ul{margin: 0;padding: 0;list-style: none;}
.list{width: 200px;overflow: hidden;border: 1px solid gray;background-color: lightgreen;line-height: 30px;display:grid;justify-content:space-between;grid-auto-flow:column;}
.in{background-color: lightblue;padding: 0 10px;}
</style>    
<ul class="list">
    <li class="in">內容</li>
    <li class="in">樣式</li>
    <li class="in">行為</li>
</ul>

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()