CSS 小技巧 & 滾動條樣式更改

1.使用CSS復位

CSS復位可以在不同的瀏覽器上保持一致的樣式風格。您可以使用CSS reset 庫Normalize等,也可以使用一個更簡化的復位方法:

*,
*::before,
*::after {
 box-sizing: border-box;
 margin: 0;
 padding: 0;
}

現在元素的 marginpadding 已爲0,box-sizing可以管理您的CSS盒模型佈局。
注意:如果你遵循接下來繼承 box-sizing講解的這個技巧, 你不需要在以上代碼中添加 box-sizing 屬性。

2.繼承 box-sizing

從 html 元素繼承box-sizing

html {
 box-sizing: border-box;
}
 
*,
*::before,
*::after {
 box-sizing: inherit;
}

如此在插件或其它組件裏改變 box-sizing 變得簡單。

3.使用unset而不是重置所有屬性

重置元素的屬性時,不需要重置每個單獨的屬性:

button {
 background: none;
 border: none;
 color: inherit;
 font: inherit;
 outline: none;
 padding: 0;
}

你可以用all簡寫來指定所有元素的屬性。 將該值設置爲unset會將元素的屬性更改爲其初始值:

button {
 all: unset;
}

注意: 所有速記在IE11中不被支持,目前正在考慮Edge的支持。 IE11不支持unset

4.使用 :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() 更加清晰,具有可讀性。

5.爲 body 元素添加行高

不必爲每一個 <p><h*>元素逐一添加 line-height,直接添加到 body 元素:

body {
 line-height: 1.5;
}

文本元素可以很容易地繼承 body 的樣式。

6.爲表單元素設置:focus

有視力的鍵盤用戶依靠焦點來確定鍵盤事件在頁面中的位置。 使表單元素的焦點脫穎而出,然後與瀏覽器的默認實現保持一致:

a:focus,
button:focus,
input:focus,
select:focus,
textarea:focus {
 box-shadow: none;
 outline: #000 dotted 2px;
 outline-offset: .05em;
}

7.垂直居中任何元素

不!這絕不是黑魔法,真的可以垂直居中任何元素:

html,
body {
 height: 100%;
 margin: 0;
}
 
body {
 display: -webkit-flex;
 display: flex;
 justify-content: center;
  -webkit-align-items: center;
 -ms-flex-align: center;
 align-items: center;
}

…還有CSS Grid:

body {
 display: grid;
 height: 100vh;
 margin: 0;
 place-items: center center;
}

這還不夠?垂直方向,水平方向?任何元素,任何時間,任何地點?CSS-Tricks 有篇好文講到了各種居中的技巧。
注意: IE11 對 flexbox 的支持有點 bug

8.逗號分隔列表

使列表的每項都由逗號分隔:

ul > li:not(:last-child)::after {
 content: ",";
}

因最後一項不加逗號,可以使用 :not() 僞類。
注意: 這一技巧對於無障礙,特別是屏幕閱讀器而言並不理想。而且複製粘貼並不會帶走CSS生成的內容,需要注意。

9.使用負的 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;
}

如此簡單!

10.使用 SVG 圖標

沒有理由不使用 SVG 圖標:

.logo {
 background: url("logo.svg");
}

SVG 在所有分辨率下都可以良好縮放,並且支持所有 IE9 以後的瀏覽器,丟掉你的 .png, .jpg, 或 .gif-jif-whatev 文件吧。
注意: 針對僅有圖標的按鈕,如果 SVG 沒有加載成功的話,以下樣式對無障礙有所幫助:

.no-svg .icon-only::after {
 content: attr(aria-label);
}

11.使用 “形似貓頭鷹” 的選擇器

這個名字可能比較陌生,不過通用選擇器 (*) 和 相鄰兄弟選擇器 (+) 一起使用,效果非凡:

* + * {
 margin-top: 1.5em;
}

在此示例中,文檔流中的所有的相鄰兄弟元素將都將設置 margin-top: 1.5em的樣式。
更多 “形似貓頭鷹” 的選擇器,可參考 A List Apart 上面 Heydon Pickering 的文章

