里我工作中收集了10個很不錯的CSS技巧,你可以用在你的項目上。它可以幫你很好地整理你的元素并讓他們看起來蠻酷的。下面開始我們的內容,希望你會喜歡它。下面是我收集的CSS技巧,希望能幫助到你,感覺收藏吧。
1. 黑白圖像
這段代碼會讓你的彩色照片顯示為黑白照片,是不是很酷?
img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); }
2. 使用 :not()
在菜單上應用/取消應用邊框
先給每一個菜單項添加邊框
/* 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; }
這樣代碼就干凈,易讀,易于理解了。
當然,如果你的新元素有兄弟元素的話,也可以使用通用的兄弟選擇符(~):
..nav li:first-child ~ li { border-left: 1px solid #666; }
3. 頁面頂部陰影
下面這個簡單的 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 >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;
}
太陽、地球、月球應該是我們人人都比較了解的天體,它們的運動軌跡是我們研究星系乃至宇宙的重要信息之一。那么我們如何使用HTML + CSS來模擬這么一個效果呢?
簡單的來說就是轉圈。
// 定義軌跡
@keyframes rotate {
from {
transform: rotateZ(0);
}
to {
transform: rotateZ(360deg);
}
}
/* 定義一個太陽 */
.sum {
position: relative;
margin: 100px auto 0;
width: 200px;
height: 200px;
border-radius: 50%;
background: radial-gradient(
circle at center,
#ff9b9b 0%,
rgba(235, 36, 64, 0) 70%
);
}
地球繞太陽一圈的時間是365.24天左右,這被稱為一年。每年有一個額外的日子被稱為閏年,這一年會有366天,目的是為了彌補地球繞太陽的公轉周期與我們日歷的定義周期之間的不匹配。
/* 定義地球位置及公轉信息 */
.earth {
position: absolute;
/* 定義地球與太陽的相對位置以及軌道線 */
width: 300px;
height: 300px;
margin-left:-50px;
margin-top:-50px;
border:1px solid #dddddd;
border-radius:50%;
/*動畫: 設定公轉時間以及軌跡 */
animation: rotate 36.524s infinite linear;
}
/* 地球本體 */
.earth::before{
content: ' ';
position:absolute;
background-color: blue;
width: 30px;
height: 30px;
margin-left:70px;
border-radius: 30px;
}
月球繞地球一圈的時間大約是27.32天。這被稱為一個月(也稱為地月周期)。月亮的運動軌跡略呈橢圓形,因此月球與地球的距離會隨時間而變化,這也導致月球的運動速度略有不同,有時會更快,有時會更慢,但平均值仍然是27.32天左右。
/* 定義月球位置及公轉信息 */
.moon {
position: absolute;
/* 定義月球與地球的相對位置以及軌道線 */
width: 60px;
height: 60px;
margin-left:55px;
margin-top:-15px;
border:1px solid #dddddd;
border-radius:50%;
/*動畫: 設定公轉時間以及軌跡 */
animation: rotate 2.732s infinite linear;
}
/* 月球本體 */
.moon::before{
content: ' ';
position:absolute;
background-color: blue;
width: 10px;
height: 10px;
margin-left:7px;
border-radius: 10px;
}
<div class="sum">
<div class="earth">
<div class="moon"></div>
</div>
</div>
者:老姚,《JS正則迷你書》的作者 https://github.com/qdlaoyao/css-gif
本文的每一條,都是我曾經發過的掘金沸點,其中有很多條超過了百贊(竊喜)。鑒于時不時有童鞋翻我以前的沸點,因此,本文收集了個人目前發過的所有CSS知識點動圖,以便閱讀。
需要說明的是,順序仍是按當時發布順序羅列的,還沒有系統的總結,多多包涵。另外這里,老姚謝謝各位一如既往的支持。
希望有所幫助。
也歡迎閱讀本人的《JS正則迷你書》
本文完。
《JS正則迷你書》(https://github.com/qdlaoyao/js-regex-mini-book)
*請認真填寫需求信息,我們會在24小時內與您取得聯系。