42個來自《 CSS世界》中的實用技巧

2021-02-13 web前端開發

來自 | 《css世界》一書


1、清除浮動主要為子元素浮動(float)之後,父元素無法撐起高度和寬度。

<!-- html --><div>    <img src="demo.gif"></div>
<!-- css --><style> img { float: left; } /* 清除浮動 */ .clear::after { content: ""; display: block; clear: both; }</style>

2、文字少時居中,多時靠左

但是要注意,當p的內容為英文單詞組成的時候,如果單詞過長,而不是「 pppppppppppppppppppppppppppppp」這樣的一次,會被視為一個單位而造成超過div的尺寸。

如果你想要英文字符也有中文字符的效果的話,在p使用「 word-break:break-all」。

<!-- html --><div>    <p></p></div>
<!-- css --><style> .box { text-align: center; } .content { display: inline-block; text-align: left; }</style>

3、凹凸人

目的在於製造一個凹或凸的形狀,利用了「 2」中英文單詞不換行的特性

<!-- html --><div class='ao'></div>
<!-- CSS --><style> .ao { display: inline-block; width: 0; } .ao::before { content: "love 你 love"; outline: 2px solid #000; color: #fff; }</style>

4、讓padding,border不影響盒模型的大小

相信這點大部分人都知道,但是有一些奇怪的行為,比如說width <content + padding會怎樣?實際上當padding + border> width時,元素的渲染大小(Chrome下)為padding + border;而padding + border <width時,允許剩餘空間分配給content。

<!-- html --><div></div>
<!-- CSS --><style> div { box-sizing: border-box; }</style>

5、身高:100%佔屏效果

<!-- html --><div></div>
<!-- CSS方法一 --><style> html,body { height: 100%; } div { height: 100% }</style><!-- CSS方法二 --><style> div { position: absolute; height: 100%; }</style>

6、任意高度元素展開

缺點是,如果高度太大會造成展開過快和重複中斷,那麼這個足夠大的值應該適當。

<!-- html --><div></div>
<!-- CSS --><style> div { max-height: 0; overflow: hidden; transition: max-height .25s; } div.active { max-height: 666px; /* 需要足夠大的值 */ }</style>

7、優雅的圖片未加載或加載失敗效果

需要注意的是,圖片顯示完成後,img會成為「替換元素」,而替換元素是無法設置偽元素的,因為內容被圖片替換掉了;還需要注意attr裡面的變量不能加雙引號。

<!-- html --><div>    <img src="demo.gif" alt="lululu"></div>
<!-- CSS --><style> div { width: 100px; height: 100px; overflow: hidden; } img { display: inline-block; width: 100%; height: 100%; position: relative; } img::after { /* 生成 alt 信息 */ content: attr(alt); /* 尺寸和定位 */ position: absolute; left: 0;bottom: 0;right: 0; /* 顏色 */ background-color: rgba(0,0,0,.5); /* alt 信息隱藏 */ transform: translateY(100%); /* 過渡動畫效果 */ transition: transform .2s; } img:hover::after { /* alt 信息顯示 */ transform: translateY(0); } </style>

8、CSS的懸浮圖片替換效果

需要注意的是,如果快捷保存圖片,保存的是src內的圖片,而不是替換之後的。

<!-- html --><img src="demo.gif">
<!-- CSS --><style> img:hover { content: url(amazing.gif); }</style>

9、利於seo的「替換元素」標題logo

用h1的原因主要是因為seo,語義化的問題。

<!-- html --><h1>Weismann's blog</h1>
<!-- CSS --><style> h1 { content: url(logo.gif); }</style>

10、高兼容,自動等寬,底部對齊的柱狀圖

需要注意的是,第一個i不能換行,換行後會產生後移的結果。

<!-- html --><div><i></i>    <i></i>    <i></i>    <i></i></div>
<!-- CSS --><style> .box { width: 256px; height: 256px; text-align: justify; } .box:before { content: ""; display: inline-block; height: 100%; } .box:after { content: ""; display: inline-block; width: 100%; } .bar { display: inline-block; width: 20px; /* height自定 */ } </style>

11、高兼容性的加載效果

在IE6-IE9下是...,其他都是動態的;使用點的目的是語義化和低版本瀏覽器的兼容。

<!-- html -->正在加載中<dot>...</dot>
<!-- CSS --> <style> dot { display: inline-block; height: 1em; line-height: 1; text-align: left; vertical-align: -.25em; overflow: hidden; } dot::before { display: block; content: '...\A..\A.'; white-space: pre-wrap; animation: dot 3s infinite step-start both; } @keyframes dot { 33% { transform: translateY(-2em); } 66% { transform: translateY(-1em); } }</style>

12、擴大點擊區域

第一種主要利用了內聯元素的填充只會影響外觀和不影響布局的特點;第二種針對其他屬性會改變背景圖定位的一種方式。

<!-- html --><a href="">demo</a>
<!-- CSS1 --><style> a { padding: 20px 0; }</style><!-- CSS2 --><style> a { border: 11px solid transparent; }</style>

13、不使用偽元素的「三道槓」和「圓點」效果

<!-- html --><i></i>
<!-- CSS三道槓 --><style> .icon { display: inline-block; width: 140px; height: 10px; padding: 35px 0; border-top: 10px solid; border-bottom: 10px solid; background-color: currentColor; background-clip: content-box; }</style><!-- CSS三道槓2 --><style> .icon { width: 120px; height: 20px; border-top: 60px double; border-bottom: 20px solid; }</style><!-- CSS圓點 --><style> .icon { display: inline-block; width: 100px; height: 100px; padding: 10px; border: 10px solid; border-radius: 50%; background-color: currentColor; background-clip: content-box; }</style>

14、導航欄消除右邊多餘的尺寸

利用保證金來改變尺寸,需要注意,改變尺寸的元素水平方向的尺寸不能是確定的。

<!-- html --><div>    <ul>        <li></li>        <li></li>        <li></li>    </ul></div>
<!-- CSS --><style> div { width: 380px; } ul { margin-right: -20px; } ul > li { float: left; width: 100px; margin-right: 20px; } </style>

15、正確的滾動底部留白方式

如果使用padding留白,在Firefox和IE不會顯示。

<!-- html --><div>    <img src="demo.gif"></div>
<!-- CSS --> <style> .box { height:200px; overflow:auto; } .box>img { margin: 50px 0; }</style>

16、高兼容的多欄等高

注意container高度不能是確定值,缺點是如果在內部使用錨點定位會出現問題。

<!-- html --><div>    <div id="colLeft">        <h4>正方觀點</h4>        <p>觀點1</p>        <p>觀點1</p>    </div>    <div id="colRight">        <h4>反方觀點</h4>        <p>觀點1</p>    </div></div>
<!-- CSS --><style> .container { overflow: hidden; } .column-left, .column-right { margin-bottom: -9999px; padding-bottom: 9999px; width: 50%; float: left; } .column-left { background-color: #34538b; } .column-right { background-color: #cd0000; }</style>

17、正確的塊級元素右對齊

自動值對於保證金是佔用剩餘的空間。

<!-- html --><div>demo</div>
<!-- CSS --><style> div { width: 100px; margin-left: auto; }</style>

18、圖片上傳增加框

此技巧主要說明border的顏色默認是繼承自color的。

<!-- html --><div></div>
<!-- CSS --><style> .add { display: inline-block; width: 76px; height: 76px; color: #ccc; border: 2px dashed; text-indent: -12em; transition: color .25s; position: relative; overflow: hidden; } .add:hover { color: #34538b; } .add::before, .add::after { content: ''; position: absolute; top: 50%; left: 50%; } .add::before { width: 20px; border-top: 4px solid; margin: -2px 0 0 -10px; } .add::after { height: 20px; border-left: 4px solid; margin: -10px 0 0 -2px; }</style>

19、不影響背景圖片位置設置邊距

和增加點擊區域第二種方式一樣

<!-- html --><div></div>
<!-- CSS --><style> .box { display: inline-block; width: 100px; height: 100px; border-right: 50px solid transparent; background-position: 100% 50%; }</style>

20、border製作梯形,各種三角形

<!-- html --><div></div>
<!-- CSS梯形 --><style> div { width: 10px; height: 10px; border: 10px solid; border-color: #f30 transparent transparent; }</style><!-- CSS三角 --><style> div { width: 0; border-width: 10px 20px; border-style: solid; border-color: #f30 transparent transparent; } </style><!-- CSS直角三角 --><style> div { width: 0; border-width: 10px 20px; border-style: solid; border-color: #f30 #f30 transparent transparent; } </style>

21、高兼容雙欄,一邊等寬一邊自適應,等高布局

缺點是邊框不支持百分比,最多2-3欄。

<!-- html --><div>    <nav>        <div>123</div>        <div>123</div>        <div>123</div>    </nav>    <section>        <div>1234</div>    </section></div>
<!-- CSS --><style> .box { border-left: 150px solid #333; background-color: #f0f3f9; } .box::after { content: ""; display: block; clear: both; } .box > nav { width: 150px; margin-left: -150px; float: left; } .box > section { overflow: hidden; }</style>

22、內聯元素「近似」垂直居中

而為什麼說「近似」,一句話說清楚,請看開頭

<!-- CSS --><style>    span {        line-height: 24px;    }</style>

多行內容「近似」垂直居中

<!-- html --><div>    <div>基於行高實現的...</div></div> 
<!-- CSS --><style> .box { width: 120px; line-height: 120px; background-color: #f0f3f9; } .content { display: inline-block; line-height: 20px; margin: 0 20px; vertical-align: middle; } </style>

23、容器內圖片的垂直方向間隙問題

產生的問題和「幽靈空白子系統」和x-height有關,你可以嘗試在img前加入x字符觀察一下。

<!-- html --><div>    <img src="demo.gif"></div>
<!-- CSS --><style> .box { width: 280px; outline: 1px solid #aaa; text-align: center; /* 解決方案1 */ font-size: 0; /* 解決方案2 */ line-leight: 0; } .box > img { height: 96px; /* 解決方案3 */ display: block; } </style>

24、圖標文字對齊

ex代表的是x-height的高度,根據x字形的不同(如font-family)而不同。

<div class="box">    <p>        <i class="icon icon-demo"></i>拉拉    </p></div>
<style> .box { line-height: 20px; } p { font-size: 40px; } .icon { display: inline-block; width: 20px; height: 20px; white-space: nowrap; letter-spacing: -1em; text-indent: -999em; } .icon::before { content: '\3000'; } .icon-demo { background: url(demo.png) no-repeat center; }</style>
<p>文字 <img src="delete.png"></p>
<style> p { font-size: 14px; } p > img { width: 16px; height: 16px; vertical-align: .6ex; position: relative; top: 8px; }</style>

25、永遠居中的彈框

特點是內容和瀏覽器尺寸變化都是自動變換大小和位置,可以通過偽元素的高度控制上下位置。

<div class="container">    <div class="dialog">demo</dialog></div>
<style> .container { position: fixed; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0,0,0,.5); text-align: center; font-size: 0; white-space: nowrap; overflow: auto; } .container::after { content: ''; display: inline-block; height: 100%; vertical-align: middle; } .dialog { display: inline-block; vertical-align: middle; text-align: left; font-size: 14px; white-space: normal; padding: 10px 14px; border: 1px solid #000; border-radius: 4px; background: #fff; }</style>

26、文字環繞圖片

float的真正用途。

<!-- html --><div>    <div>        <img src="demo.gif">    </div>    <p>demo,demo,demo,demo,demo,demo,demo</p></div>
<!-- CSS --><style> .box { width: 100px; } img { float: left; width: 40px; height: 40px; }</style>

27、利用溢出:隱藏自定義滾動條

實際上overflow:hidden是可以滾動的,可以通過錨點,focus,scrollTop滾動。滾動條的實現請自行發揮。

28、通過標籤實現的選項卡效果

與錨點不同的是不會觸發由內到外(多層滾動造成的某種事件冒泡的效果)的頁面跳動(元素上邊與分段上邊對齊),還支持Tab選項的效果;缺點是需要js支持效果。

<div class="box">    <div class="list"><input id="one">1</div>    <div class="list"><input id="two">2</div>    <div class="list"><input id="three">3</div>    <div class="list"><input id="four">4</div></div><div class="link">    <label class="click" for="one">1</label>    <label class="click" for="two">2</label>    <label class="click" for="three">3</label>    <label class="click" for="four">4</label></div> 
<style> .box { height: 10em; border: 1px solid #ddd; overflow: hidden; } .list { height: 100%; background: #ddd; position: relative; } .list > input { position: absolute; top:0; height: 100%; width: 0; border:0; padding: 0; margin: 0; } </style>

29、「包含塊」的絕對定位元素「一柱擎天」問題。
<!-- html --><div>    <div>拉拉</div></div>
<!-- CSS --><style> .father { position: relative; width: 20px; height: 20px; } .son { position: absolute; /* 解決方案 */ white-space: nowrap; }</style>

30、「無依賴絕對定位」的表單驗證應用

在一個元素上如果單用(父元素的位置屬性均是替換)「 position:absolute」,事實上元素將原地不動,最終會產生BFC。

<!-- html --><div>    <label><span>*</span>郵箱</label>    <div>        <input type="email">        <span>郵箱格式不準確(示意)</span>    </div></div><div>    ...</div>
<!-- CSS --> <style> .group { width: 300px; } .label { float: left; } .remark { position: absolute; }</style>

31、主體頁面側邊欄

利用text-align和fixed的組合;高度放置0和overflow隱藏目的是為了不影響主體的體驗,而之所以絕對定位元素沒有被隱藏的原因是「如果overflow不是定位元素,同時絕對定位元素和overflow容器同時也沒有定位元素,則溢出無法對絕對定位元素進行剪裁。」 —《 CSS世界》。

<div class="alignright">    <span class="follow"></span></div>
<style> .alignright { height: 0; text-align: right; overflow: hidden; background: blue; } .alignright:before { content: "\2002"; } .follow { position: fixed; bottom: 100px; z-index: 1; width: 10px; height: 10px; border: 1px solid #000; } </style>

32、不通過寬度和高度設置預定全佔用

利用top和bottom或left和right同時設置的時候會觸發流體特性的特點;與通過「 top:0; left:0; width:100%; height:100%;」,在設置邊距,邊框, padding的時候不會溢出到合併的外面(就算你想到box-sizing,那margin呢?);而之所以用span的原因是想說明絕對定位放置元素的顯示置為塊。

<!-- html --><span></span>
<!-- CSS --><style> span { position: absolute; top:0; left:0; right:0; bottom: 0; }</style>

33、margin:自動水平垂直居中

<div></div>
<style> div { width: 300px; height: 200px; position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; }</style>

34、紙張卷邊陰影

主要利用「位置:相對;z-index:0;」創建並合併到z-index的負值將陰影放置在「 contaniner」和「 page」之間。

你可以嘗試將關鍵CSS去掉查看效果。

<div class="container">    <div class="page">        <h4>demo</h4>        <p>demo</p>    </div></div>
<style> .container { background-color: #666; height: 1000px; position: relative; z-index: 0; } .page { width: 600px; background-color: #f4f39e; background: linear-gradient(to bottom, #f4f39e, #f5da41 60%, #fe6); box-shadow: 0 2px 10px 1px rgba(0, 0, 0, .2); text-shadow: 0 1px 0 #f6ef97; position: relative; left: 200px; top: 200px; } .page:before { transform: skew(-15deg) rotate(-5deg); transform-origin: left bottom; left: 0; } .page:after { transform: skew(15deg) rotate(5deg); transform-origin: right bottom; right: 0; } .page:before, .page:after { width: 90%; height: 20%; content: ""; box-shadow: 0 8px 16px rgba(0, 0, 0, .3); position: absolute; bottom: 0; z-index: -1; }</style>

35、隱藏文字

說這個主要是為了說明,Chrome瀏覽器如果字體設置12px以下的大小(新版本已經不限制了),會被自動處理成12px,但是有一個值除外,0。

<style>    p {        font-size: 0;    }</style>

36、解決text-decoration下劃線和文本重疊

因為是內聯元素,所以完全不用擔心會影響元素高度的問題。

<style>    a {        text-decoration: none;        border-bottom: 1px solid;        padding-bottom: 5px;    }</style>

37、自動將輸入的小寫字母轉換大寫

<!-- CSS --><style>    input {        text-transform: uppercase;    }</style>

38、價格場景下的首個符號選擇器

特點是可以讓html結構活躍乾淨。

<!-- html --><p>¥399</p>
<!-- CSS --><style> .price:first-letter { ... }</style>

39、元素隱藏同時資源不加載

後續可通過script.innerHTML訪問。

<!-- html --><script type="text/html">    <img src="1.jpg"></script> 

40、頭像裁剪矩形鏤空效果

主要利用輪廓。

<!-- html --><div>    <div id="cropArea"></div>    <img src="demo.gif"></div>
<!-- CSS --><style> .crop { width: 200px; height: 200px; overflow: hidden; position: relative; } .crop > .crop-area { position: absolute; top:0; height: 0; width: 80px; height: 80px; outline: 200px solid rgba(0,0,0,.5); cursor: move; } </style>

41、自定義光標

需要注意IE只支持cur文件。

<!-- CSS --><style>    .cursor-demo {        cursor: url(demo.cur);    }</style>

42、修改水平流到垂直流

兼容到IE7;此應用涉及到一體的東西,所有水平流的特性都可以應用到垂直流中(稱為水平居中變成了垂直居中)。

<!-- CSS --><style>    .verticle-mode{        writing-mode: tb-rl;        -webkit-writing-mode: vertical-rl;        writing-mode: vertical-rl;    }</style>

推薦閱讀

相關焦點

  • CSS文字處理實用小技巧總結
    因此,在這裡簡單的整理一些CSS開發技巧,希望能讓你寫出耳目一新、容易理解、舒服自然的代碼。CSS實用技巧第一講:文字處理本小結主要是圍繞css對文字處理的技巧,有興趣的小夥伴可以收藏一下。文本對齊方式CSS最常用的對齊方式,居中對齊、左對齊(默認)、右對齊,而且實現起來也是非常的簡單。
  • 巧用css圓角實現有點意思的加載動畫
    作為一名前端工程師, 需要對css技巧有充分的研究和了解, 接下來筆者將會帶大家一起掌握如何用
  • 9個非常實用的CSS圖標庫
    其優點:輕鬆的定義圖標的顏色,大小,陰影,和任何與css相關的特性。使用矢量字體,這意味著他們可以完美的顯示在高解析度顯示器中。1、Iconfont阿里巴巴矢量圖標庫,Iconfont-國內功能很強大且圖標內容很豐富的矢量圖標庫,提供矢量圖標下載、在線存儲、格式轉換等功能。阿里巴巴體驗團隊傾力打造,設計和前端開發的便捷工具。
  • CSS14個實用技巧的精選和推薦
    此文章主要為大家介紹了CSS14個實用技巧的精選推薦,具有一定的參考價值,學習覺得挺不錯的,分享給大家。這很容易解決, 只需要在標籤樣式中加入 outline:none。body { overflow-x: hidden; }當你決定使用一個比瀏覽器窗口大的圖片或者flash時,這個技巧將非常有用。5、針對瀏覽器的選擇器這些選擇器在你需要針對某款瀏覽器進行css設計時將非常有用。
  • CSS新手閱讀的CSS技巧十則
    一般地,Border的寬度預設是medium,一般等於3到4個像素;預設的顏色是其中文字的顏色。如果這個值正好合適的話,就不用設那麼多了。三、給元素同時使用兩個類一般一個元素設定一個類(Class),但這並不意味著不能用兩個。
  • 這些資源讓你成為CSS專家 (二)​
    http://askthecssguy.com/articles/showing-hyperlink-cues-with-css/52、10個你可能不知道的CSS訣竅 — 涵蓋了CSS字體、圖片替換、垂直居中等技巧。
  • 太全面了,合理避稅的60種方法和42個技巧,老闆要知道,實用
    太全面了,合理避稅的60種方法和42個技巧,老闆要知道,實用很多企業懂得要避稅,但是他們不懂得其中的方法和技巧,常常忽略,覺得避稅就是省錢,我省了錢就是避稅。但是,沒有掌握避稅的方法和技巧,知道哪些避稅也是沒有用的。張張給大家找到了這些方法和技巧,希望對大家有用。
  • Web開發者的福利 30段超實用CSS代碼
    下面這段代碼就使用pre來封裝代碼,讓內容直接顯示在頁面中。/* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */}源碼地址:http://css-tricks.com/snippets/css/make-pre-text-wrap/14.滑鼠指向時變成手型網頁中有許多item在點擊時,滑鼠不會變成小手的形狀
  • 10個實用的CSS和JavaScript動畫插件/框架/庫
    10個實用的CSS和JavaScript動畫插件/框架/庫 站長之家(CHINAZ.com)12月1日編譯:在網站中嵌入動畫已經成為今年的設計趨勢之一
  • 17個CSS知識點整理
    來自:編程小技巧整理自網絡連結:
  • 十一款遊戲教你學會 CSS!
    網上有很多有助於學習CSS的遊戲,本文收集了一些非常實用的免費CSS遊戲,希望這些遊戲可以幫助你再次體驗CSS的樂趣!
  • JavaScript中的CSS: CSSX
    當我嘗試這種方法的時候,我碰到了兩個問題:第一個問題文本沒有樣式(FOUT)。如果我們依靠JavaScript提供CSS,那麼頁面在得到樣式之前,用戶看到的內容是沒有樣式的,這樣就會導致布局混亂和導致用戶的體驗非常的糟糕。第二個問題就是沒有樣式表。樣式寫在JavaScript中的應用示例有很多,但大多數都是內聯樣式。換句話說,他們只要是用來修改DOM元素的style。
  • 20+個最棒的CSS在線參考網站
    來自:Coder資源網via:cssauthor,本文由 Specs 翻譯整理連結:http://info.9iphp.com
  • CSS中behavior屬性語法簡介
    作者:52css來源:52css.com|2010-09-01 11:00
  • 資源放送:6個值得收藏CSS學習資源網站
    首頁 > 空間 > 關鍵詞 > CSS最新資訊 > 正文 資源放送:6個值得收藏CSS學習資源網站
  • Vue中使用animate.css實現動畫效果
    在實際開發過程中,有些頁面需要一些炫酷的動畫效果這時我們可以通過animate來實現Animate是一個非常實用的動畫庫官網: https://animate.style/Animate動畫庫裡面有很多實用的動畫效果展示如下:應用實例效果展示:使用方法:在vue中使用animate.css第一步:安裝
  • 提高CSS開發能力的技巧集
    來自:FedFun - CSDN博客連結:http://blog.csdn.net/whqet/article
  • 29個非常實用的HTML 5實例、教程和技巧
    51CTO推薦專題:HTML 5 下一代Web開發標準詳解由於Flash在Web和網際網路應用程式中的應用越來越少,HTML5為Web設計師和開發者打開了新的大門。在這種情況下,每一個Web開發者的確有必要了解HTML 5的基本教程、技巧和術語。
  • 10 個 GitHub 上超火的 CSS 技巧項目,找到寫 CSS 的靈感!
    You-need-to-know-css該項目是 CSS 的各種效果實現,尤其是動畫效果。筆者把自己的收穫和工作中常用的一些 CSS 小樣式總結成這份文檔。目前文檔一共包含 43 個 CSS 的小樣式(持續更新…),所以還是很不錯的學習 CSS 的項目來的。
  • 網頁設計之css+div PK table+css
    用過div+CSS作個整個網站,如果是純粹的div的布局是比較麻煩的,尤其是你div裡面嵌套div的,div布局的時候,你有些頁面效果還是要捨棄一點的,比如圖片的圓角,這些如果套div比較麻煩,在一個div在VS2005設計器裡面可能變形,如果過多的套div,你實現Ajax拖動效果的時候比較麻煩,所以我覺得眼下還是