12.使用 max-height 來建立純 CSS 的滑塊

max-heightoverflow: hidden 一起來建立純 CSS 的滑塊:

.slider {
 max-height: 200px;
 overflow-y: hidden;
 width: 300px;
}
 
.slider:hover {
 max-height: 600px;
 overflow-y: scroll;
}

鼠標移入滑塊元素時增大它的 max-height 值,便可以顯示溢出部分。

13.創造格子等寬的表格

table-layout: fixed 可以讓每個格子保持等寬:

.calendar {
 table-layout: fixed;
}

無痛的 table 佈局。

14.利用 Flexbox 去除多餘的外邊距

與其使用 nth-, first-,和 last-child 去除列之間多餘的間隙,不如使用 flexboxspace-between 屬性:

.list {
 display: flex;
 justify-content: space-between;
}
 
.list .person {
 flex-basis: 23%;
}

列之間的間隙總是均勻相等。

15.利用屬性選擇器來選擇空鏈接

<a> 元素沒有文本內容,但有 href 屬性的時候,顯示它的 href 屬性:

a[href^="http"]:empty::before {
 content: attr(href);
}

相當簡便。

16.給 “默認” 鏈接定義樣式

給 “默認” 鏈接定義樣式:

a[href]:not([class]) {
 color: #008000;
 text-decoration: underline;
}

通過 CMS 系統插入的鏈接,通常沒有class 屬性,以上樣式可以甄別它們,而且不會影響其它樣式。

17.一致垂直節奏

通用選擇器 (*) 跟元素一起使用,可以保持一致的垂直節奏:

.intro > * {
 margin-bottom: 1.25rem;
}

一致的垂直節奏可以提供視覺美感,增強內容的可讀性。

18.固定比例盒子

要創建具有固定比例的一個盒子,所有你需要做的就是給 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)。

19.爲破碎圖象定義樣式

只要一點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的 原帖.

20.用 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;
}

現在,每個模塊變得獨立,更容易、靈活的樣式便於維護。

21.隱藏沒有靜音、自動播放的影片

這是一個自定義用戶樣式表的不錯的技巧。避免在加載頁面時自動播放。如果沒有靜音,則不顯示視頻:

video[autoplay]:not([muted]) {
 display: none;
}

再次,我們利用了 :not() 的優點。

22.使用選擇器:root來控制字體彈性

在響應式佈局中,字體大小應需要根據不同的視口進行調整。你可以計算字體大小根據視口高度的字體大小和寬度,這時需要用到:root:

:root {
 font-size: calc(1vw + 1vh + .5vmin);
}

現在,您可以使用 root em

body {
 font: 1rem/1.6 sans-serif;
}

23.爲更好的移動體驗,爲表單元素設置字體大小

當觸發<select>的下拉列表時,爲了避免表單元素在移動瀏覽器(IOS Safari 等等)上的縮放,加上font-size

input[type="text"],
input[type="number"],
select,
textarea {
 font-size: 16px;
}
:dancer:

24.使用指針事件來控制鼠標事件

指針事件允許您指定鼠標如何與其觸摸的元素進行交互。 要禁用按鈕上的默認指針事件,例如:

.button-disabled {
 opacity: .5;
 pointer-events: none;
}

25、滾動條樣式更改

/*縱向滾動條整體部分,必須要設置*/
::-webkit-scrollbar{
  width: 4px;
  height: 4px;
  display: none;
}
/*滾動條的軌道*/
::-webkit-scrollbar-track{
  background-color: #EEEDF0;
}
/*滾動條的滑塊按鈕*/
::-webkit-scrollbar-thumb{
  background-color: #D5C9FF;
}
/*滾動條的上下兩端的按鈕*/
::-webkit-scrollbar-button{
  height: 0px;
}
:hover::-webkit-scrollbar{
  display: block; 
}

支持情況
這些技巧適用於最新版的 Chrome, Firefox, Safari, Opera,Edge, 以及 IE11。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章