What is the best way to align a set of input fields with their corresponding labels, along with a single button?

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

I am facing a specific alignment challenge in my project, especially due to the presence of labels. When I group inputs and a button together and use align items center, it works perfectly. However, the labels affect the height, causing the button to be misaligned.

I attempted to divide the layout into two rows - one for labels and another for inputs + button, which worked on larger devices but not on smaller ones.

Another potential solution I considered was using absolute positioning for the labels, but I'm hesitant about its effectiveness.

Using pseudo-elements for the labels is another idea I have in mind.

Finally, I thought of applying align-self to the button to adjust its position at the bottom and then use margins or padding for alignment. However, I'm unsure if this approach is the best one.

Can anyone suggest a better way to achieve the desired alignment? What approach do you think would work best?

Below is the code snippet I used to experiment with two rows. It requires Bootstrap to run.

<div class="d-flex align-items-end pb-4">
                <div class="w-100">
                    <!-- Label wrapper -->
                   <div class="row gx-3">
                    <div class="col-9  row gx-3 align-items-center">
                        <div class="col-3">
                            <label class="form-label" for="input1">Label</label>
                        </div>
                        <div class="col-3">
                            <label class="form-label" for="input2">Label</label>
                        </div>
                        <div class="col-3">
                            <label class="form-label" for="input3">Label</label>
                        </div>
                    </div>
                   </div>
                   <!-- Input wrapper -->
                   <div class="row gx-3">
                    <div class="col-9 row gx-3 align-items-center">
                        <div class="col-3">
                            <input placeholder="Placeholder" id="input1" class="form-control" />
                        </div>
                        <div class="col-3">
                            <select class="form-select" id="input2">
                                <option selected value="default">Select</option>
                                <option value="1">One</option>
                                <option value="2">Two</option>
                                <option value="3">Three</option>
                            </select>
                        </div>
                        <div class="col-3">
                            <select class="form-select" id="input3">
                                <option selected value="default">Select</option>
                                <option value="1">One</option>
                                <option value="2">Two</option>
                                <option value="3">Three</option>
                            </select>
                        </div>
                        <div class="col-3">
                            <button class="btn btn-outline-secondary">Search</button>
                        </div>
                    </div>
                    
                   </div>
                </div>
            </div>

Please try to recreate the desired layout based on the image in the description. I have not provided code for the other methods I considered, but I believe a verbal description is sufficient. It essentially involves a flex container with inputs. However, when an input is enclosed in a label, aligning the 'search' button at the end becomes an issue as it has to align with both the input and the label wrapper.

Answer №1

To vertically align your content, you can simply utilize flexbox:

Modified:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddbfb2b2a9aea9afbcad9de8f3edf3ef">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4969b9b808780869584b4c1dac4dac6">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<div class="d-flex gap-3 flex-row align-items-center position-relative" style="padding-top: 1.25em">
    <div class="col-3">
        <label for="input1" class="position-absolute top-0">Label</label>
        <input placeholder="Placeholder" id="input1" class="form-control" />
    </div>
    <div class="col-3">
        <label for="input2" class="position-absolute top-0">Label</label>
        <select class="form-select d-block" id="input2">
            <option selected value="default">Select</option>
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </select>
    </div>
    <div class="col-3">
        <label for="input3" class="position-absolute top-0">Label</label>
        <select class="form-select d-block" id="input3">
            <option selected value="default">Select</option>
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </select>
    </div>
    <div class="col-3">
        <button class="btn btn-outline-secondary btn-lg">Search</button>
    </div>
</div>

Answer №2

Utilizing the power of tailwind, I successfully implemented the solution to your query.

<div class="flex gap-4 items-end">
  <div class="flex flex-col">
    <label>label a</label>
    <input type="text" class="w-24" />
  </div>
   <div class="flex flex-col">
    <label>label a</label>
    <input type="text" class="w-24" />
  </div>
   <div class="flex flex-col">
    <label>label a</label>
    <input type="text" class="w-24" />
  </div>
   <div class="flex flex-col">
    <input type="button" class="w-24 bg-gray-300 p-2" value="button" />
  </div>
</div>

Feel free to take a look at this fiddle for more details

Answer №3

Utilizing an invisible fourth element in the labels row in conjunction with flex:1 0 0 to evenly space the elements and align the top row with the bottom row.

