Stripped personal data from development repository
Samo Penic
2019-02-20 83c3f647c35477564b77cbc5b36d37d793d5442a
commit | author | age
83c3f6 1 ---
SP 2 layout: docs
3 title: List group
4 description: List groups are a flexible and powerful component for displaying a series of content. Modify and extend them to support just about any content within.
5 group: components
6 toc: true
7 ---
8
9 ## Basic example
10
11 The most basic list group is an unordered list with list items and the proper classes. Build upon it with the options that follow, or with your own CSS as needed.
12
13 {% capture example %}
14 <ul class="list-group">
15   <li class="list-group-item">Cras justo odio</li>
16   <li class="list-group-item">Dapibus ac facilisis in</li>
17   <li class="list-group-item">Morbi leo risus</li>
18   <li class="list-group-item">Porta ac consectetur ac</li>
19   <li class="list-group-item">Vestibulum at eros</li>
20 </ul>
21 {% endcapture %}
22 {% include example.html content=example %}
23
24 ## Active items
25
26 Add `.active` to a `.list-group-item` to indicate the current active selection.
27
28 {% capture example %}
29 <ul class="list-group">
30   <li class="list-group-item active">Cras justo odio</li>
31   <li class="list-group-item">Dapibus ac facilisis in</li>
32   <li class="list-group-item">Morbi leo risus</li>
33   <li class="list-group-item">Porta ac consectetur ac</li>
34   <li class="list-group-item">Vestibulum at eros</li>
35 </ul>
36 {% endcapture %}
37 {% include example.html content=example %}
38
39 ## Disabled items
40
41 Add `.disabled` to a `.list-group-item` to make it _appear_ disabled. Note that some elements with `.disabled` will also require custom JavaScript to fully disable their click events (e.g., links).
42
43 {% capture example %}
44 <ul class="list-group">
45   <li class="list-group-item disabled" aria-disabled="true">Cras justo odio</li>
46   <li class="list-group-item">Dapibus ac facilisis in</li>
47   <li class="list-group-item">Morbi leo risus</li>
48   <li class="list-group-item">Porta ac consectetur ac</li>
49   <li class="list-group-item">Vestibulum at eros</li>
50 </ul>
51 {% endcapture %}
52 {% include example.html content=example %}
53
54 ## Links and buttons
55
56 Use `<a>`s or `<button>`s to create _actionable_ list group items with hover, disabled, and active states by adding `.list-group-item-action`. We separate these pseudo-classes to ensure list groups made of non-interactive elements (like `<li>`s or `<div>`s) don't provide a click or tap affordance.
57
58 Be sure to **not use the standard `.btn` classes here**.
59
60 {% capture example %}
61 <div class="list-group">
62   <a href="#" class="list-group-item list-group-item-action active">
63     Cras justo odio
64   </a>
65   <a href="#" class="list-group-item list-group-item-action">Dapibus ac facilisis in</a>
66   <a href="#" class="list-group-item list-group-item-action">Morbi leo risus</a>
67   <a href="#" class="list-group-item list-group-item-action">Porta ac consectetur ac</a>
68   <a href="#" class="list-group-item list-group-item-action disabled" tabindex="-1" aria-disabled="true">Vestibulum at eros</a>
69 </div>
70 {% endcapture %}
71 {% include example.html content=example %}
72
73 With `<button>`s, you can also make use of the `disabled` attribute instead of the `.disabled` class. Sadly, `<a>`s don't support the disabled attribute.
74
75 {% capture example %}
76 <div class="list-group">
77   <button type="button" class="list-group-item list-group-item-action active">
78     Cras justo odio
79   </button>
80   <button type="button" class="list-group-item list-group-item-action">Dapibus ac facilisis in</button>
81   <button type="button" class="list-group-item list-group-item-action">Morbi leo risus</button>
82   <button type="button" class="list-group-item list-group-item-action">Porta ac consectetur ac</button>
83   <button type="button" class="list-group-item list-group-item-action" disabled>Vestibulum at eros</button>
84 </div>
85 {% endcapture %}
86 {% include example.html content=example %}
87
88 ## Flush
89
90 Add `.list-group-flush` to remove some borders and rounded corners to render list group items edge-to-edge in a parent container (e.g., cards).
91
92 {% capture example %}
93 <ul class="list-group list-group-flush">
94   <li class="list-group-item">Cras justo odio</li>
95   <li class="list-group-item">Dapibus ac facilisis in</li>
96   <li class="list-group-item">Morbi leo risus</li>
97   <li class="list-group-item">Porta ac consectetur ac</li>
98   <li class="list-group-item">Vestibulum at eros</li>
99 </ul>
100 {% endcapture %}
101 {% include example.html content=example %}
102
103 ## Contextual classes
104
105 Use contextual classes to style list items with a stateful background and color.
106
107 {% capture example %}
108 <ul class="list-group">
109   <li class="list-group-item">Dapibus ac facilisis in</li>
110
111   {% for color in site.data.theme-colors %}
112   <li class="list-group-item list-group-item-{{ color.name }}">A simple {{ color.name }} list group item</li>{% endfor %}
113 </ul>
114 {% endcapture %}
115 {% include example.html content=example %}
116
117 Contextual classes also work with `.list-group-item-action`. Note the addition of the hover styles here not present in the previous example. Also supported is the `.active` state; apply it to indicate an active selection on a contextual list group item.
118
119 {% capture example %}
120 <div class="list-group">
121   <a href="#" class="list-group-item list-group-item-action">Dapibus ac facilisis in</a>
122
123   {% for color in site.data.theme-colors %}
124   <a href="#" class="list-group-item list-group-item-action list-group-item-{{ color.name }}">A simple {{ color.name }} list group item</a>{% endfor %}
125 </div>
126 {% endcapture %}
127 {% include example.html content=example %}
128
129 {% include callout-warning-color-assistive-technologies.md %}
130
131 ## With badges
132
133 Add badges to any list group item to show unread counts, activity, and more with the help of some [utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/flex/).
134
135 {% capture example %}
136 <ul class="list-group">
137   <li class="list-group-item d-flex justify-content-between align-items-center">
138     Cras justo odio
139     <span class="badge badge-primary badge-pill">14</span>
140   </li>
141   <li class="list-group-item d-flex justify-content-between align-items-center">
142     Dapibus ac facilisis in
143     <span class="badge badge-primary badge-pill">2</span>
144   </li>
145   <li class="list-group-item d-flex justify-content-between align-items-center">
146     Morbi leo risus
147     <span class="badge badge-primary badge-pill">1</span>
148   </li>
149 </ul>
150 {% endcapture %}
151 {% include example.html content=example %}
152
153 ## Custom content
154
155 Add nearly any HTML within, even for linked list groups like the one below, with the help of [flexbox utilities]({{ site.baseurl }}/docs/{{ site.docs_version }}/utilities/flex/).
156
157 {% capture example %}
158 <div class="list-group">
159   <a href="#" class="list-group-item list-group-item-action active">
160     <div class="d-flex w-100 justify-content-between">
161       <h5 class="mb-1">List group item heading</h5>
162       <small>3 days ago</small>
163     </div>
164     <p class="mb-1">Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.</p>
165     <small>Donec id elit non mi porta.</small>
166   </a>
167   <a href="#" class="list-group-item list-group-item-action">
168     <div class="d-flex w-100 justify-content-between">
169       <h5 class="mb-1">List group item heading</h5>
170       <small class="text-muted">3 days ago</small>
171     </div>
172     <p class="mb-1">Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.</p>
173     <small class="text-muted">Donec id elit non mi porta.</small>
174   </a>
175   <a href="#" class="list-group-item list-group-item-action">
176     <div class="d-flex w-100 justify-content-between">
177       <h5 class="mb-1">List group item heading</h5>
178       <small class="text-muted">3 days ago</small>
179     </div>
180     <p class="mb-1">Donec id elit non mi porta gravida at eget metus. Maecenas sed diam eget risus varius blandit.</p>
181     <small class="text-muted">Donec id elit non mi porta.</small>
182   </a>
183 </div>
184 {% endcapture %}
185 {% include example.html content=example %}
186
187 ## JavaScript behavior
188
189 Use the tab JavaScript plugin—include it individually or through the compiled `bootstrap.js` file—to extend our list group to create tabbable panes of local content.
190
191 <div class="bd-example" role="tabpanel">
192   <div class="row">
193     <div class="col-4">
194       <div class="list-group" id="list-tab" role="tablist">
195         <a class="list-group-item list-group-item-action active" id="list-home-list" data-toggle="tab" href="#list-home" role="tab" aria-controls="list-home">Home</a>
196         <a class="list-group-item list-group-item-action" id="list-profile-list" data-toggle="tab" href="#list-profile" role="tab" aria-controls="list-profile">Profile</a>
197         <a class="list-group-item list-group-item-action" id="list-messages-list" data-toggle="tab" href="#list-messages" role="tab" aria-controls="list-messages">Messages</a>
198         <a class="list-group-item list-group-item-action" id="list-settings-list" data-toggle="tab" href="#list-settings" role="tab" aria-controls="list-settings">Settings</a>
199       </div>
200     </div>
201     <div class="col-8">
202       <div class="tab-content" id="nav-tabContent">
203         <div class="tab-pane fade show active" id="list-home" role="tabpanel" aria-labelledby="list-home-list">
204           <p>Velit aute mollit ipsum ad dolor consectetur nulla officia culpa adipisicing exercitation fugiat tempor. Voluptate deserunt sit sunt nisi aliqua fugiat proident ea ut. Mollit voluptate reprehenderit occaecat nisi ad non minim tempor sunt voluptate consectetur exercitation id ut nulla. Ea et fugiat aliquip nostrud sunt incididunt consectetur culpa aliquip eiusmod dolor. Anim ad Lorem aliqua in cupidatat nisi enim eu nostrud do aliquip veniam minim.</p>
205         </div>
206         <div class="tab-pane fade" id="list-profile" role="tabpanel" aria-labelledby="list-profile-list">
207           <p>Cupidatat quis ad sint excepteur laborum in esse qui. Et excepteur consectetur ex nisi eu do cillum ad laborum. Mollit et eu officia dolore sunt Lorem culpa qui commodo velit ex amet id ex. Officia anim incididunt laboris deserunt anim aute dolor incididunt veniam aute dolore do exercitation. Dolor nisi culpa ex ad irure in elit eu dolore. Ad laboris ipsum reprehenderit irure non commodo enim culpa commodo veniam incididunt veniam ad.</p>
208         </div>
209         <div class="tab-pane fade" id="list-messages" role="tabpanel" aria-labelledby="list-messages-list">
210           <p>Ut ut do pariatur aliquip aliqua aliquip exercitation do nostrud commodo reprehenderit aute ipsum voluptate. Irure Lorem et laboris nostrud amet cupidatat cupidatat anim do ut velit mollit consequat enim tempor. Consectetur est minim nostrud nostrud consectetur irure labore voluptate irure. Ipsum id Lorem sit sint voluptate est pariatur eu ad cupidatat et deserunt culpa sit eiusmod deserunt. Consectetur et fugiat anim do eiusmod aliquip nulla laborum elit adipisicing pariatur cillum.</p>
211         </div>
212         <div class="tab-pane fade" id="list-settings" role="tabpanel" aria-labelledby="list-settings-list">
213           <p>Irure enim occaecat labore sit qui aliquip reprehenderit amet velit. Deserunt ullamco ex elit nostrud ut dolore nisi officia magna sit occaecat laboris sunt dolor. Nisi eu minim cillum occaecat aute est cupidatat aliqua labore aute occaecat ea aliquip sunt amet. Aute mollit dolor ut exercitation irure commodo non amet consectetur quis amet culpa. Quis ullamco nisi amet qui aute irure eu. Magna labore dolor quis ex labore id nostrud deserunt dolor eiusmod eu pariatur culpa mollit in irure.</p>
214         </div>
215       </div>
216     </div>
217   </div>
218 </div>
219
220 {% highlight html %}
221 <div class="row">
222   <div class="col-4">
223     <div class="list-group" id="list-tab" role="tablist">
224       <a class="list-group-item list-group-item-action active" id="list-home-list" data-toggle="list" href="#list-home" role="tab" aria-controls="home">Home</a>
225       <a class="list-group-item list-group-item-action" id="list-profile-list" data-toggle="list" href="#list-profile" role="tab" aria-controls="profile">Profile</a>
226       <a class="list-group-item list-group-item-action" id="list-messages-list" data-toggle="list" href="#list-messages" role="tab" aria-controls="messages">Messages</a>
227       <a class="list-group-item list-group-item-action" id="list-settings-list" data-toggle="list" href="#list-settings" role="tab" aria-controls="settings">Settings</a>
228     </div>
229   </div>
230   <div class="col-8">
231     <div class="tab-content" id="nav-tabContent">
232       <div class="tab-pane fade show active" id="list-home" role="tabpanel" aria-labelledby="list-home-list">...</div>
233       <div class="tab-pane fade" id="list-profile" role="tabpanel" aria-labelledby="list-profile-list">...</div>
234       <div class="tab-pane fade" id="list-messages" role="tabpanel" aria-labelledby="list-messages-list">...</div>
235       <div class="tab-pane fade" id="list-settings" role="tabpanel" aria-labelledby="list-settings-list">...</div>
236     </div>
237   </div>
238 </div>
239 {% endhighlight %}
240
241 ### Using data attributes
242
243 You can activate a list group navigation without writing any JavaScript by simply specifying `data-toggle="list"` or on an element. Use these data attributes on `.list-group-item`.
244
245 <div role="tabpanel">
246 {% highlight html %}
247 <!-- List group -->
248 <div class="list-group" id="myList" role="tablist">
249   <a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">Home</a>
250   <a class="list-group-item list-group-item-action" data-toggle="list" href="#profile" role="tab">Profile</a>
251   <a class="list-group-item list-group-item-action" data-toggle="list" href="#messages" role="tab">Messages</a>
252   <a class="list-group-item list-group-item-action" data-toggle="list" href="#settings" role="tab">Settings</a>
253 </div>
254
255 <!-- Tab panes -->
256 <div class="tab-content">
257   <div class="tab-pane active" id="home" role="tabpanel">...</div>
258   <div class="tab-pane" id="profile" role="tabpanel">...</div>
259   <div class="tab-pane" id="messages" role="tabpanel">...</div>
260   <div class="tab-pane" id="settings" role="tabpanel">...</div>
261 </div>
262 {% endhighlight %}
263 </div>
264
265 ### Via JavaScript
266
267 Enable tabbable list item via JavaScript (each list item needs to be activated individually):
268
269 {% highlight js %}
270 $('#myList a').on('click', function (e) {
271   e.preventDefault()
272   $(this).tab('show')
273 })
274 {% endhighlight %}
275
276 You can activate individual list item in several ways:
277
278 {% highlight js %}
279 $('#myList a[href="#profile"]').tab('show') // Select tab by name
280 $('#myList a:first-child').tab('show') // Select first tab
281 $('#myList a:last-child').tab('show') // Select last tab
282 $('#myList a:nth-child(3)').tab('show') // Select third tab
283 {% endhighlight %}
284
285 ### Fade effect
286
287 To make tabs panel fade in, add `.fade` to each `.tab-pane`. The first tab pane must also have `.show` to make the initial content visible.
288
289 {% highlight html %}
290 <div class="tab-content">
291   <div class="tab-pane fade show active" id="home" role="tabpanel">...</div>
292   <div class="tab-pane fade" id="profile" role="tabpanel">...</div>
293   <div class="tab-pane fade" id="messages" role="tabpanel">...</div>
294   <div class="tab-pane fade" id="settings" role="tabpanel">...</div>
295 </div>
296 {% endhighlight %}
297
298 ### Methods
299
300 #### $().tab
301
302 Activates a list item element and content container. Tab should have either a `data-target` or an `href` targeting a container node in the DOM.
303
304 {% highlight html %}
305 <div class="list-group" id="myList" role="tablist">
306   <a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab">Home</a>
307   <a class="list-group-item list-group-item-action" data-toggle="list" href="#profile" role="tab">Profile</a>
308   <a class="list-group-item list-group-item-action" data-toggle="list" href="#messages" role="tab">Messages</a>
309   <a class="list-group-item list-group-item-action" data-toggle="list" href="#settings" role="tab">Settings</a>
310 </div>
311
312 <div class="tab-content">
313   <div class="tab-pane active" id="home" role="tabpanel">...</div>
314   <div class="tab-pane" id="profile" role="tabpanel">...</div>
315   <div class="tab-pane" id="messages" role="tabpanel">...</div>
316   <div class="tab-pane" id="settings" role="tabpanel">...</div>
317 </div>
318
319 <script>
320   $(function () {
321     $('#myList a:last-child').tab('show')
322   })
323 </script>
324 {% endhighlight %}
325
326 #### .tab('show')
327
328 Selects the given list item and shows its associated pane. Any other list item that was previously selected becomes unselected and its associated pane is hidden. **Returns to the caller before the tab pane has actually been shown** (for example, before the `shown.bs.tab` event occurs).
329
330 {% highlight js %}
331 $('#someListItem').tab('show')
332 {% endhighlight %}
333
334 ### Events
335
336 When showing a new tab, the events fire in the following order:
337
338 1. `hide.bs.tab` (on the current active tab)
339 2. `show.bs.tab` (on the to-be-shown tab)
340 3. `hidden.bs.tab` (on the previous active tab, the same one as for the `hide.bs.tab` event)
341 4. `shown.bs.tab` (on the newly-active just-shown tab, the same one as for the `show.bs.tab` event)
342
343 If no tab was already active, the `hide.bs.tab` and `hidden.bs.tab` events will not be fired.
344
345 <table class="table table-bordered table-striped">
346   <thead>
347     <tr>
348       <th style="width: 150px;">Event type</th>
349       <th>Description</th>
350     </tr>
351   </thead>
352   <tbody>
353     <tr>
354       <td>show.bs.tab</td>
355       <td>This event fires on tab show, but before the new tab has been shown. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab (if available) respectively.</td>
356     </tr>
357     <tr>
358       <td>shown.bs.tab</td>
359       <td>This event fires on tab show after a tab has been shown. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab (if available) respectively.</td>
360     </tr>
361     <tr>
362       <td>hide.bs.tab</td>
363       <td>This event fires when a new tab is to be shown (and thus the previous active tab is to be hidden). Use <code>event.target</code> and <code>event.relatedTarget</code> to target the current active tab and the new soon-to-be-active tab, respectively.</td>
364     </tr>
365     <tr>
366       <td>hidden.bs.tab</td>
367       <td>This event fires after a new tab is shown (and thus the previous active tab is hidden). Use <code>event.target</code> and <code>event.relatedTarget</code> to target the previous active tab and the new active tab, respectively.</td>
368     </tr>
369   </tbody>
370 </table>
371
372 {% highlight js %}
373 $('a[data-toggle="list"]').on('shown.bs.tab', function (e) {
374   e.target // newly activated tab
375   e.relatedTarget // previous active tab
376 })
377 {% endhighlight %}