Unable to fix button alignment wrt container in bootstrap 5

Viewed 42

I have a piece of html code, where everything is fine except for the position of the button. It's very far away from the container. This is my code below:

<div class="container-fluid mt-1 d-flex align-items-center justify-content-center" id="inp_field" style="background-color: rgb(127, 255, 202); padding: 5em 5em">
        <div class="container" style="background-color: rgb(250, 214, 116); width:25em;">
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" id="mySwitch" name="emp">
                <label class="form-check-label" for="mySwitch"><b id="tag">EmpName</b></label>
            </div>

            <div class="form-floating mb-3 mt-3">
                <input type="text" class="form-control" id="emp" data-bs-toggle="tooltip" title="You can toggle the above button to choose between empname and PF no :))"  placeholder="Enter empname" onkeypress="return (event.charCode > 64 && 
                event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || event.charCode==32" >
                <label for="emp">Enter your name</label>
            </div>
            <label for="sel1" class="form-label"><b>Select period (YM - YM):</b></label>
                <div id="period">
                    <select class="form-select" id="sel1" name="sel1" style="width: 7em;"></select>
                    <b>-</b>
                    <select class="form-select" id="sel2" name="sel2" style="width: 7em;" disabled></select>
                </div>
                <br>
                <div class="form-check">
                    <input type="checkbox" id="checksupp" name="Check if supplementary bill is required" value="Check if supplementary bill is required">
                    <label class="form-check-label" for="checksupp">Check if supplementary bill is required</label>
                </div>
        </div>
        <button class="btn btn-primary mt-4 ms-2 d-flex align-items-start justify-content-start" type="button" id="submit">Submit</button>
    </div> 

The button with the id submit is very far away from the container (the reference image is uploaded below). I am not able to fix this. Please help me.

I want the button to be in this position below the container in the center

3 Answers

Remove .container from the yellow box and add padding, Also, add .flex-column to the parent class to make it a column, right now your flex container is row-wise, make the parent div of the select elements a flex box with justify-content-between to create gap between the elements And to make your select row wise

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>NOTA MEDIA</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    </head>
    <body>
        <!-- Add flex-column class-->  
        <div class="container-fluid mt-1 d-flex flex-column align-items-center justify-content-center" id="inp_field" style="background-color: rgb(127, 255, 202); padding: 5em 5em">
       <!-- Remove container class from this div and add padding -->
        <div class="p-4"style="background-color: rgb(250, 214, 116); width:25em;">
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" id="mySwitch" name="emp">
                <label class="form-check-label" for="mySwitch"><b id="tag">EmpName</b></label>
            </div>

            <div class="form-floating mb-3 mt-3">
                <input type="text" class="form-control" id="emp" data-bs-toggle="tooltip" title="You can toggle the above button to choose between empname and PF no :))"  placeholder="Enter empname" onkeypress="return (event.charCode > 64 && 
                event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || event.charCode==32" >
                <label for="emp">Enter your name</label>
            </div>
            <label for="sel1" class="form-label"><b>Select period (YM - YM):</b></label>
                <!-- Make it a flex box container-->
                <div id="period" class="d-flex justify-content-between">
                    <select class="form-select" id="sel1" name="sel1" style="width: 7em;"></select>
                    <b>-</b>
                    <select class="form-select" id="sel2" name="sel2" style="width: 7em;" disabled></select>
                </div>
                <br>
                <div class="form-check">
                    <input type="checkbox" id="checksupp" name="Check if supplementary bill is required" value="Check if supplementary bill is required">
                    <label class="form-check-label" for="checksupp">Check if supplementary bill is required</label>
                </div>
        </div>
        <button class="btn btn-primary mt-4 ms-2 d-flex align-items-start justify-content-start" type="button" id="submit">Submit</button>
    </div> 
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    </body>
</html>

I added a parent grid element, now you can arrange and style your elements as you wish.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>NOTA MEDIA</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

</head>

<body>

  <div class="container-fluid mt-1 d-flex align-items-center justify-content-center" id="inp_field" style="background-color: rgb(127, 255, 202); padding: 5em 5em">
    <div style="display: grid; grid-template-columns: 1fr 1fr; grid-column-gap: 2em">
      <div class="container" style="background-color: rgb(250, 214, 116); width:25em;">
        <div class="form-check form-switch">
          <input class="form-check-input" type="checkbox" id="mySwitch" name="emp">
          <label class="form-check-label" for="mySwitch"><b id="tag">EmpName</b></label>
        </div>

        <div class="form-floating mb-3 mt-3">
          <input type="text" class="form-control" id="emp" data-bs-toggle="tooltip" title="You can toggle the above button to choose between empname and PF no :))" placeholder="Enter empname" onkeypress="return (event.charCode > 64 && 
                event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || event.charCode==32">
          <label for="emp">Enter your name</label>
        </div>
        <label for="sel1" class="form-label"><b>Select period (YM - YM):</b></label>
        <div id="period">
          <select class="form-select" id="sel1" name="sel1" style="width: 7em;"></select>
          <b>-</b>
          <select class="form-select" id="sel2" name="sel2" style="width: 7em;" disabled></select>
        </div>
        <br>
        <div class="form-check">
          <input type="checkbox" id="checksupp" name="Check if supplementary bill is required" value="Check if supplementary bill is required">
          <label class="form-check-label" for="checksupp">Check if supplementary bill is required</label>
        </div>
      </div>
      <button class="btn btn-primary" style="width: 80px; height: 40px; align-self: self-end" type="button" id="submit">Submit</button>

    </div>
  </div>
</body>

</html>

Maybe you need something like this, your button is outside of the main container.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>NOTA MEDIA</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    </head>
    <body>
        <div class="container-fluid mt-1 align-items-center justify-content-center" id="inp_field" style="background-color: rgb(127, 255, 202); padding: 5em 5em">
            <div class="container" style="background-color: rgb(250, 214, 116); width:25em;">
                <div class="form-check form-switch">
                    <input class="form-check-input" type="checkbox" id="mySwitch" name="emp">
                    <label class="form-check-label" for="mySwitch"><b id="tag">EmpName</b></label>
                </div>

                <div class="form-floating mb-3 mt-3">
                    <input type="text" class="form-control" id="emp" data-bs-toggle="tooltip" title="You can toggle the above button to choose between empname and PF no :))"  placeholder="Enter empname" onkeypress="return (event.charCode > 64 && 
                    event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || event.charCode==32" >
                    <label for="emp">Enter your name</label>
                </div>
                <label for="sel1" class="form-label"><b>Select period (YM - YM):</b></label>
                <div id="period">
                    <select class="form-select" id="sel1" name="sel1" style="width: 7em;"></select>
                    <b>-</b>
                    <select class="form-select" id="sel2" name="sel2" style="width: 7em;" disabled></select>
                </div>
                <br>
                <div class="form-check">
                    <input type="checkbox" id="checksupp" name="Check if supplementary bill is required" value="Check if supplementary bill is required">
                    <label class="form-check-label" for="checksupp">Check if supplementary bill is required</label>
                </div>
               
            </div>
            <div class="row align-items-center mb-2">
                <div class="col-lg-12 text-center">
                    <button class="btn btn-primary mt-4 ms-2 align-items-start" type="button" id="submit">Submit</button>                        
                </div>
            </div>
        </div> 
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    </body>
</html>

Related