Guide on aligning input and labels side by side in Bootstrap forms using CSS only

I've exhausted all the solutions provided here, including referencing this Bootstrap horizontal-form link. However, nothing seems to be effective. I am open to trying any method at this point, even if it involves just modifying the CSS. My main objective is to have the label aligned in the same line as the input field. Essentially, I want all the labels and inputs to be displayed in a single row. Additionally, I would like to maintain the bootstrap form-control style.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<div id="" class="row py-2  rowRecordBorder" data-toggle="tooltip" data-original-title="">
  <div class="app-col col-12 col-md-2 bottom-align ">
    <label class="active">Storage Phone #</label>
    <input type="tel" id="t887" name="t887" onchange="buildTejPost('t887', 1);" value="100 100 1000" class="form-control" onfocus="$('#t887').inputmask({'mask': '(999) 999-9999'});" onblur="phoneNumberCheck(this);" style="border-color: green;">
  </div>
  <div class="app-col col-12 col-md-2 bottom-align ">
    <label class="active">Storage Fax #</label>
    <input type="tel" id="t888" name="t888" onchange="buildTejPost('t888', 1);" value="000 000 0000" class=" form-control" onfocus="$('#t888').inputmask({'mask': '(999) 999-9999'});" onblur="phoneNumberCheck(this);">
  </div>
  <div class="app-col col-12 col-md-2 bottom-align ">
    <label class="active">Storage Pickup Time Open</label>
    <input type="text" id="t889" name="t889" onchange="buildTejPost('t889', 1);" value="9:00AM" class=" form-control" maxlength="128">
  </div>
  <div class="app-col col-12 col-md-2 bottom-align ">
    <label class="active">Storage Pickup Time Close</label>
    <input type="text" id="t890" name="t890" onchange="buildTejPost('t890', 1);" value="3:00PM" class=" form-control" maxlength="128">
  </div>
</div>

Answer №1

If you want to quickly arrange labels and form controls on a single horizontal row, you can make use of the .form-inline class. Check out the documentation here: https://getbootstrap.com/docs/4.5/components/forms/#inline-forms

<div class="form-inline">
    <label />
    <input />
    <label />
    <input />
    <label />
    <input />
    <label />
    <input />
</div>

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

demo: https://jsfiddle.net/davidliang2008/edy16zx5/12/


Alternatively, you can utilize the Grid system to create columns, with each column containing a label and an input. To ensure the label and input appear on the same line within each column, you can set the column display to flex box:

<div class="row">
    <div class="col-12 col-md-2 d-flex flex-row flex-nowrap align-items-center">
        <label />
        <input />
    </div>
    ...
</div>

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

For more information on Flex utilities, visit: https://getbootstrap.com/docs/4.5/utilities/flex/

  • d-flex: sets display to flex for flexbox behaviors
  • flex-row: defines direction as row
  • flex-nowrap: prevents wrapping when overflowing
  • align-items-center: aligns items to the center

demo: https://jsfiddle.net/davidliang2008/edy16zx5/16/


In either case, as a developer, you need to consider how to position those input elements, especially in tight spaces. Bootstrap won't automatically handle that for you.

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

Creating an image slideshow with a looping JavaScript array: Step-by-step guide

