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

來自 | 《css世界》一書
1、清除浮動
img {float: left;}/* 清除浮動 */.clear::after {content: "";display: block;clear: both;}
2、文字少時居中,多時靠左
但是要注意,當(dāng)p的內(nèi)容為英文單詞組成的時候,如果單詞過長,而不是“ pppppppppppppppppppppppppppppp”這樣的一次,會被視為一個單位而造成超過div的尺寸。
如果你想要英文字符也有中文字符的效果的話,在p使用“ word-break:break-all”。
.box {text-align: center;}.content {display: inline-block;text-align: left;}
3、凹凸人
目的在于制造一個凹或凸的形狀,利用了“ 2”中英文單詞不換行的特性
.ao {display: inline-block;width: 0;}.ao::before {content: "love 你 love";outline: 2px solid #000;color: #fff;}
4、讓padding,border不影響盒模型的大小
相信這點大部分人都知道,但是有一些奇怪的行為,比如說width
div {box-sizing: border-box;}
5、身高:100%占屏效果
html,body {height: 100%;}div {height: 100%}div {position: absolute;height: 100%;}
6、任意高度元素展開
缺點是,如果高度太大會造成展開過快和重復(fù)中斷,那么這個足夠大的值應(yīng)該適當(dāng)。
div {max-height: 0;overflow: hidden;transition: max-height .25s;}div.active {max-height: 666px; /* 需要足夠大的值 */}
7、優(yōu)雅的圖片未加載或加載失敗效果
需要注意的是,圖片顯示完成后,img會成為“替換元素”,而替換元素是無法設(shè)置偽元素的,因為內(nèi)容被圖片替換掉了;還需要注意attr里面的變量不能加雙引號。
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);}
8、CSS的懸浮圖片替換效果
需要注意的是,如果快捷保存圖片,保存的是src內(nèi)的圖片,而不是替換之后的。
img:hover {content: url(amazing.gif);}
9、利于seo的“替換元素”標(biāo)題logo
用h1的原因主要是因為seo,語義化的問題。
Weismann's blog
h1 {content: url(logo.gif);}
10、高兼容,自動等寬,底部對齊的柱狀圖
需要注意的是,第一個i不能換行,換行后會產(chǎn)生后移的結(jié)果。
.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自定 */}
11、高兼容性的加載效果
在IE6-IE9下是...,其他都是動態(tài)的;使用點的目的是語義化和低版本瀏覽器的兼容。
正在加載中... 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); }}
12、擴大點擊區(qū)域
第一種主要利用了內(nèi)聯(lián)元素的填充只會影響外觀和不影響布局的特點;第二種針對其他屬性會改變背景圖定位的一種方式。
demoa {padding: 20px 0;}a {border: 11px solid transparent;}
13、不使用偽元素的“三道杠”和“圓點”效果
.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;}.icon {width: 120px;height: 20px;border-top: 60px double;border-bottom: 20px solid;}.icon {display: inline-block;width: 100px; height: 100px;padding: 10px;border: 10px solid;border-radius: 50%;background-color: currentColor;background-clip: content-box;}
14、導(dǎo)航欄消除右邊多余的尺寸
利用保證金來改變尺寸,需要注意,改變尺寸的元素水平方向的尺寸不能是確定的。
div {width: 380px;}ul {margin-right: -20px;}ul > li {float: left;width: 100px;margin-right: 20px;}
15、正確的滾動底部留白方式
如果使用padding留白,在Firefox和IE不會顯示。
.box {height:200px;overflow:auto;}.box>img {margin: 50px 0;}
16、高兼容的多欄等高
注意container高度不能是確定值,缺點是如果在內(nèi)部使用錨點定位會出現(xiàn)問題。
正方觀點
觀點1
觀點1
反方觀點
觀點1
.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;}
17、正確的塊級元素右對齊
自動值對于保證金是占用剩余的空間。
demodiv {width: 100px;margin-left: auto;}
18、圖片上傳增加框
此技巧主要說明border的顏色默認(rèn)是繼承自color的。
.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;}
19、不影響背景圖片位置設(shè)置邊距
和增加點擊區(qū)域第二種方式一樣
.box {display: inline-block;width: 100px;height: 100px;border-right: 50px solid transparent;background-position: 100% 50%;}
20、border制作梯形,各種三角形
div {width: 10px; height: 10px;border: 10px solid;border-color: #f30 transparent transparent;}div {width: 0;border-width: 10px 20px;border-style: solid;border-color: #f30 transparent transparent;}div {width: 0;border-width: 10px 20px;border-style: solid;border-color: #f30 #f30 transparent transparent;}
21、高兼容雙欄,一邊等寬一邊自適應(yīng),等高布局
缺點是邊框不支持百分比,最多2-3欄。
1231231231234.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;}
22、內(nèi)聯(lián)元素“近似”垂直居中
而為什么說“近似”,一句話說清楚,請看開頭
span {line-height: 24px;}
多行內(nèi)容“近似”垂直居中
基于行高實現(xiàn)的....box {width: 120px;line-height: 120px;background-color: #f0f3f9;}.content {display: inline-block;line-height: 20px;margin: 0 20px;vertical-align: middle;}
23、容器內(nèi)圖片的垂直方向間隙問題
產(chǎn)生的問題和“幽靈空白子系統(tǒng)”和x-height有關(guān),你可以嘗試在img前加入x字符觀察一下。
.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;}
24、圖標(biāo)文字對齊
ex代表的是x-height的高度,根據(jù)x字形的不同(如font-family)而不同。
<div class="box"><p><i class="icon icon-demo">i>拉拉p>div><style>.box {/* 根據(jù)圖片大小變化 */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 {/* 低版本IE7兼容 */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、永遠(yuǎn)居中的彈框
特點是內(nèi)容和瀏覽器尺寸變化都是自動變換大小和位置,可以通過偽元素的高度控制上下位置。
<div class="container"><div class="dialog">demodialog>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、文字環(huán)繞圖片
float的真正用途。
demo,demo,demo,demo,demo,demo,demo
.box {width: 100px;}img {float: left;width: 40px;height: 40px;}
27、利用溢出:隱藏自定義滾動條
實際上overflow:hidden是可以滾動的,可以通過錨點,focus,scrollTop滾動。滾動條的實現(xiàn)請自行發(fā)揮。
28、通過標(biāo)簽實現(xiàn)的選項卡效果
與錨點不同的是不會觸發(fā)由內(nèi)到外(多層滾動造成的某種事件冒泡的效果)的頁面跳動(元素上邊與分段上邊對齊),還支持Tab選項的效果;缺點是需要js支持效果。
<div class="box"><div class="list"><input id="one">1div><div class="list"><input id="two">2div><div class="list"><input id="three">3div><div class="list"><input id="four">4div>div><div class="link"><label class="click" for="one">1label><label class="click" for="two">2label><label class="click" for="three">3label><label class="click" for="four">4label>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、“包含塊”的絕對定位元素“一柱擎天”問題。
拉拉.father {position: relative;width: 20px;height: 20px;}.son {position: absolute;/* 解決方案 */white-space: nowrap;}
30、“無依賴絕對定位”的表單驗證應(yīng)用
在一個元素上如果單用(父元素的位置屬性均是替換)“ position:absolute”,事實上元素將原地不動,最終會產(chǎn)生BFC。
郵箱格式不準(zhǔn)確(示意)....group {width: 300px;}.label {float: left;}.remark {position: absolute;}
31、主體頁面?zhèn)冗厵?/span>
利用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、不通過寬度和高度設(shè)置預(yù)定全占用
利用top和bottom或left和right同時設(shè)置的時候會觸發(fā)流體特性的特點;與通過“ top:0; left:0; width:100%; height:100%;”,在設(shè)置邊距,邊框, padding的時候不會溢出到合并的外面(就算你想到box-sizing,那margin呢?);而之所以用span的原因是想說明絕對定位放置元素的顯示置為塊。
span {position: absolute;top:0;left:0;right:0;bottom: 0;}
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;”創(chuàng)建并合并到z-index的負(fù)值將陰影放置在“ contaniner”和“ page”之間。
你可以嘗試將關(guān)鍵CSS去掉查看效果。
<div class="container"><div class="page"><h4>demoh4><p>demop>div>div><style>.container {background-color: #666;height: 1000px;/* 創(chuàng)建層疊上下文,關(guān)鍵 */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瀏覽器如果字體設(shè)置12px以下的大小(新版本已經(jīng)不限制了),會被自動處理成12px,但是有一個值除外,0。
<style>p {font-size: 0;}style>
36、解決text-decoration下劃線和文本重疊
因為是內(nèi)聯(lián)元素,所以完全不用擔(dān)心會影響元素高度的問題。
<style>a {text-decoration: none;border-bottom: 1px solid;padding-bottom: 5px;}style>
37、自動將輸入的小寫字母轉(zhuǎn)換大寫
input {text-transform: uppercase;}
38、價格場景下的首個符號選擇器
特點是可以讓html結(jié)構(gòu)活躍干凈。
¥399
.price:first-letter {...}
39、元素隱藏同時資源不加載
后續(xù)可通過script.innerHTML訪問。
40、頭像裁剪矩形鏤空效果
主要利用輪廓。
.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;}
41、自定義光標(biāo)
需要注意IE只支持cur文件。
.cursor-demo {cursor: url(demo.cur);}
42、修改水平流到垂直流
兼容到IE7;此應(yīng)用涉及到一體的東西,所有水平流的特性都可以應(yīng)用到垂直流中(稱為水平居中變成了垂直居中)。
.verticle-mode{writing-mode: tb-rl;-webkit-writing-mode: vertical-rl;writing-mode: vertical-rl;}
推薦閱讀

