Bootstrap badge with close button example
May 20, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
In this tutorial, we will learn how to create badge with close button in bootstrap.
Table of contents
- Includes bootstrap library
- Define its class name
Step 1: Includes bootstrap library
First of all, load the Bootstrap framework CSS into the head tag of your webpage.
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
Step 2: Define its class name
After that, create the badge with close button with a class name .badge badge-info
.
<div class="container">
<h4>Bootstrap badge with close button example</h4>
<p>Current close buttone disadvantages</p>
<p>Without size styling</p>
<div class="badge badge-info">Label
<button aria-label="Close" type="button" class="close" disabled="disabled" style="white-space: nowrap;"><span
aria-hidden="true">×</span></button>
</div>
<p></p>
<p>With size styling, but without vertical adjustment</p>
<div class="badge badge-warning">Label
<button aria-label="Close" type="button" class="close" disabled="disabled"
style="white-space: nowrap; line-height: 1rem; font-size: 1.3rem;"><span aria-hidden="true">×</span></button>
</div>
<p></p>
<p>Other problem: need inverse color for close button</p>
<div class="badge badge-danger">Label
<button aria-label="Close" type="button" class="close" disabled="disabled"
style="white-space: nowrap; line-height: 1rem; font-size: 1.3rem;"><span aria-hidden="true">×</span></button>
</div>
Example.
Let’s look at the following example to understand how it basically works:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h4>Bootstrap badge with close button example</h4>
<p>Current close buttone disadvantages</p>
<p>Without size styling</p>
<div class="badge badge-info">Label
<button aria-label="Close" type="button" class="close" disabled="disabled" style="white-space: nowrap;"><span
aria-hidden="true">×</span></button>
</div>
<p></p>
<p>With size styling, but without vertical adjustment</p>
<div class="badge badge-warning">Label
<button aria-label="Close" type="button" class="close" disabled="disabled"
style="white-space: nowrap; line-height: 1rem; font-size: 1.3rem;"><span aria-hidden="true">×</span></button>
</div>
<p></p>
<p>Other problem: need inverse color for close button</p>
<div class="badge badge-danger">Label
<button aria-label="Close" type="button" class="close" disabled="disabled"
style="white-space: nowrap; line-height: 1rem; font-size: 1.3rem;"><span aria-hidden="true">×</span></button>
</div>
</body>
</html>
Check the output of the above code example.
All the best 👍