後台廣告投放需在 31 個省份中勾選,原有單純列表存在三大問題:

<label> 承載交互,樣式化為標籤卡片order 屬性實現無縫分組切換/* HTML 結構:input + label 同級 */
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
#container input,
#container label {
order: 2; /* 預設在分割線之後 */
}
input[type="checkbox"]:checked,
input[type="checkbox"]:checked + label {
order: 0; /* 選中項移至最前 */
}
hr {
display: none;
order: 1; /* 位於已選與未選之間 */
width: 100%;
}
input[type="checkbox"]:checked ~ hr {
display: block; /* 有選中項時顯示分割線 */
}
核心選擇器:
+ 相鄰兄弟選擇器:選中 input 後緊鄰的 label~ 通用兄弟選擇器:選中 input 之後的所有 hrinput[type="checkbox"] { display: none; }
label {
min-width: 120px;
border: 1px solid #ccc;
padding: 2px 8px;
text-align: center;
margin: 0 5px 5px 0;
background: #fff;
color: #333;
border-radius: 3px;
box-sizing: border-box;
cursor: pointer;
transition: all 0.2s;
}
label:hover { border-color: #adadad; background: #ebebeb; }
input:checked + label {
order: 0;
background: #5cb85c;
border-color: #4cae4c;
color: #fff;
}
input:checked + label:hover {
background: #47a447;
border-color: #398439;
}
@import url("//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css");
label::before {
font-family: "FontAwesome";
margin-right: 6px;
content: "\f096"; /* 空心方框 */
}
input:checked + label::before {
content: "\f046"; /* 勾選方框 */
animation: pop 0.3s ease;
}
@keyframes pop {
0% { transform: scale(0); }
50% { transform: scale(1.2); }
100% { transform: scale(1); }
}
嘗試對 order 屬性添加 transition 實現平滑移動,但 Chrome 30 仍不支援(規范雖標註 animatable: yes,實際實作滯後)。需等待瀏覽器更新。
@supports 或 Modernizr 做漸進增強Flexbox 的 order 配合 CSS3 選擇器,以極低成本實現了複雜的交互邏輯。這證明了現代 CSS 已具備處理「狀態驅動佈局」的能力,減少了 JS 依賴,提升了效能與維護性。期待 order 動畫支援普及後,體驗更上一層樓。