CSS如何實現自適應分隔線?方法介紹
CSS如何實作自適應分隔線?下面CSS欄位就來跟大家介紹CSS實作自適應分隔線的N種方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。
(推薦教學:CSS教學)
#分割線是網頁中比較常見的一類設計了,比如說知乎的更多答案
這裡的自適應是指兩邊的橫線會隨著文字的數量和父級的寬度自適應
偷偷的看了一下知乎的實現,很顯然是用一塊白色背景覆蓋的,加一點背景就露餡了
心想:知乎的前端也不怎麼樣? 可能別人的重點不在這些上面吧
下面列舉幾種更好的實作方式,不會露餡的那種
1.偽元素transform:translateX(-100% );
主要原理是設定文字居中text-align: center;
,然後給定兩個偽元素,分別絕對定位,那麼此時偽元素也是跟著水平居中的,設定足夠的寬度,然後把左邊的往左位移100%
就可以了,父級記得超出隱藏。
具體實作如下
html
結構為
<div class="title">我是分割线</div>
css
樣式為
.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; }
2.偽元素flex
這個比較好理解了,設定display:flex
,然後兩個偽元素分別鋪滿剩餘空間。
具體實作如下
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; }
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; }
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; }
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; }
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; }
8.fieldset+legend
利用fieldset
和legend
标签组合,可以天然实现分隔线效果,参考至张鑫旭的这篇文章
具体实现如下
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; }
小结
上面一共列举了8中方式来实现分隔线的效果,每种方法思路各不相同,重要的是可以发散自己的想象力,可能这才是CSS
与其他语言所不同的吧~
这里整理了一下,整体效果如下,可访问这里查看,大家在实际项目中可自行选取所需要的方式
可能还有其他方式没有想到,欢迎大家集思广益,在下方留言讨论
更多编程相关知识,请访问:编程入门!!
以上是CSS如何實現自適應分隔線?方法介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

要調整 Bootstrap 中元素大小,可以使用尺寸類,具體包括:調整寬度:.col-、.w-、.mw-調整高度:.h-、.min-h-、.max-h-

如何使用 Bootstrap 按鈕?引入 Bootstrap CSS創建按鈕元素並添加 Bootstrap 按鈕類添加按鈕文本

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

答案:可以使用 Bootstrap 的日期選擇器組件在頁面中查看日期。步驟:引入 Bootstrap 框架。在 HTML 中創建日期選擇器輸入框。 Bootstrap 將自動為選擇器添加樣式。使用 JavaScript 獲取選定的日期。

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。
