如何創建:按鈕組
學習如何使用 CSS 創建“按鈕組”。
水平按鈕組
在按鈕組中,將一系列按鈕排列在一行上:
如何創建按鈕組
第一步 - 添加 HTML:
<div class="btn-group"> <button>Apple</button> <button>Samsung</button> <button>Sony</button> </div>
第二步 - 添加 CSS:
.btn-group button { background-color: #04AA6D; /* 綠色背景 */ border: 1px solid green; /* 綠色邊框 */ color: white; /* 白色文本 */ padding: 10px 24px; /* 一些內邊距 */ cursor: pointer; /* 鼠標指針/手形圖標 */ float: left; /* 并排浮動按鈕 */ } .btn-group button:not(:last-child) { border-right: none; /* 防止雙邊框 */ } /* 清除浮動(clearfix hack) */ .btn-group:after { content: ""; clear: both; display: table; } /* 鼠標懸停時添加背景顏色 */ .btn-group button:hover { background-color: #3e8e41; }
兩端對齊/全寬按鈕組:
<!-- 三個按鈕為一組 --> <div class="btn-group" style="width:100%"> <button style="width:33.3%">Apple</button> <button style="width:33.3%">Samsung</button> <button style="width:33.3%">Sony</button> </div> <!-- 四個按鈕為一組 --> <div class="btn-group" style="width:100%"> <button style="width:25%">Apple</button> <button style="width:25%">Samsung</button> <button style="width:25%">Sony</button> <button style="width:25%">HTC</button> </div>
相關頁面
教程:CSS 按鈕