Achieving vertical alignment of form controls with labels that wrap in a row using Bootstrap 4

I am facing an issue where input elements in a row with labels above them are causing alignment problems when the screen width is reduced. The longer labels wrap to the next line and push the input element down, increasing the overall height of the row:

https://i.sstatic.net/o3P22.png

Although text wrapping is acceptable, I want the other inputs (with shorter labels) to remain aligned with the one that has shifted down.

Is there a way to resolve this without resorting to using ellipsis?

Below is the code snippet in question:

<div class="row">
    <div class="col-md-4 form-group">
        <div ><label>Short Label</label></div>
        <div ><input class="form-control" ></div>

    </div>

    <div class="col-md-4 form-group">
        <div ><label>This label is too long</label></div>
        <div ><input class="form-control" ></div>

    </div>

    <div class="col-md-4 form-group">
        <div ><label>Another label</label></div>
        <div ><input class="form-control" ></div>
    </div>
</div>

Answer №1

If you want to align items at the bottom, you can simply use the new align-items-end class. Check out more details on this here.

<div class="row align-items-end">
    <div class="col-md-4 form-group">
        <div ><label>Short Label</label></div>
        <div ><input class="form-control" ></div>

    </div>

    <div class="col-md-4 form-group">
        <div ><label>This label is too long</label></div>
        <div ><input class="form-control" ></div>

    </div>

    <div class="col-md-4 form-group">
        <div ><label>Another label</label></div>
        <div ><input class="form-control" ></div>
    </div>
</div>

Answer №2

If you want to change the class from col-md-4 to col, follow these instructions:

<div class="row">
    <div class="col form-group">
        <div ><label>Short Label</label></div>
        <div ><input class="form-control" ></div>
    </div>
    <div class="col form-group">
        <div ><label>This label is too long</label></div>
        <div ><input class="form-control" ></div>
    </div>
    <div class="col form-group">
        <div ><label>Another label</label></div>
        <div ><input class="form-control" ></div>
    </div>
</div>

Answer №3

My preferred method involves

<div class="row align-items-baseline">

Picture having a single text field on the left and a textarea on the right, by using "align-items-end," you can ensure that the bottom border of the text field aligns with the bottom border of the text area. I have found that with "align-items-baseline," the top borders of the two elements are aligned at the same level, which I believe is more visually appealing.

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

Transform the contents of a div tag into a function by removing the entire div tag

My goal is to find a clever solution for removing the contents between specific HTML classes using PHP. For example, I want to erase all the HTML within the "sometestclass" class. The function below is somewhat functional but not perfect. It eliminates so ...

What are some methods for identifying a single attribute element that is duplicated elsewhere on a webpage?

<html> <tr id="userman-orgchart-tree-node-9" class="fancytree-expanded fancytree-folder fancytree-has-children fancytree-exp-e fancytree-ico-ef"> <td> <span class="fancytree-node" style="padding-left: 16px;"> <span class="fancytr ...

Tips on eliminating excess white space or margins beneath a flexible video player

My issue is with a video and some content underneath. When I reduce the height of the video on mobile, there is too much white space below it. @media only screen and (max-width: 600px) { video { height: 80%; } Instead of adding margin-bottom ...

Obtaining the node generated from a document fragment

Suppose I add a document fragment to the DOM in the following way: const ul = document.querySelector("ul"); const fruits = ["Apple", "Orange", "Banana", "Melon"]; const fragment = new DocumentFragment(); ...

Fixed top bar and navigation menu, with a body that can be scrolled

My current goal is to achieve the following layout: <div style="position:absolute; background-color:Transparent; left:0px; right:0px; height:100px; z-index:2;"> <div style="background-color:Silver; width:1000px; height:100px; margin:0 auto;"& ...

Is there a way I can ensure the values are loaded when the page loads, rather than displaying NaN?

I've recently created a car rental calculator for a client, and it's almost complete. Everything is working smoothly, from the calculations to the conditions. However, I'm facing an issue where the price isn't calculated on page load. I ...

PreventDefault doesn't work well with Bootstrap toggling data

When working with a list of elements, each containing an ng-click, I encountered an issue. Nested inside each element is a div that is meant to toggle a Bootstrap modal. To prevent the ng-click event from the parent element from firing, I added $event.sto ...

"Stunning Broth: Retrieve elements from a list without the need for comma

I am attempting to extract a list of cities from a webpage using Beautiful Soup The current output is: MonroeMatthewsWaxhawIndian Trail, Matthews What I want is: Monroe, Matthews, Waxhaw, Indian Trail, Matthews This is the HTML: <div id="current_tab ...

Understanding the functionality of sessions in CodeIgniter when using AJAX

I am experiencing an issue with the Session in my code. It only works when the page is refreshed. I have a controller called ravi and the view named myview. Here is the code snippet: <?php class Ravi extends CI_Controller { public funct ...

What is the proper way to execute a JavaScript function within a JavaScript file from an HTML file?

I have the following content in an HTML file: <!DOCTYPE html> <!-- --> <html> <head> <script src="java_script.js"></script> <link rel="stylesheet" type="text/css" href="carousel.css"> & ...

Show Struts2 error message on an HTML webpage

I have included error and message handling in my Struts2 action class using the addActionMessage and addActionError methods. I am now looking for code that will allow me to display these messages and errors on the HTML page. Is it feasible to do so? ...

How can I change the color of a designated column in a Google stacked bar chart by clicking a button?

I am in the process of creating a website that compares incoming students at my university across different academic years. We have successfully implemented a pie chart to display data for the selected year. Now, we aim to include a stacked bar chart next ...

Assign the value from the list to a variable in order to execute an API call

Imagine a scenario where there's a button that displays a random joke based on a specific category. The categories are fetched using an API request from https://api.chucknorris.io/jokes/categories The jokes are generated from https://api.chucknorris. ...

display data in a sequential format

Within my MySQL database, there exists a table structure which I will share as a reference: +----+--------------+---------+--------+-----------+ | id | name | place | number | type | +----+--------------+---------+--------+-----------+ | 1 ...

Develop an outline using D3.js for the clipath shape

After successfully creating a shape with a color gradient, I encountered the need for further customization. If you are interested in this topic, check out these related questions: Add multi color gradient for different points in d3.js d3.js scatter plot c ...

Is it possible to issue commands within JavaFX-hosted Javascript?

I'm currently working on developing a JavaFX Application that assists me in creating JavaScript scripts. The application consists of a raw text Pane on the left, and an HTML Pane on the right which displays the rendered HTML content from the left Pane ...

How can we add a class to a radio button using jQuery when it is checked, and remove the class when it

I'm looking for help with using jQuery to toggle between two radio buttons and display different divs based on the selection. jQuery(document).ready(function($) { if ($('#user_personal').is(':checked')) { $('.user_co ...

Tips for sending data to CSS in Angular

I have an Angular application where I need to calculate the width of an element and store it in a variable called finalposition. Then, I want to move this element to the left by (finalposition)px when hovering over it. How can I achieve this styling effect ...

Find the parent element that houses a particular child element

I am searching for a way to identify all the parent elements that have a certain child element within them. <tr> <i class="myClass"> </i> </tr> For example, I want to find all tr elements that contain an i element with the specif ...

The click event fails to provide $event upon being clicked

Within my HTML structure in an angular 7 application, I have the following setup: My goal is to trigger the GetContent() function when any text inside the div is clicked. Strangely, when clicking on the actual text, $event captures "Liquidity" correctly. ...