Badges show small pieces of information like counts or labels. They help highlight important details quickly.
Badge component in Bootsrap
Introduction
Syntax
Bootsrap
<span class="badge bg-primary">Text</span>
Use
span with class badge and a background color class like bg-primary.Badges can be placed inside buttons, links, or text to show extra info.
Examples
Bootsrap
<span class="badge bg-secondary">New</span>
Bootsrap
<button type="button" class="btn btn-primary">Inbox <span class="badge bg-light text-dark">4</span></button>
Bootsrap
<a href="#" class="badge bg-success">Active</a>
Sample Program
This page shows three examples of badges: a red badge next to 'Messages' showing 5, a yellow badge next to 'Notifications' showing 3, and a button labeled 'Cart' with a light badge showing 2.
Bootsrap
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Badge Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <main class="p-4"> <h1>Messages <span class="badge bg-danger">5</span></h1> <p>Notifications <span class="badge bg-warning text-dark">3</span></p> <button type="button" class="btn btn-primary"> Cart <span class="badge bg-light text-dark">2</span> </button> </main> </body> </html>
Important Notes
Badges should have good color contrast for readability.
Use bg- classes for background colors and add text-dark if the background is light.
Badges can be used inside buttons, links, or plain text for flexible UI.
Summary
Badges highlight small bits of info like counts or labels.
Use span with badge and color classes for styling.
They work well inside buttons, links, or text for clear visual cues.
Practice
1. What is the main purpose of a Bootstrap
badge component?easy
Solution
Step 1: Understand the badge role
Badges are used to show small bits of info such as counts or labels next to text or buttons.Step 2: Compare options
Options A, B, and C describe other Bootstrap components or uses, not badges.Final Answer:
To highlight small pieces of information like counts or labels -> Option DQuick Check:
Badge = small info highlight [OK]
Hint: Badges show small info like numbers or labels [OK]
Common Mistakes:
- Confusing badges with navigation or headers
- Thinking badges style large elements
- Using badges for form inputs
2. Which of the following is the correct syntax to create a Bootstrap badge with a primary color?
easy
Solution
Step 1: Check Bootstrap badge syntax
Bootstrap badges use aspanwith classbadgeplus a background color class likebg-primary.Step 2: Validate each option
New uses correct tags and classes. Options A, B, and D use incorrect tags or class names.Final Answer:
<span class="badge bg-primary">New</span> -> Option CQuick Check:
Usespan.badge.bg-primary[OK]
Hint: Use <span> with classes badge and bg-color [OK]
Common Mistakes:
- Using <div> instead of <span>
- Missing the 'badge' class
- Using wrong class names like badge-primary
3. What will be the visual result of this code?
<button type="button" class="btn btn-secondary">Messages <span class="badge bg-danger">5</span></button>
medium
Solution
Step 1: Analyze button and badge classes
The button usesbtn btn-secondarywhich styles it gray. The badge usesbadge bg-dangerwhich styles it red.Step 2: Understand visual output
The button text is 'Messages' and the badge with number 5 appears in red next to it.Final Answer:
A secondary button labeled 'Messages' with a red badge showing number 5 -> Option AQuick Check:
btn-secondary + badge bg-danger = gray button + red badge [OK]
Hint: bg-danger means red badge color [OK]
Common Mistakes:
- Confusing button and badge colors
- Expecting badge to be blue or missing
- Ignoring badge inside button
4. Identify the error in this badge code:
<span class="badge badge-primary">99</span>
medium
Solution
Step 1: Check Bootstrap badge color class
In Bootstrap 5+, color classes for badges usebg-primary, notbadge-primary.Step 2: Validate other parts
Thespantag is correct, number size is fine, and the closing tag is present.Final Answer:
The class 'badge-primary' is outdated; should use 'bg-primary' instead -> Option BQuick Check:
Use bg-primary, not badge-primary [OK]
Hint: Use bg-primary for badge color, not badge-primary [OK]
Common Mistakes:
- Using old Bootstrap 4 class names
- Thinking span is invalid tag
- Assuming number size matters
5. You want to add a badge inside a navigation link that shows the number of unread notifications. Which is the best way to do this for accessibility and styling?
<a href="#" class="nav-link">Notifications <?></a>What should replace
<?>?hard
Solution
Step 1: Choose correct tag and classes
Badges should usespanwithbadgeand color class likebg-info.Step 2: Add accessibility attribute
Addingaria-labeldescribes the badge meaning for screen readers, improving accessibility.Final Answer:
<span class="badge bg-info" aria-label="unread notifications">3</span> -> Option AQuick Check:
Use span.badge with aria-label for accessibility [OK]
Hint: Use aria-label on badge span for screen readers [OK]
Common Mistakes:
- Using div inside links (invalid HTML)
- Adding role=button unnecessarily
- Not adding aria-label for accessibility
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
