bootstrap-4bootstrap-modalbootstrap5-modal

How to align a label on a line in Bootstrap?


I added a modal and I would like the label to be on one line. I guess I need to reduce the input and increase the width of the label.

But... Do you know how to do this on Bootstrap? I'm lost in my divs.

enter image description here

Below, here is an idea of my current code:

Thank you for your help.

<!DOCTYPE html>
<html>
<head>
<title>HTML CSS JS</title>
</head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<body>
<div class="modal-header">
    <h4 class="modal-title" id="modal-basic-title">Simple modal</h4>
    <button type="button" class="btn-close" aria-label="Close button" aria-describedby="modal-title" (click)="modal.hide()">
    </button>

</div>
<div class="modal-body">
    <form>
        <div class="row ">
            <div class="col-12">
                <div class="row">
                    <div class="col-12 col-sm-12 mb-2">
                        <div class="form-group row">
                            <label for="cliente" class="col-sm-2 col-form-label text-end">Quantité a transférer</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="cliente">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row ">
            <div class="col-12">
                <div class="row">
                    <div class="col-12 col-sm-12 mb-2">
                        <div class="form-group row">
                            <label for="endereco" class="col-sm-2 col-form-label text-end">Changement Bénéficiaire Économique</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="endereco">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row ">
            <div class="col-12">
                <div class="row">
                    <div class="col-12 col-sm-12 mb-2">
                        <div class="form-group row">
                            <label for="complemento" class="col-sm-2 col-form-label text-end">Compte du destinataire</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="complemento">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary">Send message</button>
</div>
</body>
</html>


Solution

  • Bootstrap has a grid system, where the entire screen is divided into 12 columns. You assign each label 2 columns, and each input box 10 columns, using the classes col-sm-2 and col-sm-10. If you change the numbers there, you can change how many columns each field takes up. For example, if you change the label class to `col-sm-4' and the input class to 'col-sm-8', the label will take up 1/3 of the screen (4/12 columns) and the input will take up 2/3 of the screen (8/12 columns). You can change these numbers to get a value that works for you.

    The sample below shows 3 different values for the amount of columns, so you can see how this works.

    <!DOCTYPE html>
    <html>
    <head>
    <title>HTML CSS JS</title>
    </head>
      <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <body>
    <div class="modal-header">
        <h4 class="modal-title" id="modal-basic-title">Simple modal</h4>
        <button type="button" class="btn-close" aria-label="Close button" aria-describedby="modal-title" (click)="modal.hide()">
        </button>
    
    </div>
    <div class="modal-body">
        <form>
            <div class="row ">
                <div class="col-12">
                    <div class="row">
                        <div class="col-12 col-sm-12 mb-2">
                            <div class="form-group row">
                                <label for="cliente" class="col-sm-3 col-form-label text-end">Quantité a transférer</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="cliente">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row ">
                <div class="col-12">
                    <div class="row">
                        <div class="col-12 col-sm-12 mb-2">
                            <div class="form-group row">
                                <label for="endereco" class="col-sm-4 col-form-label text-end">Changement Bénéficiaire Économique</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" id="endereco">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row ">
                <div class="col-12">
                    <div class="row">
                        <div class="col-12 col-sm-12 mb-2">
                            <div class="form-group row">
                                <label for="complemento" class="col-sm-5 col-form-label text-end">Compte du destinataire</label>
                                <div class="col-sm-7">
                                    <input type="text" class="form-control" id="complemento">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
    </div>
    </body>
    </html>