1、用all簡寫來指定所有元素的屬性。 將該值設置爲unset會將元素的屬性更改爲其初始值:
button {
all: unset;
}
2、使用 :not() 選擇器來決定表單是否顯示邊框
先爲元素添加邊框
/* 添加邊框 /
.nav li {
border-right: 1px solid #666;
}
爲最後一個元素去除邊框
/ 去掉邊框 /
.nav li:last-child {
border-right:none;
}
不過不要這麼做,使用 :not() 僞類來達到同樣的效果:
.nav li:not(:last-child) {
border-right: 1px solid #666;
}
當然,你也可以使用 .nav li + li,但是 :not() 更加清晰,具有可讀性。
3、爲 body 元素添加行高
不必爲每一個
,<h>元素逐一添加 line-height,直接添加到 body 元素:
body {
line-height: 1.5;
}
文本元素可以很容易地繼承 body 的樣式。
4、垂直居中任何元素
html,
body {
height: 100%;
margin: 0;
}
body {
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-flex;
display: flex;
}
…還有CSS Grid:
body {
display: grid;
height: 100vh;
margin: 0;
place-items: center center;
}
注意: IE11 對 flexbox 的支持有點 bug。
5、.逗號分隔列表
使列表的每項都由逗號分隔:
ul > li:not(:last-child)::after {
content: “,”;
}
因最後一項不加逗號,可以使用 :not() 僞類。
注意: 這一技巧對於無障礙,特別是屏幕閱讀器而言並不理想。而且複製粘貼並不會帶走CSS生成的內容,需要注意。
6、使用負的 nth-child 來選擇元素
使用負的 nth-child 可以選擇1 至 n 個元素。
li {
display: none;
}
/* 選擇第 1 至第 3 個元素並顯示出來 /
li:nth-child(-n+3) {
display: block;
}
或許你已經掌握瞭如何使用 :not()這個技巧,試下這個:
/ 選擇除前3個之外的所有項目,並顯示它們 /
li:not(:nth-child(-n+3)) {
display: none;
}
7、使用 SVG 圖標
沒有理由不使用 SVG 圖標:
.logo {
background: url(“logo.svg”);
}
SVG 在所有分辨率下都可以良好縮放,並且支持所有 IE9 以後的瀏覽器,丟掉你的 .png, .jpg, 或 .gif-jif-whatev 文件吧。
注意: 針對僅有圖標的按鈕,如果 SVG 沒有加載成功的話,以下樣式對無障礙有所幫助:
.no-svg .icon-only::after {
content: attr(aria-label);
}
8、使用 “形似貓頭鷹” 的選擇器
這個名字可能比較陌生,不過通用選擇器 () 和 相鄰兄弟選擇器 (+) 一起使用,效果非凡:
-
-
- {
margin-top: 1.5em;
}
在此示例中,文檔流中的所有的相鄰兄弟元素將都將設置 margin-top: 1.5em的樣式。
9、使用 max-height 來建立純 CSS 的滑塊
max-height 與overflow hidden 一起來建立純 CSS 的滑塊:
.slider {
max-height: 200px;
overflow-y: hidden;
width: 300px;
}
.slider:hover {
max-height: 600px;
overflow-y: scroll;
}
鼠標移入滑塊元素時增大它的 max-height 值,便可以顯示溢出部分。
10、創造格子等寬的表格
table-layout: fixed 可以讓每個格子保持等寬:
.calendar {
table-layout: fixed;
}。
11、利用 Flexbox 去除多餘的外邊距
與其使用 nth-, first-,和 last-child 去除列之間多餘的間隙,不如使用 flexbox 的 space-between 屬性:
.list {
display: flex;
justify-content: space-between;
}
.list .person {
flex-basis: 23%;
}
列之間的間隙總是均勻相等。
12.利用屬性選擇器來選擇空鏈接
當 元素沒有文本內容,但有 href 屬性的時候,顯示它的 href 屬性:
a[href^=“http”]:empty::before {
content: attr(href);
}
13.給 “默認” 鏈接定義樣式:
a[href]:not([class]) {
color: #008000;
text-decoration: underline;
}
14.一致垂直節奏
通用選擇器 (*) 跟元素一起使用,可以保持一致的垂直節奏:
.intro > * {
margin-bottom: 1.25rem;
}
一致的垂直節奏可以提供視覺美感,增強內容的可讀性。
15.固定比例盒子
要創建具有固定比例的一個盒子,所有你需要做的就是給 div 設置一個 padding:
.container {
height: 0;
padding-bottom: 20%;
position: relative;
}
.container div {
border: 2px dashed #ddd;
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
使用20%的padding-bottom使得框等於其寬度的20%的高度。與視口寬度無關,子元素的div將保持其寬高比(100%/ 20%= 5:1)。
16.爲破碎圖象定義樣式
只要一點CSS就可以美化破碎的圖象:
img {
display: block;
font-family: sans-serif;
font-weight: 300;
height: auto;
line-height: 2;
position: relative;
text-align: center;
width: 100%;
}
以添加僞元素的法則來顯示用戶信息和URL的引用:
img::before {
content: “We’re sorry, the image below is broken 😦”;
display: block;
margin-bottom: 10px;
}
img::after {
content: "(url: " attr(src) “)”;
display: block;
font-size: 12px;
}
瞭解更多關於這類樣式的技巧 Ire Aderinokun的 原帖.
17.用 rem 來調整全局大小;用 em 來調整局部大小
在根元素設置基本字體大小後 (html { font-size: 100%; }), 使用 em 設置文本元素的字體大小:
h2 {
font-size: 2em;
}
p {
font-size: 1em;
}
然後設置模塊的字體大小爲 rem:
article {
font-size: 1.25rem;
}
aside .module {
font-size: .9rem;
}
現在,每個模塊變得獨立,更容易、靈活的樣式便於維護。
18、.隱藏沒有靜音、自動播放的影片
這是一個自定義用戶樣式表的不錯的技巧。避免在加載頁面時自動播放。如果沒有靜音,則不顯示視頻:
video[autoplay]:not([muted]) {
display: none;
}
再次,我們利用了 :not() 的優點。
19.使用選擇器:root來控制字體彈性
在響應式佈局中,字體大小應需要根據不同的視口進行調整。你可以計算字體大小根據視口高度的字體大小和寬度,這時需要用到:root:
:root {
font-size: calc(1vw + 1vh + .5vmin);
}
現在,您可以使用 root em
body {
font: 1rem/1.6 sans-serif;
}
20.爲更好的移動體驗,爲表單元素設置字體大小
當觸發的下拉列表時,爲了避免表單元素在移動瀏覽器(IOS Safari 等等)上的縮放,加上font-size:
input[type=“text”],
input[type=“number”],
select,
textarea {
font-size: 16px;
}
21.使用指針事件來控制鼠標事件
指針事件允許您指定鼠標如何與其觸摸的元素進行交互。 要禁用按鈕上的默認指針事件,例如:
.button-disabled {
opacity: .5;
pointer-events: none;
}
- {
-