這里我工作中收集了10個(gè)很不錯(cuò)的CSS技巧硼补,你可以用在你的項(xiàng)目上。它可以幫你很好地整理你的元素并讓他們看起來(lái)蠻酷的微酬。下面開始我們的內(nèi)容霉赡,希望你會(huì)喜歡它橄务。下面是我收集的CSS技巧,希望能幫助到你穴亏,感覺(jué)收藏吧蜂挪。
在這里我還是要推薦下我自己建的web前端開發(fā)學(xué)習(xí)群:731669587,群里都是學(xué)web前端開發(fā)的嗓化,如果你正在學(xué)習(xí)前端 棠涮,小編歡迎你加入,今天分享的這個(gè)案例已經(jīng)上傳到群文件刺覆,大家都是軟件開發(fā)黨严肪,不定期分享干貨(只有前端軟件開發(fā)相關(guān)的),包括我自己整理的一份2018最新的前端進(jìn)階資料和高級(jí)開發(fā)教程谦屑,歡迎進(jìn)階中和進(jìn)想深入前端的小伙伴驳糯。
1. 黑白圖像
這段代碼會(huì)讓你的彩色照片顯示為黑白照片,是不是很酷氢橙?
img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%);}
2. 使用?:not()?在菜單上應(yīng)用/取消應(yīng)用邊框
先給每一個(gè)菜單項(xiàng)添加邊框
/* add border */.nav li { border-right: 1px solid #666;}
……然后再除去最后一個(gè)元素……
// remove border /.nav li:last-child { border-right: none;}
……可以直接使用 :not() 偽類來(lái)應(yīng)用元素:
.nav li:not(:last-child) { border-right: 1px solid #666;}
這樣代碼就干凈酝枢,易讀,易于理解了悍手。
當(dāng)然隧枫,如果你的新元素有兄弟元素的話,也可以使用通用的兄弟選擇符(~):
..nav li:first-child ~ li { border-left: 1px solid #666;}
3. 頁(yè)面頂部陰影
下面這個(gè)簡(jiǎn)單的 CSS3 代碼片段可以給網(wǎng)頁(yè)加上漂亮的頂部陰影效果:
body:before { content: ""; position: fixed; top: -10px; left: 0; width: 100%; height: 10px; -webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8); -moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8); box-shadow: 0px 0px 10px rgba(0,0,0,.8); z-index: 100;}
4. 給 body 添加行高
你不需要分別添加 line-height 到每個(gè)p,h標(biāo)記等谓苟。只要添加到 body 即可:
body { line-height: 1;}
這樣文本元素就可以很容易地從 body 繼承。
5. 所有一切都垂直居中
要將所有元素垂直居中协怒,太簡(jiǎn)單了:
html, body { height: 100%; margin: 0;}body { -webkit-align-items: center; -ms-flex-align: center; align-items: center; display: -webkit-flex; display: flex;}
看涝焙,是不是很簡(jiǎn)單。
注意:在IE11中要小心flexbox孕暇。
6. 逗號(hào)分隔的列表
讓HTML列表項(xiàng)看上去像一個(gè)真正的仑撞,用逗號(hào)分隔的列表:
ul > li:not(:last-child)::after { content: ",";}
對(duì)最后一個(gè)列表項(xiàng)使用 :not() 偽類赤兴。
7. 使用負(fù)的 nth-child 選擇項(xiàng)目
在CSS中使用負(fù)的 nth-child 選擇項(xiàng)目1到項(xiàng)目n。
li { display: none;}/* select items 1 through 3 and display them */li:nth-child(-n+3) { display: block;}
8. 對(duì)圖標(biāo)使用 SVG
我們沒(méi)有理由不對(duì)圖標(biāo)使用SVG:
.logo { background: url("logo.svg");}
SVG對(duì)所有的分辨率類型都具有良好的擴(kuò)展性隧哮,并支持所有瀏覽器都回歸到IE9桶良。這樣可以避開.png、.jpg或.gif文件了沮翔。
9. 優(yōu)化顯示文本
有時(shí)陨帆,字體并不能在所有設(shè)備上都達(dá)到最佳的顯示,所以可以讓設(shè)備瀏覽器來(lái)幫助你:
html { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;}
注:請(qǐng)負(fù)責(zé)任地使用 optimizeLegibility采蚀。此外疲牵,IE /Edge沒(méi)有 text-rendering 支持。
10. 對(duì)純 CSS 滑塊使用 max-height
使用 max-height 和溢出隱藏來(lái)實(shí)現(xiàn)只有CSS的滑塊:
.slider ul { max-height: 0; overlow: hidden;}.slider:hover ul { max-height: 1000px; transition: .3s ease;}
11. 繼承 box-sizing
讓 box-sizing 繼承 html:
html { box-sizing: border-box;}*, *:before, *:after { box-sizing: inherit;}
這樣在插件或杠桿其他行為的其他組件中就能更容易地改變 box-sizing 了榆鼠。
12. 表格單元格等寬
表格工作起來(lái)很麻煩纲爸,所以務(wù)必盡量使用 table-layout: fixed 來(lái)保持單元格的等寬:
.calendar { table-layout: fixed;}
13. 用 Flexbox 擺脫外邊距的各種 hack
當(dāng)需要用到列分隔符時(shí),通過(guò)flexbox的 space-between 屬性妆够,你就可以擺脫nth-识啦,first-,和 last-child 的hack了:
.list { display: flex; justify-content: space-between;}.list .person { flex-basis: 23%;}
現(xiàn)在神妹,列表分隔符就會(huì)在均勻間隔的位置出現(xiàn)颓哮。
14. 使用屬性選擇器用于空鏈接
當(dāng)a元素沒(méi)有文本值,但 href 屬性有鏈接的時(shí)候顯示鏈接:
a[href^="http"]:empty::before { content: attr(href);}
相當(dāng)方便灾螃。
15. 檢測(cè)鼠標(biāo)雙擊
HTML:
CSS:
.test3 span { position: relative;}.test3 span a { position: relative; z-index: 2;}.test3 span a:hover, .test3 span a:active { z-index: 4;}.test3 span input { background: transparent; border: 0; cursor: pointer; position: absolute; top: -1px; left: 0; width: 101%; /* Hacky */ height: 301%; /* Hacky */ z-index: 3;}.test3 span input:focus { background: transparent; border: 0; z-index: 1;}
16. CSS 寫出三角形
/* create an arrow that points up */div.arrow-up { width:0px; height:0px; border-left:5px solid transparent; /* left arrow slant */ border-right:5px solid transparent; /* right arrow slant */ border-bottom:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px;}/* create an arrow that points down */div.arrow-down { width:0px; height:0px; border-left:5px solid transparent; border-right:5px solid transparent; border-top:5px solid #2f2f2f; font-size:0px; line-height:0px;}/* create an arrow that points left */div.arrow-left { width:0px; height:0px; border-bottom:5px solid transparent; /* left arrow slant */ border-top:5px solid transparent; /* right arrow slant */ border-right:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px;}/* create an arrow that points right */div.arrow-right { width:0px; height:0px; border-bottom:5px solid transparent; /* left arrow slant */ border-top:5px solid transparent; /* right arrow slant */ border-left:5px solid #2f2f2f; /* bottom, add background color here */ font-size:0px; line-height:0px;}
17. CSS3 calc() 的使用
calc() 用法類似于函數(shù)题翻,能夠給元素設(shè)置動(dòng)態(tài)的值:
/* basic calc */.simpleBlock { width: calc(100% - 100px);}/* calc in calc */.complexBlock { width: calc(100% - 50% / 3); padding: 5px calc(3% - 2px); margin-left: calc(10% + 10px);}
18. 文本漸變
文本漸變效果很流行,使用 CSS3 能夠很簡(jiǎn)單就實(shí)現(xiàn):
h2[data-text] { position: relative;}h2[data-text]::after { content: attr(data-text); z-index: 10; color: #e3e3e3; position: absolute; top: 0; left: 0; -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,0)), color-stop(50%, rgba(0,0,0,1)), to(rgba(0,0,0,0)));}
19. 禁用鼠標(biāo)事件
CSS3 新增的 pointer-events 讓你能夠禁用元素的鼠標(biāo)事件腰鬼,例如嵌赠,一個(gè)連接如果設(shè)置了下面的樣式就無(wú)法點(diǎn)擊了。
.disabled { pointer-events: none; }
20. 模糊文本
簡(jiǎn)單但很漂亮的文本模糊效果熄赡,簡(jiǎn)單又好看姜挺!
.blur { color: transparent; text-shadow: 0 0 5px rgba(0,0,0,0.5);}
21.簡(jiǎn)單的方法調(diào)整圖片大小
.content img {
height:auto;
width:500px;
}
22.IE HTML Hack
div#content {width: 580px}
* html body div#content {width: 600px}
23.CSS陰影
.shadow {
-moz-box-shadow: 3px 3px 5px 6px #ccc;
-webkit-box-shadow: 3px 3px 5px 6px #ccc;
box-shadow: 3px 3px 5px 6px #ccc;
}
24.CSS首字放大
p:first-letter {
display: block;
float: left;
margin: 5px 5px 0 0;
color: red;
font-size: 1.4em;
background: #ddd;
font-family: Helvetica;
}
25.用CSS翻轉(zhuǎn)圖像
#content img {
-moz-transform: scaleX(-1);
-o-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
-ms-filter: "FlipH";
}
26.移除被點(diǎn)鏈接的點(diǎn)框
a {outline: none}
或者
a {outline: 0}
27.在CSS中使用特殊字體
你可以使用CSS來(lái)加載特殊字體,你要做的就是把這個(gè)TTF格式的字體上傳到服務(wù)器上彼硫,然后使用字體規(guī)則在CSS上導(dǎo)入它炊豪。
28.元素透明
.element {
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
29.使用CSS顯示鏈接之后的URL
a:after{content:" (" attr(href) ") ";}
這會(huì)在鏈接錨點(diǎn)后顯示URL。你也可以用字體或其他樣式定義它拧篮。
30.為手持設(shè)備定制特殊樣式
31.文字的水平居中
text-align:center;
32.link狀態(tài)的設(shè)置順序
a:link
a:visited
a:hover
a:active
33.用圖片充當(dāng)列表標(biāo)志
ul {list-style: none}
ul li {
background-image: url("path-to-your-image");
background-repeat: none;
background-position: 0 0.5em;
}
34.禁止自動(dòng)換行
h1 { white-space:nowrap; }
35.獲得焦點(diǎn)的表單元素?
input:focus { border: 2px solid green; }
36.user-select 禁止用戶選中文本
div {
user-select: none; /* Standard syntax */
}
37.清除手機(jī)tap事件后element 時(shí)候出現(xiàn)的一個(gè)高亮
* {
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
38.增強(qiáng)用戶體驗(yàn)词渤,使用偽元素實(shí)現(xiàn)增大點(diǎn)擊熱區(qū)
.btn::befoer{
content:"";
position:absolute;
top:-10px;
right:-10px;
bottom:-10px;
left:-10px;
}
39.偽元素實(shí)現(xiàn)換行,替代換行標(biāo)簽
inline-element ::after{
content:"A";
white-space: pre;
}
40.will-change提高頁(yè)面滾動(dòng)串绩、動(dòng)畫等渲染性能
/* 關(guān)鍵字值 */
will-change: auto;
will-change: scroll-position;
will-change: contents;
will-change: transform; /* 示例 */
will-change: opacity; /* 示例 */
will-change: left, top; /* 兩個(gè)示例 */
will-change的使用也要謹(jǐn)慎村生,遵循最小化影響原則穿香,不要這樣直接寫在默認(rèn)狀態(tài)中,因?yàn)閣ill-change會(huì)一直掛著:
.will-change {
will-change: transform;
transition: transform 0.3s;
}
.will-change:hover {
transform: scale(1.5);
}
可以讓父元素hover的時(shí)候脉幢,聲明will-change,這樣,移出的時(shí)候就會(huì)自動(dòng)remove,觸發(fā)的范圍基本上是有效元素范圍。
.will-change-parent:hover .will-change {
will-change: transform;
}
.will-change {
transition: transform 0.3s;
}
.will-change:hover {
transform: scale(1.5);
}
41.box-sizing 讓元素的寬度塞淹、高度包含border和padding
{
box-sizing: border-box;
}
42.calc() function, 計(jì)算屬性值
div {
width: calc(100% - 100px);
}
例子就是讓寬度為100%減去100px的值
43.css實(shí)現(xiàn)不換行、自動(dòng)換行罪裹、強(qiáng)制換行
//不換行
white-space:nowrap;
//自動(dòng)換行
word-wrap: break-word;
word-break: normal;
//強(qiáng)制換行
word-break:break-all;
44.perspective 透視
這個(gè)屬性的存在決定你看到的元素是2d還是3d饱普。一般設(shè)置在包裹元素的父類上。
.div-box {
perspective: 400px;
}
45.設(shè)置圖像透明度的兩種方式
opcity:0.6坊谁;
background:rgba(0,0,0,.6)费彼;
46.position定位屬性
position屬性指定一個(gè)元素(靜態(tài)的、相對(duì)的口芍、絕對(duì)或固定)的定位方法的類型箍铲。
position的屬性值:
absolute:生成絕對(duì)定位的元素;
fixed:生成絕對(duì)定位的元素鬓椭,相對(duì)于瀏覽器窗口進(jìn)行定位颠猴;
relative:生成相對(duì)定位的元素,相對(duì)于其正常位置經(jīng)行定位小染。
z-index:指定一個(gè)元素的堆疊順序翘瓮。
47.cursor屬性
cursor屬性定義了鼠標(biāo)指針?lè)旁谝粋€(gè)元素邊界范圍內(nèi)時(shí)所用的光標(biāo)形狀。
CSS提供的cursor值:
pointer :小手指裤翩;
help:箭頭加問(wèn)號(hào)资盅;
wait:轉(zhuǎn)圈圈;
move:移動(dòng)光標(biāo)踊赠;
crosshair:十字光標(biāo)呵扛。
通過(guò)pointer屬性我們可以偽造超鏈接:
pointer
48.隱藏沒(méi)有靜音、自動(dòng)播放的影片
video[autoplay]:not([muted]) {
display: none;
}
49.Font-Size 基準(zhǔn)
/* 假設(shè)瀏覽器的默認(rèn)的大小是 16px 筐带, 首先將其設(shè)置為10px (font-size:10/16) */
body {font-size:10/16;}
/* 然后就可以用em做統(tǒng)一字體單位了 2.4em = 24px */
h1 {font-size: 2.4 em}
50.透明容器
.element {
filter:alpha(opacity=50); /* for ie */
-moz-opacity:0.5; /* for ff */
-khtml-opacity: 0.5; /* for webkit as chrome */
opacity: 0.5; /* for opera */
}