Bootstrap lists
In Bootstrap, lists are styled and organized with several predefined classes to enhance the appearance and functionality of both ordered and unordered lists. Bootstrap provides utility classes to style lists in various ways, including removing default styles, making lists responsive, and adding custom icons or badges.
Here’s a detailed breakdown of how lists work in Bootstrap:
1. Basic Unordered List (<ul>
)
Bootstrap applies minimal styling to unordered lists (<ul>
) by default. You can create an unordered list using the basic HTML structure, which will show default bullet points for list items.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
2. Basic Ordered List (<ol>
)
Similar to unordered lists, ordered lists (<ol>
) are styled with default numbering.
<ol>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
</ol>
3. Removing List Styles
To remove the default bullets or numbers from lists, Bootstrap provides the .list-unstyled
class, which removes any markers (bullets or numbers) from the list.
<ul class="list-unstyled">
<li>Item without bullet 1</li>
<li>Item without bullet 2</li>
</ul>
This is useful when you want to format lists like menus, links, or inline items without the default markers.
4. Inline Lists
To create a list where the items appear next to each other in a horizontal line, use the .list-inline
class. Each <li>
should also have the .list-inline-item
class to properly align the items.
<ul class="list-inline">
<li class="list-inline-item">Item 1</li>
<li class="list-inline-item">Item 2</li>
<li class="list-inline-item">Item 3</li>
</ul>
This is useful for navigation bars, tag clouds, or lists of links.
5. List Groups
Bootstrap's list group component provides a more structured way to display a list of items, often used in menus, sidebar navigation, or dynamic content. The list group uses the .list-group
class on the parent <ul>
, <ol>
, or <div>
, and .list-group-item
on each item.
<ul class="list-group">
<li class="list-group-item">List Group Item 1</li>
<li class="list-group-item">List Group Item 2</li>
<li class="list-group-item">List Group Item 3</li>
</ul>
6. Active and Disabled States
You can add an active state to a list group item by applying the .active
class, and a disabled state by using .disabled
.
<ul class="list-group">
<li class="list-group-item active">Active item</li>
<li class="list-group-item">Normal item</li>
<li class="list-group-item disabled">Disabled item</li>
</ul>
7. List Group with Links or Buttons
You can also use list groups with links or buttons for navigation or actions. Use the <a>
or <button>
elements instead of <li>
inside the .list-group
.
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action active">Active link</a>
<a href="#" class="list-group-item list-group-item-action">Link 1</a>
<a href="#" class="list-group-item list-group-item-action">Link 2</a>
</div>
This is particularly useful when you want to create interactive, clickable list items.
8. Contextual Classes in List Groups
Bootstrap offers contextual classes to color the list items, similar to alerts. You can apply these classes to change the background of individual list items:
.list-group-item-primary
.list-group-item-secondary
.list-group-item-success
.list-group-item-danger
.list-group-item-warning
.list-group-item-info
.list-group-item-light
.list-group-item-dark
<ul class="list-group">
<li class="list-group-item list-group-item-primary">Primary item</li>
<li class="list-group-item list-group-item-success">Success item</li>
<li class="list-group-item list-group-item-danger">Danger item</li>
</ul>
9. List Group with Badges
You can add badges to list group items to display numbers, notifications, or other small information. Use the .badge
class alongside the .list-group-item
class to achieve this.
<ul class="list-group">
<li class="list-group-item d-flex justify-content-between align-items-center">
Item 1
<span class="badge bg-primary rounded-pill">14</span>
</li>
<li class="list-group-item d-flex justify-content-between align-items-center">
Item 2
<span class="badge bg-danger rounded-pill">2</span>
</li>
</ul>
10. Flush List Groups
If you want to remove borders and spacing around list group items, use the .list-group-flush
class. This is often used when you want the list to blend seamlessly with other content (e.g., inside a card).
<ul class="list-group list-group-flush">
<li class="list-group-item">No border item 1</li>
<li class="list-group-item">No border item 2</li>
</ul>
11. Horizontal List Groups
You can create horizontal list groups using the .list-group-horizontal
class. This displays the list items in a row, instead of stacking them vertically. This is useful for navigation or toolbars.
<ul class="list-group list-group-horizontal">
<li class="list-group-item">Item 1</li>
<li class="list-group-item">Item 2</li>
<li class="list-group-item">Item 3</li>
</ul>
12. Responsive Horizontal List Groups
To make the list group horizontal only on specific screen sizes, use responsive variants such as .list-group-horizontal-sm
, .list-group-horizontal-md
, .list-group-horizontal-lg
, etc.
<ul class="list-group list-group-horizontal-md">
<li class="list-group-item">Item 1</li>
<li class="list-group-item">Item 2</li>
<li class="list-group-item">Item 3</li>
</ul>
Summary of Bootstrap Lists:
- Basic Lists: Use
<ul>
and<ol>
for unordered and ordered lists. - Removing Styles: Apply
.list-unstyled
to remove bullets or numbers. - Inline Lists: Use
.list-inline
and.list-inline-item
to display items in a row. - List Groups: Create structured lists with
.list-group
and.list-group-item
. - Interactive Lists: Use
<a>
or<button>
for interactive, clickable list items. - Contextual Classes: Apply background colors with
.list-group-item-*
. - Badges: Add badges to list items using
.badge
. - Horizontal Lists: Use
.list-group-horizontal
for side-by-side lists.