按鈕是 UI 元件庫裡最基礎也最高频的一種。這個範例走的是 Google+ 那種簡約風格——之所以偏好這種設計,是因為它在不同瀏覽器之間的視覺落差最小,不必為了補 IE 的窟窿寫一堆 hack。以下程式碼建議用標準瀏覽器預覽。
.mt { margin-top: 20px; }
.button {
display: inline-block;
*display: inline; /* IE6/7 hack */
zoom: 1;
padding: 6px 20px;
margin: 0;
cursor: pointer;
border: 1px solid #bbb;
overflow: visible;
font: bold 13px arial, helvetica, sans-serif;
text-decoration: none;
white-space: nowrap;
color: #555;
background-color: #ddd;
background-image: linear-gradient(top, rgba(255,255,255,1), rgba(255,255,255,0));
-webkit-transition: background-color .2s ease-out;
-moz-transition: background-color .2s ease-out;
background-clip: padding-box; /* 修正邊角溢出 */
border-radius: 3px;
box-shadow: 0 1px 0 rgba(0,0,0,.3),
0 2px 2px -1px rgba(0,0,0,.5),
0 1px 0 rgba(255,255,255,.3) inset;
text-shadow: 0 1px 0 rgba(255,255,255,.9);
}
.button:hover { background-color: #eee; color: #333; }
.button:active {
background: #e9e9e9;
position: relative;
top: 1px;
text-shadow: none;
box-shadow: 0 1px 1px rgba(0,0,0,.3) inset;
}
.button.small { padding: 4px 12px; }
.button.large { padding: 12px 30px; text-transform: uppercase; }
.button.large:active { top: 2px; }
.button.green, .button.red, .button.blue {
color: #FFF;
text-shadow: 0 1px 0 rgba(0,0,0,.5);
background-image: -webkit-linear-gradient(top, rgba(255,255,255,.3), rgba(255,255,255,0));
background-image: -moz-linear-gradient(top, rgba(255,255,255,.3), rgba(255,255,255,0));
background-image: -o-linear-gradient(top, rgba(255,255,255,.3), rgba(255,255,255,0));
background-image: linear-gradient(top, rgba(255,255,255,.3), rgba(255,255,255,0));
}
.button.green { background-color:#57a957; border-color:#57a957; }
.button.green:hover { background-color:#62c462; }
.button.green:active { background:#57a957; }
.button.red { background-color:#c43c35; border-color:#c43c35; }
.button.red:hover { background-color:#ee5f5b; }
.button.red:active { background:#c43c35; }
.button.blue { background-color:#269CE9; border-color:#269CE9; }
.button.blue:hover { background-color:#70B9E8; }
.button.blue:active { background:#269CE9; }
.button[disabled], .button[disabled]:hover, .button[disabled]:active {
border-color:#eaeaea; background:#fafafa;
cursor: default; position: static; color: #999;
/* 這裡的 !important 是刻意的:需要蓋掉所有狀態 */
box-shadow: none !important;
text-shadow: none !important;
}
.green[disabled], .green[disabled]:hover, .green[disabled]:active {
border-color:#57A957; background:#57A957; color:#D2FFD2;
}
.red[disabled], .red[disabled]:hover, .red[disabled]:active {
border-color:#C43C35; background:#C43C35; color:#FFD3D3;
}
.blue[disabled], .blue[disabled]:hover, .blue[disabled]:active {
border-color:#269CE9; background:#269CE9; color:#93D5FF;
}
禁用態刻意把三個狀態寫全,是因為 :hover 與 :active 的優先權重足以蓋掉基本樣式,不這樣寫滑鼠移過去還是會變色。
.button-group, .button-group li {
display: inline-block; *display: inline; zoom: 1;
}
.button-group {
font-size: 0; /* 消除 inline-block 之間的空白縫隙 */
margin: 0; padding: 7px;
background: #f1f1f1;
border-bottom: 1px solid rgba(0,0,0,.07);
border-radius: 7px;
}
.button-group li { margin-right: -1px; } /* 讓相鄰邊框重疊 */
.button-group .button { font-size: 13px; border-radius: 0; }
.button-group .button:active {
box-shadow: 0 0 1px rgba(0,0,0,.2) inset,
5px 0 5px -3px rgba(0,0,0,.2) inset,
-5px 0 5px -3px rgba(0,0,0,.2) inset;
}
.button-group li:first-child .button { border-radius: 3px 0 0 3px; }
.button-group li:first-child .button:active {
box-shadow: 0 0 1px rgba(0,0,0,.2) inset, -5px 0 5px -3px rgba(0,0,0,.2) inset;
}
.button-group li:last-child .button { border-radius: 0 3px 3px 0; }
.button-group li:last-child .button:active {
box-shadow: 0 0 1px rgba(0,0,0,.2) inset, 5px 0 5px -3px rgba(0,0,0,.2) inset;
}
font-size:0 這招值得記住:inline-block 元素之間的換行會被渲染成空白,把父層字級歸零再於子元素還原,是最簡潔的解法。
<button class="small button">Button</button> <button class="button">Button</button> <button class="large button">Button</button> <div class="mt"> <button class="button">Button</button> <button class="button green">Button</button> <button class="button red">Button</button> <button class="button blue">Button</button> </div> <div class="mt"> <button class="button" disabled="disabled">Button</button> <button class="button green" disabled="disabled">Button</button> </div> <ul class="button-group"> <li><button class="button">Button1</button></li> <li><button class="button green">Button2</button></li> <li><button class="button red">Button3</button></li> <li><button class="button blue">Button4</button></li> </ul>
整套設計的關鍵是「用多重 box-shadow 模擬立體感」:上方一條淺色內陰影做出高光,下方一條深色外陰影做出厚度,按下時反過來改成內陰影並下移 1px,就有真實的按壓回饋。