原文:github的A collection of useful CSS protips
譯者:愛前端樂分享的FedFun
譯言:提高您CSS開發能力的技巧集,希望對大家有所幫助。
我們通常使用如下代碼給導航條增加間隔線
/* 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;
}
2.2 給body元素增加Line-Height屬性我們不需要給每個p、h1元素設置line-height,只需要給body元素設置,其他文本元素會自動繼承body的特性。
body {
line-height: 1;
}
2.3 任意元素垂直居中不是黑魔法,確實可以讓任意元素垂直居中。
html, body {
height: 100%;
margin: 0;
}
body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}
2.4 逗號分隔的列表讓html列表貌似現實中逗號分隔的列表
ul > li:not(:last-child)::after {
content: ",";
}
2.5 在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;
}
2.6 使用svg圖標沒有理由不使用svg圖標,在大多數解析度和瀏覽器裡能夠實現縮放,甚至兼容到IE9,所以不用再用.png、.gif等等。
.logo {
background: url("logo.svg");
}
2.7 文本顯示優化一些字體不能再所有設備中最優展示,所以需要設置。
html {
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
}
注意optimizeLegibility屬性值的使用問題,同時IE/Edge不支持text-rendering。
使用max-height實現隱藏、顯示的動畫。
.slider ul {
max-height: 0;
overlow: hidden;
}
.slider:hover ul {
max-height: 1000px;
transition: .3s ease;
}
參見本博《Auto值的CSS3 Transition解決方案》
2.9 初始化box-sizing從html中繼承box-sizing屬性,這樣的話,後期維護比較方便。
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
2.10 表格單元格等寬.calendar {
table-layout: fixed;
}
2.11 使用Flexbox擺脫各種Margin Hacks在實現側欄時,我們不再需要各種nth-、first-和last-child等設置margin,可以使用Flexbox輕鬆實現均勻分布。
.list {
display: flex;
justify-content: space-between;
}
.list .person {
flex-basis: 23%;
}
2.12 給空連接使用屬性選擇符對於那些擁有href屬性但是內容為空的a,自動添加內容。
a[href^="http"]:empty::before {
content: attr(href);
}
非常方便,是吧。
版權聲明愛前端,樂分享。前端痴王海慶的博客,希望與您共同進步。
歡迎任何形式的轉載,煩請註明裝載,保留本段文字。
本文原文連結http://blog.csdn.net/whqet/article/details/48997389
獨立博客http://whqet.github.io
新浪微博http://weibo.com/FedFun
極客頭條http://geek.csdn.net/user/publishlist/whqet
●本文編號117,以後想閱讀這篇文章直接輸入117即可。
●輸入m可以獲取到文章目錄
更多推薦請看《15個技術類公眾微信》
涵蓋:程序人生、算法與數據結構、黑客技術與網絡安全、大數據技術、前端開發、Java、Python、Web開發、安卓開發、iOS開發、C/C++、.NET、Linux、資料庫、運維等。傳播計算機學習經驗、推薦計算機優秀資源:點擊前往《值得關注的15個技術類微信公眾號》
點擊閱讀原文,了解野狗