One issue with the program is that when it runs, only the last array value image is displaying on the screen while the other images are not. Below is the HTML code: <img id="mg" alt="image not found"> And here is the JavaScript code: var images=[ ...

How may I utilize CSS to generate a dynamically resizing rotated triangle that adjusts based on the text content?

Looking to add a dynamic badge in the top left corner of a div that adjusts its size based on the text content. Check out this example: http://jsfiddle.net/sbick/npxaasht/ <article> <div class="triangle"> <span>NEW</span ...

Tips for adjusting the alignment of a navbar-brand and navbar using Bootstrap 5

I am currently using bootstrap to develop a webpage. I have a navigation bar at the top of the page and I would like my navbar-brand to be positioned on the right side, while my navbar links should be on the left. However, when I try to implement this, it ...

At what point are DOMs erased from memory?

Recently, I've been working on an application that involves continuous creation and removal of DOM elements. One thing I noticed is that the process memory for the browser tab keeps increasing even though the javascript heap memory remains steady. To ...

Utilize the power of jQuery's AJAX capabilities

Is it normal for a jQuery AJAX request to take between 1-2 seconds before receiving a response? Is there any way to minimize this delay? The response type is JSON and the HTML code is small. Thank you! ...

Opting for css3 translate over using position: left for positioning elements

Why Using translate() for Element Movement is Superior to Pos: abs Top/left Read the full article Lisa Anderson I struggled with implementing a CSS3 translate animation, so I opted for a jQuery solution that achieves the same effect. Click on the X icon . ...

Running a continuous JQuery function loop

I need assistance with creating a continuous image fade in and out effect. Currently, my code looks like this: $(document).ready(function(){ $(".slider#1").delay(1000).fadeIn(2000); $(".slider#1").delay(1000).fadeOut(2000); $(".sli ...

`Erase content from a field once text is typed into a different field`

I have a Currency Converter with two input fields and a button. I enter the amount to be converted in the first field, and the result of the conversion appears in the second field. My question is: How can I automatically clear the second field when I type ...

Make the navigation bar stay at the top of the page when scrolling past another element with a top position of 100vh

Trying to explain a unique concept here. I want a nav bar fixed in the position of top:100vh, so that as I scroll down and reach the next section, the navbar sticks at the top rather than staying stuck at the beginning with position:fixed top:0. The aim is ...

consistent height across the div when expanding

There is a recurring issue I encounter where it takes too long for me to solve the problem. My goal is to ensure that the bootstrap boxes at the bottom of the page have the same height. I attempted to set a fixed height in pixels using my CSS, but this cau ...

What is the best way to accommodate 4 columns within a 3-column HTML table layout?

I am facing a challenge with my table layout. Normally, it has 3 columns, but I am trying to figure out how to fit 4 cells into one row while maintaining the same width. Any suggestions on how to achieve this? ------------- | 1 | 2 | 3 | ------------- | ...

Picture hidden beyond the words

I am trying to achieve a layout where an image is displayed behind an H1 tag, with the width of the image matching the width of the text. Here is my code: <div style="display: inline;"> <img src="http://img585.imageshack.us/img585/3989/m744. ...

What is the best way to have my program switch out a string with the pressed key in a particular portion of the string?

I'm currently developing a hangman game using JavaScript. I am facing an issue where I need to replace the "-" with the guessed letter by the user in the correct position it belongs to within the word. While I can add the letter at the beginning or en ...

Sync Data Automatically from SQL Database

For the past two months, I've been researching how to achieve an effect similar to the auto-updating sales on the page. So far, I haven't had any luck. I do have a PHP file that counts the number of results in a database and displays them as a n ...

Mastering the art of utilizing the LESS preprocessor with React Create-React-APP

Is it possible to implement the LESS preprocessor in my create-react-app project? In my React code, I have the following: ReactDOM.render( <form> <input type="email" data-info="Enter Email" pattern="/\S+@\S+\.\S+/ ...

How to customize the arrow color of tooltips in Bootstrap 4

After spending an hour searching online, I still can't figure out how to change the tooltip arrow color to red. None of the code snippets I found seem to work for me. My latest attempt was: .tooltip-arrow { border-right-color: red; border-left-c ...

Attach the button to the input tag so that they are always connected

While working on the layout for my webpage, I encountered an issue with an input area and a textarea that I wanted to clone. Everything was functioning correctly, however, I wanted the "+" button to remain attached to the input tag even when the screen siz ...

Tips for adding styles to the first and last elements of a printed page

I have created a dummy code example that generates multiple paragraph elements with some text in it. However, I need to add borders to the first and last elements to enhance the design. Here is the code snippet: <!doctype html> <html> <he ...

The method .slideUp() is not functioning as intended

My current project involves a page structure that looks like this: <div id="wrapper"> <div id="page_banner"> <canvas id="voucher_canvas"></canvas> <div id="div_voucher_img"><img id="voucher_img" src="" ...

Updating a section of a webpage using jQuery Mobile

I'm currently facing an issue with modifying a specific section of my webpage. Although this problem has been discussed before, I haven't found a satisfactory solution yet. The element in red on the page needs to change dynamically based on the ...