Make bootstrap CHECKBOX look like bootstrap BADGE

Viewed 750

I'd like to make bootstrap checkboxes that look like bootstrap badges, while retaining the checkbox functionality. I thought maybe I could simply style the checkbox label as a badge, but it didn't work.

<head>
        <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css'>
        <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js'></script>
</head>
    
        <div class='form-check m-1' style='display:inline-block;'>
        <input id='".$tagDAT[1]."' name='".$tagDAT[1]."' type='checkbox' class='form-check-input form-check-inline'>
        <label class='tag form-check-label text-capitalize' for='".$tagDAT[1]."'>
        <span class='badge badge-secondary'>".$tagDAT[1]."</span></label>
        </div>

This code is being echoed from PHP. The checkboxes look like regular checkboxes. It's ignoring the 'badge' code. I thought maybe since it was being echoed via ajax, the bootstrap in the html page wasn't affecting the echoed code. That's why i added the 'head' tag with the bootstrap. Still didn't work.

Anybody have ideas? Or a better way of getting the same result?

2 Answers
  1. Move the checkbox inside the label
  2. Remove the nested <span>
  3. Apply .badge .badge-secondary directly to the <label>
  4. Apply .form-check-inline to the wrapper not the input

Click anywhere on the .badge to check/uncheck the box.

<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css'>

<div class='form-check form-check-inline m-1'>
  <label class='tag form-check-label text-capitalize badge badge-secondary' for='myTag'>
    <input id='myTag' name='myTag' type='checkbox' class='form-check-input'>myTag
  </label>
</div>

Bootstrap 5 has got Toggle Buttons

<input type="checkbox" class="btn-check" id="btn-check" autocomplete="off">
<label class="btn btn-primary" for="btn-check">Single toggle</label>

<input type="checkbox" class="btn-check" id="btn-check-2" checked autocomplete="off">
<label class="btn btn-primary" for="btn-check-2">Checked</label>
Related