Bootstrap 5 text BEFORE switch-checkbox (without extra addons/plugins)
Asked Answered
D

3

6

I use Bootstrap 5 and I wand to use a switch-toggle.

I don't want to use any other addons, but only with bootstrap.

How to put a Text BEFORE the toggle switch.

This does not work:

<div class="form-check form-switch">
  off
  <input type="checkbox" class="form-check-input" id="site_state">
  <label for="site_state" class="form-check-label">on</label>
</div>

Anyone have an idea ?

Duer answered 11/3, 2021 at 11:35 Comment(0)
C
11

What if you put your Off label before the checkbox and make everything display: inline-block?

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>


<div class="container">
    <div class="row">
        <div class="col">
            <div class="d-inline-block me-1">Off</div>
            <div class="form-check form-switch d-inline-block">
                <input type="checkbox" class="form-check-input" id="site_state" style="cursor: pointer;">
                <label for="site_state" class="form-check-label">On</label>
                </div>
        </div>
    </div>
</div>
Capital answered 11/3, 2021 at 11:59 Comment(1)
Hi. Of course, I also thought of "d-inline-block" first, but I hadn't thought of constructing a complete scaffolding around it. But the solution works and I would like to thank you very much!Duer
A
4

You could wrap it in a d-flex..

<div class="d-flex">
    off
    <div class="form-switch ms-2">
        <input type="checkbox" class="form-check-input" id="site_state">
    </div>
    <label for="site_state" class="form-check-label">on</label>
</div>

Demo

Arthrospore answered 11/3, 2021 at 12:56 Comment(1)
Any advantage of this over the accepted answer?Neigh
S
1

bootstrap 5.3

form-check-reverse exchanges the positions of label and input; form-check-inline clears the padding/margin;

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
      <div class="form-check form-check-inline form-check-reverse">
        <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Label on the left</label>
      </div>
      <hr>
      <div class="form-check">
        <input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
        <label class="form-check-label" for="flexSwitchCheckDefault">Label on the right</label>
      </div> 
Scavenger answered 25/11, 2023 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.