列表是內容型頁面用得最频繁的結構之一。Bootstrap 在原生 ul / ol / dl 之上提供了幾個實用的修飾類別,本文逐一說明並附上原始碼。
列表類型總覽
普通列表 ul li
有序列表 ol li
去點列表 .list-unstyled
內聯列表 .list-inline
定義列表 dl dt dd
水平定義列表 .dl-horizontal
先看 Bootstrap 對列表的基本重置:
ul, ol { margin-top: 0; margin-bottom: 10px; } ul ul, ol ul, ul ol, ol ol { margin-bottom: 0; }
這段處理了兩件事:統一上下邊距,並讓巢狀列表不要多留底部間距,避免層層堆疊後間距失控。
移除項目符號與左側內邊距:
//源碼 .list-unstyled { padding-left: 0; list-style: none; }
讓列表項橫向排列:
//源碼 .list-inline { padding-left: 0; margin-left: -5px; list-style: none; } .list-inline > li { display: inline-block; padding-right: 5px; padding-left: 5px; }
注意容器上的 margin-left: -5px,它抵消了第一個項目左側的 5px 內距,讓首項文字能與周圍內容左緣對齊。
dl { margin-top: 0; margin-bottom: 20px; } dt, dd { line-height: 1.42857143; } dt { font-weight: bold; } dd { margin-left: 0; }
dd 預設有較大的左縮排,Bootstrap 直接歸零,改由 .dl-horizontal 統一控制。

它的做法是讓 dt 左浮動並固定寬度,dd 用對應的 margin-left 讓出空間:
@media (min-width: 768px) { .dl-horizontal dt { float: left; width: 160px; overflow: hidden; clear: left; text-align: right; //顯示省略符號來代表被修剪的文本 text-overflow: ellipsis; white-space: nowrap; } .dl-horizontal dd { margin-left: 180px; } }
幾個細節值得留意:包在 @media (min-width: 768px) 裡,代表窄螢幕上會退回普通的上下堆疊;dt 的寬度 160px 加上 dd 的 180px 讓出 20px 間距;text-overflow: ellipsis 搭配 white-space: nowrap,標題過長時會自動變成省略號而不撐破版面。
以上就是 Bootstrap 列表樣式的整理,希望對你的學習有所幫助。