Search code examples
cssbootstrap-4alignment

Horizontally align bottom of input field on bootstrap form-groups


Using Bootstrap 4 in my Angular 5 application I have two form groups right next to each other. When the text label of one is much longer than the other, the input fields don't line up, which ends up looking strange when there are multiple rows like this on the page.

You can see exactly what I mean at https://angular-wcd2ln.stackblitz.io/

How can I tell it that I want the two input labels to be vertically aligned together and have the label "push down" with the input field?


Solution

  • Use the flexbox utility classes (d-flex flex-column) on the form-group, and mt-auto (margin-top:auto) on the form-control inputs...

     <div class="row">
            <div class="col-6">
                <div class="form-group h-100 d-flex flex-column">
                    <label for="left">This is a short label.</label>
                    <input class="form-control mt-auto" id="left" type="text">
                </div>
            </div>
            <div class="col-6">
                <div class="form-group h-100 d-flex flex-column">
                    <label for="right"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
                        aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </label>
                    <input class="form-control  mt-auto" id="right" type="text">
                </div>
            </div>
     </div>
    

    https://www.codeply.com/go/z0vucbNNU9