Aligning form fields in a HTML form using Bootstrap

In my extensive form, several fields within the setup encounter the same issue. Specifically, a checkbox below 'contact telephone number' causes the surrounding content to become misaligned. https://i.sstatic.net/8qI8Z.png

My attempts to resolve this by setting margin-bottom to 0 have only slightly reduced the gap but it remains noticeable.

<div class="form-group">
    <label for="contact_tel" style="margin: 0">Contact Telephone Number <span style="color:red;">*</span></label>
    <small style="margin: 0"><br>Same as your main telephone number?</small>
    <input type="checkbox" name="sameNum">
    <input type="text" class="form-control input-sm"/>
</div>

Removing the <br> places the checkbox on the same line as the label.

I've also tried inserting a placeholder beneath the contact name field like so:

 <small><br>&nbsp;</small>

However, this solution looks quite unsightly https://i.sstatic.net/Rshp8.png

What is the most effective approach to ensure all fields remain properly aligned in the form?

Answer №1

I came across what seems to be a common pattern.
To maintain the same alignment as that format, simply including d-flex align-items-XXX is an effortless solution.(using flex)

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<form>
  <div class="form-row d-flex align-items-end">
    <div class="form-group col-6" style="background-color: #F6CEE3;">
      <label for="test1">Test1</label>
      <input type="text" class="form-control " id="test1" placeholder="test1">
    </div>
    <div class="form-group col-6"  style="background-color: #A9E2F3;">
      <label for="contact_tel" style="margin: 0">Contact Telephone Number <span style="color:red;">*</span></label>
      <small style="margin: 0"><br>Same as your main telephone number?</small>
      <input type="checkbox" name="sameNum">
      <input type="text" class="form-control input-sm"/>
    </div>
  </div>
  <div class="form-row d-flex align-items-start">
    <div class="form-group col-6" style="background-color: #F6CEE3;">
      <label for="test2">Test2</label>
      <input type="text" class="form-control " id="test2" placeholder="test2">
    </div>
    <div class="form-group col-6"  style="background-color: #A9E2F3;">
      <label for="test3">test3</label>
      <input type="text" id="test3" class="form-control input-sm" placeholder="test3"/>
      <input type="checkbox" name="sameNum">          
      <label for="contact_tel" style="margin: 0">Contact Telephone Number <span style="color:red;">*</span></label>
      <small style="margin: 0"><br>Same as your main telephone number?</small>
    </div>
  </div>
</form>

Answer №2

Alternatively, you have the option to utilize the Bootstrap grid system:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<div class="container">
  <div class="row my-4">
    <div class="col">
      <div class="jumbotron">
        <h1>Customizing HTML form field alignment with Bootstrap 4</h1>
        <p class="lead">by djibe.</p>
        <p class="text-muted">(thanks to BS4)</p>
        <p>
          Find the answer on Stackoverflow: <a href="https://stackoverflow.com/questions/51632452/html-bootstrap-form-fields-alignment" target="_blank">https://stackoverflow.com/questions/51632452/html-bootstrap-form-fields-alignment</a>
        </p>
        <h2>
          Tutorial
        </h2>
        <h2>
          Demo
        </h2>
        <div class="row">
          <div class="col-sm d-flex flex-column">
            <div class="form-group d-flex flex-column flex-fill justify-content-between">
              <label for="contactname">Contact Name <span class="text-danger">*</span></label>
              <input type="text" class="form-control" id="contactname">
            </div>
          </div>
          <div class="col-sm">
            <div class="form-group">
              <label for="contact-tel">Contact Telephone Number <span class="text-danger">*</span></label>
              <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" id="customCheck1">
                <label class="custom-control-label mb-1" for="customCheck1">Same as Main Telephone Number</label>
              </div>
              <input type="text" class="form-control" id="contact-tel">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm">
            <div class="form-group">
              <label for="contact-position">Contact Position <span class="text-danger">*</span></label>
              <input type="text" class="form-control" id="contact-position">
            </div>
          </div>
          <div class="col-sm">
            <div class="form-group">
               <label for="contact-2">Accounts Contact Name <span class="text-danger">*</span></label>
              <input type="text" class="form-control" id="contact-2">
            </div>
          </div>
        </div>

      </div>
    </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

Challenges encountered with the navigation bar in Bootstrap 4