*,
*:before,
*:after {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.container {
  display: inline-flex;
  flex-direction: column;
  border: 1px solid;
}

.inputs,
.labels {
  display: flex;
  align-items: center;
}

.labels:after {
  content: '';
}

.inputs>div,
.labels:after,
.labels>div {
  flex: 1 0 0;
}


/* Handling input styles */

input {
  width: 100%;
}

button {
  animation: x 2s linear infinite alternate;
}

@keyframes x {
  from {
    height: 20px;
  }
  to {
    height: 40px;
  }
}
<div class="container">
  <!-- Label wrapper -->
  <div class="labels">
    <div>
      <label>Label</label>
    </div>
    <div>
      <label>Label</label>
    </div>
    <div>
      <label>Label</label>
    </div>
  </div>
  <!-- Input wrapper -->
  <div class="inputs">
    <div>
      <input />
    </div>
    <div>
      <select>
        <option>Select</option>
        <option>One</option>
        <option>Two</option>
        <option>Three</option>
      </select>
    </div>
    <div>
      <select>
        <option>Select</option>
        <option>One</option>
        <option>Two</option>
        <option>Three</option>
      </select>
    </div>
    <div>
      <button>Search</button>
    </div>
  </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

Some devices experience currency symbol overlap problem with Money-rails Gem

I am currently using the most recent version of money-rails. However, I am facing an issue where the currency symbol from the humanized_money_with_symbol helper is overlapping with the value, as illustrated in the image below: https://i.sstatic.net/g2bwG. ...

Any tips or hacks for successfully implementing vw resizing on the :before pseudo-element in webkit browsers?

When using vw sizes on webkit browsers, a well-known bug can occur where they do not update when the window is resized. The typical workaround for this issue has been to employ javascript to force a redraw of the element by reassigning the z-index upon res ...

Using Bootstrap to style the <option> element with RGBA colors

Struggling to figure this out and seeking assistance. I have a form using bootstrap where I can apply rgba color for my select tag but not for the options. Select CSS: select { background-color: rgba(0,0,0,0.25) !important; border: 1px solid rgba ...

"Adjust the orientation of a video and ensure it properly fills the screen with CSS styling

Below is the CSS style being used: video { position: absolute; transform: rotate(90deg); width: 100%; height: 100%; z-index: 4; visibility: visible; } This code snippet demonstrates a video element: <video id="myVideo" ...

Extract ID for Bootstrap modal display

In my project, I am using a bootstrap modal that displays various strings. The challenge I am facing involves a loop of cards, each with a distinct 'id'. When triggering the modal, I want to show the corresponding id inside the modal itself, whic ...

Creating overlapping layouts with absolute positioning in Bootstrap leads to a visually appealing

I'm currently working on a layout that involves absolute positioning elements, similar to the one shown in this example: https://i.sstatic.net/TixbZ.jpg It seems like the elements are overlapping and not clearing properly. I've been looking for ...

When a CSS fluid layout includes a containing div with margin or padding, it may lead to

On my jsfiddle , the outermost wrapper div has a width of 100%. I am trying to create an inner div (tb_margin or tb_padding) where the content starts 40px from the left. I have attempted to use both margin and padding for this left spacing, but in both cas ...

Firefox experiencing trouble handling flexbox overflow

Having some trouble with a test project that involves using flexbox. The task at hand is to create a dashboard with multiple lists of cards arranged side-by-side with infinite overflow. I was able to achieve this layout, however, the issue arises when eac ...

Guide to modifying the text color of a Primefaces/jqPlot line chart:

I've implemented a basic JSF line chart with PrimeFaces (using jqPlot library) in my project: <p:lineChart id="linear" value="#{team.chart}" title="Lap Times" xaxisLabel="Lap" yaxisLabel="Time ...

Is there a way to customize the font size of Material UI Autocomplete?

I'm currently trying to customize the Material UI Autocomplete component with my own CSS. Specifically, I aim to adjust the font size of the input field. Below is my current implementation: <Autocomplete style={{ width: 200, height: 60, ...

Children divs unexpectedly showing up outside of their parent divs

I have reviewed similar questions on this matter, but none of them seem to provide a solution for the issue I am facing. Currently, my goal is to display a stylized list, but I am encountering more difficulties than expected. You can find a fiddle linked ...

Tips for returning the slider to its default position when a tab is deselected

A fantastic code snippet can be found at this link for creating an animated navigation bar. The only thing left on my wishlist is for the slider to reset to its original position if a tab is not selected. Currently, it remains static regardless of selectio ...

What is the best way to make the text align with the top of its designated space?

I've been experimenting with different CSS properties like vertical-align, line-height, and more. This example should give you an idea of my goal. I added a small red arrow to indicate where I intend for the text to be shifted upwards. ...

Challenges with resizing floating divs

As a beginner in Web Development, I am tasked with creating a web portfolio for an assignment. In my design layout, I have a navigation bar in a div floated left, and a content div floated right serving as an about me section containing paragraphs and lis ...

Tips for altering the appearance of a dropped item using JQueryUI sortable

I have a straightforward website where I need to implement the ability to drag and drop styled DIV elements between two containers. Utilizing JQueryUI's sortable function, this behavior was successfully achieved with the following code: $("#active-co ...

Flow bar for micro-tasks

In my current project, I am faced with the task of organizing a series of 4 mini tasks and displaying to the end user which specific mini task they are currently on. To accomplish this, I have been utilizing image tags for each task. <img>1</img ...

What is the most effective method for incorporating parameterized aesthetics using CSS in a JSP program?

I am currently working on a JSP web application without the support of any framework, unfortunately. This application is intended to serve multiple customers, each with their unique set of colors and company logos. The overall layout of the CSS will remai ...

What steps do I need to take in order to change an HTML div's background to a black

My current project involves dynamically loading HTML content stored on disk into a specific div element. This is achieved by using the following code: $('#FictionContent').load('Content/HuckFinn.html'); Just to provide some background ...

Creating a responsive layout that sets the window height to 100% and automatically adjusts sibling divs when the content within parent divs exceeds the defined height

<section> <div class="section-wrap"> <h1>page1</h1> </div> </section> <section> <div class="section-wrap"> <h1>page2</h1> </div> </section> My attempt at impleme ...

Visual Delights on the Menu

I am having trouble adding a "Home" picture to my menu. It keeps showing up as a blank square, even though I have tried using both .png and .jpg formats. Here is the code that I am currently using: <div id='cssmenu'> <link rel= ...