CSS實現自適應分隔線的N種方法

分割線是網頁中比較常見的一類設計了,好比說知乎的更多回答css

image

這裏的自適應是指兩邊的橫線會隨着文字的個數和父級的寬度自適應

偷偷的看了一下知乎的實現,很顯然是用一塊白色背景覆蓋的,加一點背景就露餡了html

image

心想:知乎的前端也不怎麼樣? 可能別人的重點不在這些上面吧前端

下面列舉幾種更好的實現方式,不會露餡的那種git

1.僞元素+transform:translateX(-100%);

主要原理是設置文本居中text-align: center;,而後給定兩個僞元素,分別絕對定位,那麼此時僞元素也是跟隨着水平居中的,設置足夠的寬度,而後把左邊的往左位移100%就能夠了,父級記得超出隱藏。github

具體實現以下wordpress

html結構爲flex

<div class="title">我是分割線</div>

css樣式爲spa

.title{
    position: relative;
    text-align: center;
    overflow: hidden;
    font-size: 14px;
    color: #999;
}
.title::before,.title::after{
    content: '';
    display: inline-block;
    width: 100%;
    height: 1px;
    position: absolute;
    background: #ccc;
    top: 50%;
}
.title::before{
    margin-left: -10px;
    transform: translateX(-100%);
}
.title::after{
    margin-left: 10px;
}


CSS分隔線 (僞元素+transform)
設計

2.僞元素+flex

這個比較好理解了,設置display:flex,而後兩個僞元素分別鋪滿剩餘空間。3d

具體實現以下

html結構爲

<div class="title">我是分割線</div>

css樣式爲

.title{
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #999;
}
.title::before,.title::after{
    content: '';
    flex: 1;
    height: 1px;
    background: #ccc;
}
.title::before{
    margin-right: 10px;
}
.title::after{
    margin-left: 10px;
}


CSS分隔線 (僞元素+flex)

3.僞元素+box-shadow/outline+clip-path

一樣利用text-align: center使文本和僞元素居中,而後生成足夠大的box-shadow或者outline,因爲不支持單個方向,因此用clip-path或者clip裁剪掉

具體實現以下

html結構爲

<div class="title">我是分割線</div>

css樣式爲

.title{
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
}
.title::before,.title::after{
    content: '';
    display: inline-block;
    width: 0;
    height: 1px;
    box-shadow: 0 0 0 9999px #ccc;
    vertical-align: middle;
}
.title::before{
    margin-right: 10px;
    clip-path: polygon(0 0, -9999px 0, -9999px 100%, 0 100%);
}
.title::after{
    margin-left: 10px;
    clip-path: polygon(0 0, 9999px 0, 9999px 100%, 0 100%);
}


CSS分隔線 (僞元素+box-shadow/outline+clip-path)

4.僞元素+right:100%

這個實現須要多一層標籤,外部仍然是text-align: center,內部文本里添加兩個僞元素絕對定位,其中左邊的設置距離右邊100%(相對於文本標籤)便可

具體實現以下

html結構爲

<div class="title">
    <span class="inner">我是分割線</span>
</div>

css樣式爲

.title{
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
}
.inner{
    position: relative;
}
.inner::before,.inner::after{
    position: absolute;
    content: '';
    width: 9999px;
    height: 1px;
    background: #ccc;
    top: 50%;
}
.inner::before{
    right: 100%;
    margin-right: 10px;
}
.inner::after{
    margin-left: 10px;
}


CSS分隔線 (僞元素+right:100%)

5. border+transform

這個思路能夠不用到僞元素,不過須要額外的標籤,給內部文本左右足夠大的1px邊框,此時須要設置line-height:1px,因爲內部總體以及足夠大了(超過父級),能夠使用絕對定位和transform: translateX(-50%)居中

具體實現以下

html結構爲

<div class="title">
    <span class="inner">我是分割線</span>
</div>

css樣式爲

.title{
    position: relative;
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
    padding: .6em 0;/**把高度撐起來**/
}
.inner{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
    line-height: 1px;
    border-left: 9999px solid #ccc;
    border-right: 9999px solid #ccc;
    padding: 0 10px;
}


CSS分隔線 (border+transform)

6.僞元素+border+left/right

這個思路只須要一個僞元素,在文本內部生成一個僞元素,利用足夠大的border和相同的負值(絕對定位+left/right)還原位置

具體實現以下

html結構爲

<div class="title">
    <span class="inner">我是分割線</span>
</div>

css樣式爲

.title{
    text-align: center;
    font-size: 14px;
    color: #999;
    overflow: hidden;
}
.inner{
    position: relative;
    padding: 0 10px;
}
.inner::before{
    content: '';
    position: absolute;
    height: 1px;
    top: 50%;
    border-left: 9999px solid #ccc;
    border-right: 9999px solid #ccc;
    right: -9999px;
    left: -9999px;
}


CSS分隔線 (僞元素+border+left/right)

7.僞元素+table-cell

主要思路爲父級設置display:table,僞元素設置display:table-cell,並設置足夠大的寬度便可

具體實現以下

html結構爲

<div class="title">
    <span class="inner">我是分割線</span>
</div>

css樣式爲

.title{
    display: table;
    font-size: 14px;
    color: #999;
}
.inner{
    display: table-cell;
    white-space: nowrap;
    padding: 0 10px;
}
.title::before,.title::after{
    content: '';
    display: table-cell;
    width: 9999px;
    overflow: hidden;
    background: linear-gradient(#ccc 0,#ccc) center no-repeat;/**這裏用線性漸變生成的,也能夠用其餘方式**/
    background-size: 100% 1px;
}


CSS分隔線 (僞元素+table-cell)

8.fieldset+legend

利用fieldsetlegend標籤組合,能夠自然實現分隔線效果,參考至張鑫旭的這篇文章

具體實現以下

html結構爲

<fieldset class="title">
    <legend class="inner">我是分割線</legend>
</fieldset>

css樣式爲

.title{
    font-size: 14px;
    color: #999;
    border: 0;
    border-top: 1px solid #ccc;
    padding: 0;
}
.inner{
    margin: 0 auto;;
    padding: 0 10px;
}


CSS分隔線 (fieldset+legend)

小結

上面一共列舉了8中方式來實現分隔線的效果,每種方法思路各不相同,重要的是能夠發散本身的想象力,可能這纔是CSS與其餘語言所不一樣的吧~

這裏整理了一下,總體效果以下,可訪問這裏查看,你們在實際項目中可自行選取所須要的方式

image

可能還有其餘方式沒有想到,歡迎你們集思廣益,在下方留言討論

原文連接

相關文章
相關標籤/搜索