Tips for positioning the button between two labels placed above and below the input field

I am currently utilizing Bootstrap 4 and I am attempting to align a button with an input that has two labels - one above and one below it.

It seems to work when I place the button outside the form tag, but unfortunately, I need to keep it within for the Javascript code to function properly.

I have experimented with the form-inline class, which worked fine when there was only one label above the input. However, when adding another label below the input, the alignment between the button and the input gets disrupted.

If you try to make the bottom label visible by typing a letter in the input field and pressing enter, you will notice that the button becomes misaligned.

My goal is to consistently maintain the alignment of the button to the right of the input.

Link to Image

Here is my CodePen link: CodePen Link

Below are the HTML codes:

        <article>
            <div class="container">
                <div class="row searchSection">
                    <form id="searchForm" class="form-inline input-group input-group-lg">
                        <div class="col-lg-10 form-group align-self-start">
                            <label class="postcode">Zip Code</label>
                            <input type="text" name="text" autocomplete="off">
                        </div>
                        <div class="col-lg-2 form-group align-self-end">
                            <button type="submit">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </article>

Answer №1

To ensure alignment between the button and the field, simply insert an empty label within the button's div:

<div class="col-lg-2 form-group align-self-start">
  <label>&nbsp;</label>
  <button type="submit">Submit</button>
</div>

Additionally, I have utilized align-self-start in this case, mirroring what was done in the field's div.

Answer №2

The issue lies in your current implementation of using align-self: end, which results in the button always being displayed at the bottom of the div.

Even attempting to switch it to align-self: center won't work properly if there is only a top label without a bottom label, as shown in the image: https://i.sstatic.net/MyXZB.png

A straightforward solution exists: Given that the top label has a fixed height of 24px, you can also fix the position of the button accordingly.

To implement this, I suggest removing the 'align-self-end' class and instead adding a .fixed class with the following styles:

.fixed { 
   display: block !important;
   margin-top: 24px;
} 

.fixed button { 
 line-height: 47px; //matching the input's line-height of 47px for consistency
}

You can refer to this codepen example to see it in action.

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

Text width that adjusts dynamically

Consider this HTML structure; <div> <span>Text one</span> <span class="sticky">ABC</span> </div> <div> <span>Some other text</span> <span class="sticky">DEF</span> </div> < ...

What is the best way to replicate a div's background color using an image within the body element?

I am looking to achieve a layout similar to the one below: https://i.sstatic.net/1cOYw.png The dark grey color represents the sidebar, but I want to use this color as a repeating vertical image in the body element without covering the footer (light gray) ...

The primary division grows continuously in size

Currently setting up a basic layout for my homepage. Here's how I envision it: Having trouble defining the size of the main div. I want it to dynamically expand in both height and width, filling the remaining space not taken by menu and footer. HT ...

Designing my website to resize and adapt to different screen resolutions using CSS

As I navigate my first week of CSS, HTML, and PHP programming, I encountered a problem with text overlapping and causing issues on my site when scaled according to window size. I'm seeking clarity on what mistakes I may be making. Despite trying medi ...

Extracting phone numbers from the 'Call' button in Google's local search results on mobile devices: A step-by-step guide

As I was conducting a search using a mobile User Agent for "chiropractors+New York+NY," Google presented me with a page that resembled the following: https://i.sstatic.net/o5ewW.png I am interested in programmatically extracting the phone number linked t ...

Trying to implement a personalized cursor with CSS was unsuccessful

I implemented a custom cursor for my website using an .ico file, but unfortunately it's not displaying correctly. Here's the CSS I used: html, body { cursor: url("images/39020_fPv_icon.ico"), url("images/39020.png"), defa ...

Effortlessly alternate between dual-column and single-column layouts using CSS

Creating a basic two column layout with fixed widths is my current project. <div id='box'> <div id='ontop'>Ontop</div> <div id='column1'>Column1</div> <div id='column2'&g ...

How can I apply an underline effect when hovering over navigation links in a responsive menu?

I've successfully added an underline effect on hover for the navigation menu. However, I encountered an issue with the responsiveness of the menu. When viewed on screens smaller than 600px, the underline effect covers the entire width of the block ins ...

Flex containers positioned next to each other without overlapping and adjusting in relation to one another

I would like to prevent the lower div (which includes an image and a button) from overlapping with the division above it that contains text. So, when the button encounters this upper division, I want it to move down in a way that pushes the image beneath i ...

I cannot seem to get the image to display properly, it keeps getting cut off and

I am trying to add an image to my website: https://i.sstatic.net/giAdT.jpg However, when I try to do this, the full image is not showing: https://i.sstatic.net/VRBlB.png This is the code I am using: <!DOCTYPE html> <html lang="en"> ...

Centering the searchbar in the Bootstrap 4 navbar

I'm finding it difficult to center my search bar inside my navigation. I've experimented with different options like mr-auto and ml-auto or mx-auto, but the element just won't budge. I want to avoid using specific pixel values as it may affe ...

What is the best way to ensure that a child div can expand to fit within the scrollable area of its parent div

I am facing an issue with a parent div that changes size based on the content inside it. When the content exceeds the initial size, causing the parent to scroll instead of expanding, I have a child div set to 100% width and height of the parent. However, t ...

What is the best way to align an extensive text and an image side by side within a table cell?

I am facing an issue with my code. In one of the td elements, I have the user's name image on the left and their first name on the right. The problem arises when the first name is too long, causing the image to disappear. Everything works fine if the ...

Scroll horizontally within each row

I am attempting to create a table with individual horizontal scrolling for each row, dynamically. Currently, my code only allows the entire table to scroll horizontally, not the individual rows. https://i.sstatic.net/3oaPl.jpg <table class="table-mai ...

Trouble with CSS 3 animations arises if dimensions such as width or height are specified

When I apply a width-transition to a div element, the animation works perfectly. However, if I provide both height and width to the same div in CSS, the animation stops working. What could be the issue? On hover, neither color nor animation changes. How c ...

Gallery Pagination using JQuery is not working properly

I am experimenting with the jquery easy paginate plugin on a separate page of my Blogspot. The images are displaying properly, but the pagination and CSS are not showing up. I need to adjust my CSS to make this jQuery pagination work correctly. Can someone ...

What is the best way to position two grid containers side by side?

I am new to using Material UI and Grid, and I'm encountering an issue with positioning two grid containers side by side. My goal is to have an image on the left within a Grid container, and a list of objects on the right. While I was successful in po ...

Set an attribute without replacing any existing class attributes

I have developed a script that updates the class attribute of the body element on my website when an option is selected from a dropdown menu. However, I am facing an issue where it overrides the existing dark mode attribute. The purpose of this script is ...

Align the '::before' item and another div element horizontally at the center using the flex concept in Bootstrap 4

.status-light-red::before{ background-color: rgb(255, 0, 0); box-shadow: 0px 0px 5px 2px rgb(255 0 0 / 75%); -webkit-box-shadow: 0px 0px 5px 2px rgb(255 0 0 / 75%); -moz-box-shadow: 0px 0px 5px 2px rgb(255 0 0 / 75%); } .status-light::before{ co ...

Enhance your slider with tabs using Bootstrap 5

My latest project involved creating a slider using Bootstrap 5. Here is the current result: <head> <title>Bootstrap 5 Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial ...