The vertical alignment in Bootstrap form grid layout can be frustrating to work with

Looking at the image, it's clear that there's an issue with my Bootstrap 5 form layout. One of the labels has multiple lines of text and causing it to be misaligned with the other elements in the grid.

The problematic HTML code is as follows:

<!-- Include Bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41232e2e35323533203101746f736f71">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e4c41415a5d5a5c4f5e03474d41405d6e1f0017001f">[email protected]</a>/font/bootstrap-icons.css">

<div class="row">
    <div class="col-6 offset-3">
        <form action="/" method="POST" novalidate class="row g-3 validated-form">
            <div class="col-12 mb-3">
                <label for="address-line-1" class="form-label">Address Line 1</label>
                <input type="text" id="address-line-1" class="form-control" name="addressLine1" autocomplete="address-line1" required>
            </div>
            <div class="col-12 mb-3">
                <label for="address-line-2" class="form-label">Address Line 2</label>
                <input type="text" id="address-line-2" class="form-control" name="addressLine2" autocomplete="address-line2">
            </div>
            <div class="col-md-6 mb-3">
                <label for="city" class="form-label">City</label>
                <input type="text" id="city" class="form-control" name="city" autocomplete="address-level2" required>
            </div>
            <div class="col-md-4 mb-3">
                <label for="region" class="form-label">Province/State</label>
                <input type="text" id="region" class="form-control" name="region" autocomplete="address-level1" required>
            </div>
            <div class="col-md-2 mb-3">
                <label for="postcode" class="form-label">Postal Code/ZIP</label>
                <input type="text" id="postcode" class="form-control" name="postcode" autocomplete="postal-code" required>
            </div>
            <button class="btn btn-primary">Create</button>
        </form>
    </div>
</div>

I'm struggling to align these items properly. I've attempted using "d-flex align-items-end" but they end up stacking on top of each other and losing the default form styling. Any suggestions on how to make the alignment more sensible would be greatly appreciated.

Here is a visual of the badly aligned items.

Answer №1

By adding the align-items-end class to the form element, you can achieve the following:

<form action="/" method="POST" novalidate class="row g-3 validated-form align-items-end">

This adjustment will result in the layout shown here: https://i.sstatic.net/0kJbq.png

The align-items-end class is part of the flexbox utility classes provided by Bootstrap. You can learn more about it here: https://getbootstrap.com/docs/5.2/utilities/flex/#align-items

In this setup, the form element displays as a flex container due to having the row class applied.

Answer №2

<!-- Use Bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e7eaeab1b6b1b7a4b5ece9e3eff3ed">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="935b5656565d5a5c47565251515f4451544f12155657525714154740559886545c5c51">[email protected]</a>/font/bootstrap-icons.css">

<div class="row">
    <div class="col-6 offset-3">
        <form action="/" method="POST" novalidate class="row g-3 validated-form">
            <div class="col-12 mb-3">
                <label for="address-line-1" class="form-label">Enter Your Address Line 1</label>
                <input type="text" id="address-line-1" class="form-control" name="addressLine1" autocomplete="address-line1" required>
            </div>
            <div class="col-12 mb-3">
                <label for="address-line-2" class="form-label">Address Line 2 (Optional)</label>
                <input type="text" id="address-line-2" class="form-control" name="addressLine2" autocomplete="address-line2">
            </div>
            <div class="col-md-4 mb-3">
                <label for="city" class="form-label">City</label>
                <input type="text" id="city" class="form-control" name="city" autocomplete="address-level2" required>
            </div>
            <div class="col-md-4 mb-3">
                <label for="region" class="form-label">State/Province</label>
                <input type="text" id="region" class="form-control" name="region" autocomplete="address-level1" required>
            </div>
            <div class="col-md-4 mb-3">
                <label for="postcode" class="form-label">Postal Code/ZIP</label>
                <input type="text" id="postcode" class="form-control" name="postcode" autocomplete="postal-code" required>
            </div>
            <button class="btn btn-primary">Submit</button>
        </form>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to display JSON responses from AJAX in separate divs with identical class names?

I've been going through a whirlwind of confusion lately, unable to find a solution after spending over 100 hours on this. I'm reaching out for help in hopes that someone can guide me in the right direction! UPDATE: To clarify my issue and seek a ...

Ensuring an image fits perfectly within a div that spans 100% width using CSS

I'm brand new to CSS and could use some guidance Here's the issue: I have a 1600 pixel wide image and a div that is 100% wide. Is there a method to adjust the image so it fits within the div, eliminating any horizontal scroll bars and cutting o ...

Launch the file explorer using JavaScript/HTML

