這裏我工做中收集了10個很不錯的CSS技巧,你能夠用在你的項目上。它能夠幫你很好地整理你的元素並讓他們看起來蠻酷的。下面開始咱們的內容,但願你會喜歡它。下面是我收集的CSS技巧,但願能幫助到你,感受收藏吧。css
1. 黑白圖像html
這段代碼會讓你的彩色照片顯示爲黑白照片,是否是很酷?前端
img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%);}
2. 使用 :not()
在菜單上應用/取消應用邊框web
先給每個菜單項添加邊框chrome
/* add border */.nav li { border-right: 1px solid #666;}
……而後再除去最後一個元素……瀏覽器
// remove border /.nav li:last-child { border-right: none;}
……能夠直接使用 :not() 僞類來應用元素:服務器
.nav li:not(:last-child) { border-right: 1px solid #666;}
這樣代碼就乾淨,易讀,易於理解了。ssh
固然,若是你的新元素有兄弟元素的話,也可使用通用的兄弟選擇符(~):ide
..nav li:first-child ~ li { border-left: 1px solid #666;}
3. 頁面頂部陰影svg
下面這個簡單的 CSS3 代碼片斷能夠給網頁加上漂亮的頂部陰影效果:
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 便可:
body { line-height: 1;}
這樣文本元素就能夠很容易地從 body 繼承。
5. 全部一切都垂直居中
要將全部元素垂直居中,太簡單了:
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列表項看上去像一個真正的,用逗號分隔的列表:
ul > li:not(:last-child)::after { content: ",";}
對最後一個列表項使用 :not() 僞類。
7. 使用負的 nth-child 選擇項目
在CSS中使用負的 nth-child 選擇項目1到項目n。
li { display: none;}/* select items 1 through 3 and display them */li:nth-child(-n+3) { display: block;}
8. 對圖標使用 SVG
咱們沒有理由不對圖標使用SVG:
.logo { background: url("logo.svg");}
SVG對全部的分辨率類型都具備良好的擴展性,並支持全部瀏覽器都回歸到IE9。這樣能夠避開.png、.jpg或.gif文件了。
9. 優化顯示文本
有時,字體並不能在全部設備上都達到最佳的顯示,因此可讓設備瀏覽器來幫助你:
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的滑塊:
.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. 表格單元格等寬
表格工做起來很麻煩,因此務必儘可能使用 table-layout: fixed 來保持單元格的等寬:
.calendar { table-layout: fixed;}
13. 用 Flexbox 擺脫外邊距的各類 hack
當須要用到列分隔符時,經過flexbox的 space-between 屬性,你就能夠擺脫nth-,first-,和 last-child 的hack了:
.list { display: flex; justify-content: space-between;}.list .person { flex-basis: 23%;}
如今,列表分隔符就會在均勻間隔的位置出現。
14. 使用屬性選擇器用於空連接
當a元素沒有文本值,但 href 屬性有連接的時候顯示連接:
a[href^="http"]:empty::before { content: attr(href);}
至關方便。
15. 檢測鼠標雙擊
HTML:
<div class="test3"> <span><input type="text" value=" " readonly="true" /> <a href="http://renpingjun.com">Double click me</a></span></div>
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() 用法相似於函數,可以給元素設置動態的值:
/* 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 可以很簡單就實現:
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 讓你可以禁用元素的鼠標事件,例如,一個鏈接若是設置了下面的樣式就沒法點擊了。
.disabled { pointer-events: none; }
20. 模糊文本
簡單但很漂亮的文本模糊效果,簡單又好看!
.blur { color: transparent; text-shadow: 0 0 5px rgba(0,0,0,0.5);}
21.簡單的方法調整圖片大小
.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翻轉圖像
#content img {
-moz-transform: scaleX(-1);
-o-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
-ms-filter: "FlipH";
}
26.移除被點連接的點框
a {outline: none}
或者
a {outline: 0}
27.在CSS中使用特殊字體
你可使用CSS來加載特殊字體,你要作的就是把這個TTF格式的字體上傳到服務器上,而後使用字體規則在CSS上導入它。
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) ") ";}
這會在連接錨點後顯示URL。你也能夠用字體或其餘樣式定義它。
30.爲手持設備定製特殊樣式
<link type="text/css" rel="stylesheet" href="handheldstyle.css" media="handheld">
31.文字的水平居中
text-align:center;
32.link狀態的設置順序
a:link
a:visited
a:hover
a:active
33.用圖片充當列表標誌
ul {list-style: none}
ul li {
background-image: url("path-to-your-image");
background-repeat: none;
background-position: 0 0.5em;
}
34.禁止自動換行
h1 { white-space:nowrap; }
35.得到焦點的表單元素
input:focus { border: 2px solid green; }
36.user-select 禁止用戶選中文本
div {
user-select: none; /* Standard syntax */
}
37.清除手機tap事件後element 時候出現的一個高亮
* {
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
38.加強用戶體驗,使用僞元素實現增大點擊熱區
.btn::befoer{
content:"";
position:absolute;
top:-10px;
right:-10px;
bottom:-10px;
left:-10px;
}
39.僞元素實現換行,替代換行標籤
inline-element ::after{
content:"A";
white-space: pre;
}
40.will-change提升頁面滾動、動畫等渲染性能
/* 關鍵字值 */
will-change: auto;
will-change: scroll-position;
will-change: contents;
will-change: transform; /* <custom-ident>示例 */
will-change: opacity; /* <custom-ident>示例 */
will-change: left, top; /* 兩個<animateable-feature>示例 */
will-change的使用也要謹慎,遵循最小化影響原則,不要這樣直接寫在默認狀態中,由於will-change會一直掛着:
.will-change {
will-change: transform;
transition: transform 0.3s;
}
.will-change:hover {
transform: scale(1.5);
}
可讓父元素hover的時候,聲明will-change,這樣,移出的時候就會自動remove,觸發的範圍基本上是有效元素範圍。
.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, 計算屬性值
div {
width: calc(100% - 100px);
}
例子就是讓寬度爲100%減去100px的值
43.css實現不換行、自動換行、強制換行
//不換行
white-space:nowrap;
//自動換行
word-wrap: break-word;
word-break: normal;
//強制換行
word-break:break-all;
44.perspective 透視
這個屬性的存在決定你看到的元素是2d仍是3d。通常設置在包裹元素的父類上。
.div-box {
perspective: 400px;
}
45.設置圖像透明度的兩種方式
opcity:0.6;
background:rgba(0,0,0,.6);
46.position定位屬性
position屬性指定一個元素(靜態的、相對的、絕對或固定)的定位方法的類型。
position的屬性值:
absolute:生成絕對定位的元素;
fixed:生成絕對定位的元素,相對於瀏覽器窗口進行定位;
relative:生成相對定位的元素,相對於其正常位置經行定位。
z-index:指定一個元素的堆疊順序。
47.cursor屬性
cursor屬性定義了鼠標指針放在一個元素邊界範圍內時所用的光標形狀。
CSS提供的cursor值:
pointer :小手指;
help:箭頭加問號;
wait:轉圈圈;
move:移動光標;
crosshair:十字光標。
經過pointer屬性咱們能夠僞造超連接:
<span >pointer</span>
48.隱藏沒有靜音、自動播放的影片
video[autoplay]:not([muted]) {
display: none;
}
49.Font-Size 基準
/* 假設瀏覽器的默認的大小是 16px , 首先將其設置爲10px (font-size:10/16) */
body {font-size:10/16;}
/* 而後就能夠用em作統一字體單位了 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 */
}