search
HomeWeb Front-endBootstrap TutorialA brief analysis of how to use the badge icon component in Bootstrap

How to use the badge icon component in

Bootstrap? The following article will explain the usage of the Bootstrap5 Badge badge icon component through code examples. I hope it will be helpful to you!

A brief analysis of how to use the badge icon component in Bootstrap

1 Structure of Badge

Badges are usually used in a small area on the right side of the navigation bar, title, button, and avatar for counting. (such as n unread messages) or identify new releases, popular hots, etc. By using relative font sizes and em units, the badge can be scaled to match the size of the immediate parent element. Starting with Bootstrap 5, badges no longer have link focus or hover styles. [Related recommendation: "bootstrap tutorial"]

The structure of the badge is very simple, it is a span tag, which contains two classes. badge indicates that it is a badge, bg-* is the background color. You can also use text-* to set the font color.

 <span class="badge bg-secondary">文字内容</span>

2 When the badge is used for text

When the badge is used for text, it will automatically adjust the size to match the text.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="../bootstrap5/bootstrap.min.css" rel="stylesheet">
    <title>徽章</title>
  </head>
  <body>
    <div>
      <br><br><br>
        <h1 id="Example-nbsp-heading-nbsp-span-nbsp-class-badge-nbsp-bg-info-nbsp-New-span">Example heading <span class="badge bg-info ">New</span></h1>
        <h2 id="Example-nbsp-heading-nbsp-span-nbsp-class-badge-nbsp-bg-danger-nbsp-Hot-span">Example heading <span class="badge bg-danger ">Hot</span></h2>
        <h3 id="Example-nbsp-heading-nbsp-span-nbsp-class-badge-nbsp-bg-info-nbsp-New-span">Example heading <span class="badge bg-info ">New</span></h3>
        <h4 id="Example-nbsp-heading-nbsp-span-nbsp-class-badge-nbsp-bg-info-nbsp-New-span">Example heading <span class="badge bg-info ">New</span></h4>
        <h5 id="Example-nbsp-heading-nbsp-span-nbsp-class-badge-nbsp-bg-info-nbsp-New-span">Example heading <span class="badge bg-info ">New</span></h5>
        <h6 id="Example-nbsp-heading-nbsp-span-nbsp-class-badge-nbsp-bg-info-nbsp-New-span">Example heading <span class="badge bg-info ">New</span></h6>
      </div>
     <script src="../bootstrap5/bootstrap.bundle.min.js" ></script>
  </body>
</html>

A brief analysis of how to use the badge icon component in Bootstrap

3 For Buttons

Badges can provide counters as part of links or buttons.

<button type="button" class="btn btn-primary">
   未读消息 <span class="badge bg-secondary">4</span>
</button>

A brief analysis of how to use the badge icon component in Bootstrap

Please note that depending on how they are used, badges may be confusing to users of screen readers and similar assistive technologies. While the style of the badge provides a visual cue as to its purpose, these users only see the content of the badge. Depending on the context, these badges may look like random additional words or numbers at the end of a sentence, link, or button.

Unless the context is clear (as in the "Unread Messages" example, where it is understood that "4" is the number of notifications), consider including additional context in an additional text snippet that is visually hidden.

<button type="button" class="btn btn-primary">
    个人中心 <span class="badge bg-secondary">9</span>
    <span class="visually-hidden">未读消息</span>
 </button>

A brief analysis of how to use the badge icon component in Bootstrap

Note that this hidden label will not be displayed in front of the user, and there will be no prompt when the mouse is hovering. If you want a prompt when the mouse is hovering, you can add a button or badge. The difference is that when added to a button, a prompt will appear when the mouse hovers over the entire button. When added to a span label, a prompt will appear only when the mouse points to the number 9. There will be a delay when hovering, so it must be placed a few days above. seconds.

        <button type="button" class="btn btn-primary">
            个人中心 <span class="badge bg-secondary" title="您有9条未读消息" >9</span>
        </button>

        <button type="button" class="btn btn-primary" title="您有9条未读消息" >
            个人中心 <span class="badge bg-secondary" >9</span>
        </button>

A brief analysis of how to use the badge icon component in Bootstrap

4 Background Color

Quickly change the appearance of the badge using the provided utility classes. Note that when using Bootstrap's default .bg-light you may need a text color utility like .text-dark to get the correct styling. This is because the background utility only sets the background color.

<span class="badge bg-primary">Primary</span>
<span class="badge bg-secondary">Secondary</span>
<span class="badge bg-success">Success</span>
<span class="badge bg-danger">Danger</span>
<span class="badge bg-warning text-dark">Warning</span>
<span class="badge bg-info text-dark">Info</span>
<span class="badge bg-light text-dark">Light</span>
<span class="badge bg-dark">Dark</span>