Is there a way to launch a real explorer window instead of the browser's directory look? For example, like the windows key + e window... Is there a method using html or JavaScript? I've noticed it in a Firefox add-on called new tab king, but I c ...

Using brush strokes to create a unique design on the canvas page

Currently, I am working on implementing a brush-like effect on a web page. The task involves providing multiple patterns/textures in the background and allowing users to drag their mouse to create the pattern effect on the page. If you want to see the st ...

Discovering the id of the current active tabpane is possible by using

Is there a way to retrieve the id of the currently active tab-pane every time I switch tabs on my page? Depending on which tab-pane is active (tab-pane1, tab-pane2, tab-pane3), I want to store the respective value (tab-pane1 = 1 and so on) in a variable an ...

Utilize jQuery to swiftly align elements within a designated container

Check out my JSFiddle example: http://jsfiddle.net/c62rsff3/ I'm attempting to implement snapping behavior between 2 elements using this code: $('.draggable-elements').draggable({ snap: true }); In addition, I've defined a container ...

CSS: element misplaced at the top instead of the bottom where it belongs

The component with the ID of "#bottom-nav" actually appears at the top. I just designed this on CSS-board, so feel free to review the HTML and CSS code Take a look at it on CSS-desk here It is positioned at the very end of the page and includes a ' ...

Incorporate AJAX functionality with a simple HTML button

Upon clicking the button, the AJAX call fails to execute. Below is the HTML code for the buttons: <td><button id=${data[i].id} name="update" type="button" value="${data[i].id}" class="btn btn-primary update" ...

An easy guide to automatically refreshing a div with a seamless animation

I am working on an HTML page where I want to smoothly refresh a specific div every 60 seconds. What I have in mind is a simple process: fade out the div / refresh the div / fade in the div However, despite reading extensively about jQuery and AJAX for r ...

Identify and track colored dots within an image using tracking.js

I'm working on creating a program that can tally the number of dots on dominoes in an image, such as the one shown here: https://i.sstatic.net/NKHXl.jpg My goal is to develop this functionality using JavaScript. I've attempted to learn how to u ...

ng-bind-html is having trouble parsing the HTML correctly and binding it

Here is the code for my controller: myApp.controller('actionEditController', ['$scope', '$stateParams', '$sce',function ($scope, $stateParams, $sce) { $scope.table="<p>OOPSY</p>"; $sc ...

Exploring Bootstrap's Product sample and attempting to understand the process of incorporating images

Currently, I am utilizing Bootstrap 4 and taking advantage of their Product example as a foundation: https://getbootstrap.com/docs/4.4/examples/product/ My goal is to transform the white and gray background sections into actual images of the product. The ...

How can we stop the jumping of images in the grid? Is there a way to eliminate the jump effect?

Here is a script I am working with: <script src="http://static.tumblr.com/mviqmwg/XyYn59y3a/jquery.photoset-grid.min.js"></script> <script> $(document).ready(function() { $('.photoset-grid').photose ...

`How to update caret color on hover of drop down button`

Here is an example of a button using an anchor tag: <a href ng-if="!isLogin" class="dropdown-toggle" data-toggle="dropdown"> <b class="white-link">Login</b> <span class="caret white-link"></span></a> In order to style ...

The Google Closure Compiler Service is providing faulty code

I've been using Closure Compiler through to minify and obfuscate my JavaScript code, but I seem to be encountering an issue. To troubleshoot, I created a test page with a script. Here's the HTML5 code snippet: <!DOCTYPE html> <html> ...

Mobile page sliding mechanism

My website contains a div that is mostly off the page, but on hover it translates onto the main page. You can check out my website. However, this method doesn't work well on mobile devices. Hovering is not effective and I often have to click multipl ...

How to reposition a DIV element using CSS only

I'm facing a challenge where I need to relocate a DIV element to the bottom of the body without altering the HTML structure. I tried referring to some existing threads for solutions, like this one on Stack Overflow: Moving a div from bottom to top, us ...

Guide to launching a web browser within a Phonegap app with a button click event

We are developing a PhoneGap application using HTML and JavaScript. Our goal is to integrate PayPal's website so that users can make payments with a simple button click event within the app, and then seamlessly return back to the application afterward ...

Extract information from an external HTML table and store it in an array

I am looking for a way to extract data from an HTML table on an external site and save it as a JSON file for further manipulation. Is there a method to retrieve table data from an external HTML site that differs from the code example provided? Additional ...

how to position one div above another using css

I currently have a page layout with 7 different div elements structured like this; <div id="container"> <div id="head"></div> <div id="A"> <div id="A1">A1</div> <div id="A2"></div> ...