I am facing an issue with positioning the logo on the left side of the top navigation bar. Every time I try to implement this, it ends up breaking the frontend design. Here is a snippet of the code: .top-line { border-bottom: 1px solid #cecece; } ...

Can a low-opacity gradient be merged with a solid background color?

Can anyone help with this code snippet? Here's the link table { height: 200px; width: 200px; background-color: #444557; /* seems to be hidden by the gradient */ /* Here is the CSS for a gradient background */ /* Source: http://colorzilla ...

Issue with grid breakpoints for medium-sized columns and grid rows not functioning as expected

I'm working on building a grid layout from scratch in Vue.js and have created my own component for it. In my gridCol.vue component, I have defined several props that are passed in App.vue. I'm struggling to set up breakpoints for different screen ...

What causes my divs to change position when using text <h1>?

When I have multiple divs grouped together inside another div, the placement of my h1 tag shifts downwards. How can I prevent this from happening and ensure that the text is added without any unwanted shifting? This issue only occurs when there are two o ...

Make multiple images in one row resize proportionally instead of wrapping them to the next line

My phone's screen is small and I want the three images to remain in the same row. In case they don't fit, they should shrink proportionately to maintain alignment (refer to image at the bottom). Consider the code snippet below: <div id="exam ...

spontaneous angular rotations in a constantly shifting CSS environment

Is it possible to apply a random CSS rotation to an image just once, rather than having it continuously spin when hovering over a leaflet map? http://jsfiddle.net/J03rgPf/mzwwfav4/3/ I want the random CSS rotation to be set only one time. How can I achie ...

Hover events in the browser are currently operating at a sluggish pace

When I hover over a photo, an overlay is supposed to appear. However, if I move the mouse quickly (from the top to the bottom of the page, for example), the overlay remains visible even after I stop hovering over the photo. I have tried different event ha ...

Utilize Python to extract a table from an HTML document

I would like to retrieve a table from an HTML file. Below is the code-snippet I have written in order to extract the first table: import urllib2 import os import time import traceback from bs4 import BeautifulSoup #find('table',{'class&ap ...

Every time I attempt to load the table, I encounter an error

Encountering errors when attempting to load the table: 'Uncaught ReferenceError: usersArray is not defined at loadUsers (trgames.js:20:17) at trgames.js:22:1' and 'Uncaught TypeError: Cannot set properties of null (setting ...

What is the process of Defining an Element Based on Two Attributes in C#?

Is it feasible to specify a web page element based on two attributes? For example, I can locate the element by its innertext Element imagePage = ActiveBrowser.Find.ByContent(pageNumbers[p],FindContentType.InnerText); Alternatively, I can identify an ele ...

Ways to ensure the menu remains fixed on a fluid website

Is there a way to prevent the main-menu from moving to a second line when resizing the page down, while still maintaining fluidity? Thank you, Ken ...

Rotate each row of the table in sequence with a pause between each flip

I have a table with 3 columns and 10 rows. I would like to flip each row one by one, where each row contains data on both the front and back sides. The flipping animation should be similar to the example provided in this link, but the flipping should sta ...

Graphic aligned with textual content

Within my table cell resides text and an accompanying image icon. However, the presence of the image causes the text to shift downwards, not aligning in the center as desired: I am now seeking a solution that will allow the text to be centered within the ...

Tooltips experience issues when interacting with elements that do not utilize the :active state

$(function () { $('[data-toggle="tooltip"]').tooltip() }) <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" ...

jQuery's event delegation feature fails to work in conjunction with AJAX requests

I'm currently facing issues with jQuery's on event delegation: Below is the code snippet I am working with: HTML portion: <body> <!-- Page Content Wrapper--> <div id="sb-site"> <div id="overlay">& ...

Tips for aligning 2 divs in the same position?

I am working on a slider feature that includes both videos and pictures. I need to ensure that the videos are hidden when the device width exceeds 600px. Here is the HTML code snippet: <video class="player__video" width="506" height="506" muted preloa ...

What should be displayed if there are no search results?

My form for filtering a database is working perfectly in the first section: $result=$dbh->prepare("SELECT * FROM performers WHERE accent='$txt_accent' AND hair='$txt_hair' AND gender='$txt_gender' AND location='$txt_l ...

Switch the orientation of the unordered list from horizontal to vertical in a dynamic way

Perhaps a repeated inquiry, yet I have been unable to discover a solution for this issue... I am dealing with a div element (referred to as master) that I do not have the ability to adjust in terms of width or height. Contained within the master is anoth ...

Numerous slideshows using identical scripts

I am facing an issue with my code for multiple slideshows. Currently, it works fine for a single slideshow but when I have multiple slideshows and mouse over any of them, all the slideshows start running due to sharing the same class. However, if I try to ...

Is it possible to make side elements expand to occupy the remaining screen space beyond the container?

This is what it's supposed to look like: The goal here is to have the red element on the left expand to fill the remaining space beside its content, as well as the grey on the right. I am utilizing bootstrap which means the center elements occupy 117 ...