A brief analysis of how to use the badge icon component in Bootstrap

5 Capsule Badge

Use the .rounded-pill utility class to make the badge rounder and with a larger border radius.

<span class="badge rounded-pill bg-primary">Primary</span>
<span class="badge rounded-pill bg-secondary">Secondary</span>
<span class="badge rounded-pill bg-success">Success</span>
<span class="badge rounded-pill bg-danger">Danger</span>
<span class="badge rounded-pill bg-warning text-dark">Warning</span>
<span class="badge rounded-pill bg-info text-dark">Info</span>
<span class="badge rounded-pill bg-light text-dark">Light</span>
<span class="badge rounded-pill bg-dark">Dark</span>

A brief analysis of how to use the badge icon component in Bootstrap

For more knowledge about bootstrap, please visit: bootstrap basic tutorial! !

The above is the detailed content of A brief analysis of how to use the badge icon component in Bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
What is Bootstrap? An Introduction for BeginnersWhat is Bootstrap? An Introduction for BeginnersApr 22, 2025 am 12:07 AM

BootstrapisafreeCSSframeworkthatsimplifieswebdevelopmentbyprovidingpre-styledcomponentsandJavaScriptplugins.It'sidealforcreatingresponsive,mobile-firstwebsites,offeringaflexiblegridsystemforlayoutsandasupportivecommunityforlearningandcustomization.

Bootstrap Demystified: A Simple ExplanationBootstrap Demystified: A Simple ExplanationApr 21, 2025 am 12:13 AM

Bootstrapisafree,open-sourceCSSframeworkthathelpscreateresponsive,mobile-firstwebsites.1)Itoffersagridsystemforlayoutflexibility,2)includespre-styledcomponentsforquickdesign,and3)ishighlycustomizabletoavoidgenericlooks,butrequiresunderstandingCSStoop

Bootstrap vs. React: Choosing the Right ApproachBootstrap vs. React: Choosing the Right ApproachApr 20, 2025 am 12:09 AM

Bootstrap is suitable for quick construction and small projects, while React is suitable for complex and interactive applications. 1) Bootstrap provides predefined CSS and JavaScript components to simplify responsive interface development. 2) React improves performance and interactivity through component development and virtual DOM.

Bootstrap's Purpose: Building Consistent and Attractive WebsitesBootstrap's Purpose: Building Consistent and Attractive WebsitesApr 19, 2025 am 12:07 AM

The main purpose of Bootstrap is to help developers quickly build responsive, mobile-first websites. Its core functions include: 1. Responsive design, which realizes layout adjustments of different devices through a grid system; 2. Predefined components, such as navigation bars and modal boxes, ensure aesthetics and cross-browser compatibility; 3. Support customization and extensions, and use Sass variables and mixins to adjust styles.

Bootstrap vs. Other Frameworks: A Comparative OverviewBootstrap vs. Other Frameworks: A Comparative OverviewApr 18, 2025 am 12:06 AM

Bootstrap is better than TailwindCSS, Foundation, and Bulma because it is easy to use and quickly develop responsive websites. 1.Bootstrap provides a rich library of predefined styles and components. 2. Its CSS and JavaScript libraries support responsive design and interactive functions. 3. Suitable for rapid development, but custom styles may be more complicated.

Integrating Bootstrap Styles in React: Methods and TechniquesIntegrating Bootstrap Styles in React: Methods and TechniquesApr 17, 2025 am 12:04 AM

Integrating Bootstrap in React projects can be done in two ways: 1) introduced using CDN, suitable for small projects or rapid prototyping; 2) installation using npm package manager, suitable for scenarios that require deep customization. With these methods, you can quickly build beautiful and responsive user interfaces in React.

Bootstrap in React: Advantages and Best PracticesBootstrap in React: Advantages and Best PracticesApr 16, 2025 am 12:17 AM

Advantages of integrating Bootstrap into React projects include: 1) rapid development, 2) consistency and maintainability, and 3) responsive design. By directly introducing CSS files or using the React-Bootstrap library, you can use Bootstrap's components and styles efficiently in your React project.

Bootstrap: A Quick Guide to Web FrameworksBootstrap: A Quick Guide to Web FrameworksApr 15, 2025 am 12:10 AM

Bootstrap is a framework developed by Twitter to help quickly build responsive, mobile-first websites and applications. 1. Ease of use and rich component libraries make development faster. 2. The huge community provides support and solutions. 3. Introduce and use class names to control styles through CDN, such as creating responsive grids. 4. Customizable styles and extension components. 5. Advantages include rapid development and responsive design, while disadvantages are style consistency and learning curve.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor