<select> 是**替換元素**,內部結構(下拉箭頭、選單面板)由瀏覽器原生渲染,CSS 控制力極弱。各瀏覽器渲染差異巨大,統一樣式歷來是前端痛點。
<div class="select-border">
<div class="container">
<select class="select">
<option>北京</option>
<option>天津</option>
<option>上海</option>
<option>重慶</option>
</select>
</div>
</div>
.select-border {
width: 59px; height: 29px;
background: #fff;
border: 1px solid #09f;
padding: 4px;
}
.container {
border: 0; position: relative; height: 18px; overflow: hidden;
}
.select {
border: 1px solid #09f;
padding: 4px 3px;
font-size: 12px;
height: 30px; width: 60px;
margin: -1px; /* 抵消邊框,對齊容器 */
}
* html .select { width: 52px; } /* IE6 hack */
* html .select-border, * html .container { width: 50px; }
.select-border 提供統一視覺邊框.container overflow: hidden 裁切原生下拉箭頭.select 寬度略大於容器,原生箭頭被裁切隱藏margin 微調對齊.select-modern {
appearance: none; /* 移除原生樣式 */
-webkit-appearance: none;
-moz-appearance: none;
background: #fff url(arrow.svg) no-repeat right 12px center / 16px;
padding-right: 40px; /* 留出箭頭空間 */
border: 1px solid #09f;
border-radius: 4px;
font-size: 14px;
height: 36px;
width: 100%;
}
.select-modern::-ms-expand { display: none; } /* IE10/11 隱藏原生箭頭 */
當設計需求超越原生能力(分組、搜尋、多選、動畫、虛擬列表),**唯有自定義組件**:
<button> + <ul> + ARIA 屬性構建| 瀏覽器 | 推薦方案 | 關鍵技術 |
|---|---|---|
| IE6-8 | 容器模擬法 | overflow:hidden 裁切、IE Hack |
| IE9 | 容器模擬法 / appearance 部分支援 | 條件註解區分 |
| IE10-11 | appearance: none + ::-ms-expand | 自訂背景箭頭 |
| Edge (Legacy) | appearance: none | 標準語法 |
| Chrome/Firefox/Safari/Edge (Chromium) | appearance: none + 自訂箭頭 | 標準語法、SVG 箭頭 |
role="combobox" + aria-expanded + aria-controlsrole="option" + aria-selectedSelect 樣式統一是「相容性 vs 設計自由度」的權衡。簡單專案用「容器模擬法」或「appearance 重置」;複雜需求直接上成熟無障礙組件庫。**切勿嘗試用 CSS 強行改造原生 Select 內部結構**——那是瀏覽器的黑盒,隨版本更新隨時失效。