📖 Scenario: You are creating a simple webpage to show a notification badge next to a message. Badges are small count indicators often used in apps to show new messages or alerts.
🎯 Goal: Build a webpage that displays a message with a Bootstrap badge showing the number 5 as a notification count.
📋 What You'll Learn
Use Bootstrap 5 classes to create a badge
Display the badge next to the text 'Messages'
Use the badge color bg-primary
Include the correct HTML5 document structure with lang, charset, and viewport meta tags
Make sure the badge is accessible with appropriate semantic HTML
💡 Why This Matters
🌍 Real World
Badges are commonly used in websites and apps to show notifications, message counts, or status indicators.
💼 Career
Knowing how to use Bootstrap badges helps you quickly build user interfaces that communicate important information clearly and accessibly.
Progress0 / 4 steps
1
Create the basic HTML structure
Create a basic HTML5 document with lang="en", charset="UTF-8", and a viewport meta tag. Add a <main> element inside the <body>.
Bootsrap
Hint
Remember to include the lang attribute in the <html> tag and the meta tags inside <head>.
2
Add Bootstrap CSS link
Inside the <head>, add the Bootstrap 5 CSS CDN link: https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css using a <link> tag with rel="stylesheet".
Bootsrap
Hint
Use a <link> tag with rel="stylesheet" and the exact href URL for Bootstrap CSS.
3
Add the message text with a badge
Inside the <main> element, add a <span> with the text Messages. Next to it, add a Bootstrap badge using a <span> with classes badge and bg-primary that displays the number 5.
Bootsrap
Hint
Use two <span> elements: one for the text and one for the badge with classes badge and bg-primary.
4
Make the badge accessible and visually clear
Add an aria-label="5 new messages" attribute to the badge <span> to improve accessibility. Also, wrap the message and badge inside a <div> with class d-inline-flex align-items-center gap-2 to align them nicely.
Bootsrap
Hint
Use a <div> with Bootstrap flex classes to align items and add aria-label to the badge for screen readers.
Practice
(1/5)
1. What is the main purpose of a Bootstrap badge component?
easy
A. To style form input fields
B. To create large banners for page headers
C. To add navigation menus
D. To highlight small pieces of information like counts or labels
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 D
Quick 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
A. New
B.
New
C. New
D. New
Solution
Step 1: Check Bootstrap badge syntax
Bootstrap badges use a span with class badge plus a background color class like bg-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 C
Quick Check:
Use span.badge.bg-primary [OK]
Hint: Use <span> with classes badge and bg-color [OK]
B. The class 'badge-primary' is outdated; should use 'bg-primary' instead
C. The number 99 is too large for a badge
D. Missing closing tag for span
Solution
Step 1: Check Bootstrap badge color class
In Bootstrap 5+, color classes for badges use bg-primary, not badge-primary.
Step 2: Validate other parts
The span tag 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 B
Quick 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?