本文主要跟大家分享CSS 進階技巧總結,使用技巧會讓人變的越來越懶,沒錯,我就是想讓你變懶。以下是我收集的CSS高級技巧,希望你懶出境界。 <br>
1. 黑白影像
這段程式碼會讓你的彩色照片顯示為黑白照片,是不是很酷?
<br>
img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); }
2. 使用 :not() 在選單上套用/取消套用邊框
#先為每個選單項目新增邊框
<br>
/* add border */ .nav li { border-right: 1px solid #666; }
然後再除去最後一個元素
<br>
// remove border / .nav li:last-child { border-right: none; }
可以直接使用:not() 偽類別來套用元素:
<br>
.nav li:not(:last-child) { border-right: 1px solid #666; }
這樣程式碼就乾淨,易讀,易於理解了。
當然,如果你的新元素有兄弟元素的話,也可以使用通用的兄弟選擇符(~):
<br>
.nav li:first-child ~ li { border-left: 1px solid #666; }
3. 頁面頂部陰影
下面這個簡單的CSS3 程式碼片段可以為網頁加上漂亮的頂部陰影效果:
<br>
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到每個p,h標記等。只要加入 body 即可:
<br>
body { line-height: 1; }
這樣文字元素就可以很容易地從 body 繼承。
5. 所有一切都垂直居中
#要將所有元素垂直居中,太簡單了:
<br>
html, body { height: 100%; margin: 0; } body { -webkit-align-items: center; -ms-flex-align: center; align-items: center; display: -webkit-flex; display: flex; }
看,是不是很簡單。
注意:在IE11中要小心flexbox
#6. 逗號分隔的清單
讓HTML清單項目看起來像是真正的,用逗號分隔的清單:
<br>
ul > li:not(:last-child)::after { content: ","; }
##對最後一個清單項目使用: not() 偽類。
7. 使用負面的 nth-child 選擇項目
#在CSS中使用負的 nth-child 選擇項目1到項目n。
<br>
li { display: none; } /* select items 1 through 3 and display them */ li:nth-child(-n+3) { display: block; }
8. 對圖示使用SVG
我們沒有理由不對圖示使用SVG:
<br/>
.logo {
background: url("logo.svg ");
}
#SVG對所有的解析度型別都有良好的擴充性,並支援所有瀏覽器都回歸到IE9。這樣可以避開.png、.jpg或.gif檔了。
9. 最佳化顯示文字
有時,字體並不能在所有裝置上達到最佳的顯示,所以可以讓裝置瀏覽器來幫助你:
<br>
html { -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }
#註:請負責任地使用optimizeLegibility。此外,IE /Edge沒有 text-rendering 支援。
10. 對純CSS 滑桿使用max-height
#使用max-height 和溢出隱藏來實現只有CSS的滑桿:
<br>
.slider ul { max-height: 0; overlow: hidden; } .slider:hover ul { max-height: 1000px; transition: .3s ease; }
11.繼承box-sizing
讓box-sizing 繼承 html :
<br>
html { box-sizing: border-box; } *, *:before, *:after { box-sizing: inherit; }
這樣在其他插件或槓桿其他行為的元件中就能更容易改變box-sizing 了。
12. 表格单元格等宽
表格工作起来很麻烦,所以务必尽量使用 table-layout: fixed 来保持单元格的等宽:
<br>
.calendar { table-layout: fixed; }
13. 用 Flexbox 摆脱外边距的各种 hack
当需要用到列分隔符时,通过flexbox的 space-between 属性,你就可以摆脱nth-,first-,和 last-child 的hack了:
<br>
.list { display: flex; justify-content: space-between; } .list .person { flex-basis: 23%; }
现在,列表分隔符就会在均匀间隔的位置出现。
14. 使用属性选择器用于空链接
当a元素没有文本值,但 href 属性有链接的时候显示链接:
<br>
a[href^="http"]:empty::before { content: attr(href); }
相当方便。
15. 检测鼠标双击
HTML:
<br>
<p class="test3"> <span><input type="text" value=" " readonly="true" /> <a href="http://renpingjun.com">Double click me</a></span> </p>
CSS:
<br>
.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 写出三角形
<br/>
/* create an arrow that points up */ p.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 */ p.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 */ p.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 */ p.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() 用法类似于函数,能够给元素设置动态的值:
<br>
/* 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 能够很简单就实现:
<br>
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. 禁用鼠标事件
CSS3 新增的 pointer-events 让你能够禁用元素的鼠标事件,例如,一个连接如果设置了下面的样式就无法点击了。
<br>
.disabled { pointer-events: none; }
20. 模糊文本
简单但很漂亮的文本模糊效果,简单又好看!
<br>
.blur { color: transparent; text-shadow: 0 0 5px rgba(0,0,0,0.5); }
相关推荐:
常用的CSS高级技巧_html/css_WEB-ITnose
以上是CSS 進階技巧總結分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

前幾天我得到了這個問題。我的第一個想法是:奇怪的問題!特異性是關於選擇者的,而在符號不是選擇器,那麼...無關緊要?

在這篇文章中,我們將使用我構建和部署的電子商務商店演示來進行Netlify,以展示如何為傳入數據製作動態路線。這是一個公平的


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境