Tips on how to align a label in the middle of a line using HTML and Bootstrap

I'm having trouble creating an inline form with a text and select input. Despite using Bootstrap 5, I can't seem to achieve the desired appearance.

<!-- Bootstrap-5 -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d3dedec5c2c5c3d0c1f1849f819f83">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">


<!-- Body -->
<div class=r ow>
  <div class="col-lg-12">
    <div class=r ow>
      <div class="col-md-2">
        <label for="the_select" class="form-label">Key Name</label>
      </div>
      <div class="col-md-2">
        <select class="form-select" id="the_select">
          <option selected>Open this select menu</option>
          <option value="One">One</option>
          <option value="Two">Two</option>
          <option value="Three">Three</option>
          <option value="Other">Other</option>
        </select>
      </div>
      <div class="col-md-2">
        <label for="value_field_id" class="form-label">Variable Name</label>
      </div>
      <div class="col-md-2">
        <input type="text" class="form-control" id="value_field_id">
      </div>
    </div>
  </div>
</div>

This code contains my current HTML layout with Bootstrap 5. However, I'm facing an issue where the label is far too distant from the corresponding input/select and is not vertically centered with it. Is there a simple solution to correct this? I've attempted adjusting the display property in the label's style, but it doesn't seem to have any effect.

Answer №1

Here are 2 new Bootstrap Flex class tips:

  1. Center both flex items vertically.
  2. Shift the content of the first flex item's <label> to the right border of its parent flex item.
<div class="row align-items-center">
  <div class="col-md-2">
    <label for="the_select" class="form-label ms-auto">Key Name</label>

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

Collapsed Bootstrap navigation bar on mobile devices

Hello everyone, I am currently using the latest version of Bootstrap for my website. However, I have encountered an issue where the navigation collapses on mobile view. Could anyone provide assistance? Website: Below is a snippet of my HTML code: <he ...

Fetching the latest phase using BS-stepper

Does anyone know how I can retrieve the current page using bs-stepper? I've been struggling with this issue for quite some time now. This is what my code looks like: <script> var stepper = new Stepper($('.bs-stepper')[0]); </sc ...

Dealing with CSS Overflow Issues: Fixing the Scroll Bug

My current project involves creating a page with fixed links at the top and submit buttons at the bottom. The content in the middle is scrollable using overflow:auto. I've noticed that when I resize the browser window, the scrollbar slowly disappears ...

The Jquery Addclass function is not functioning properly

I have been struggling with this code as it seems to not be working in any browser, including IE, Chrome, and Firefox. I suspect that the system should add a class, so I checked the DOM using Chrome's console. I did see the class being added, but ther ...

What might be the reason for the misaligned positioning of the ThreeJS canvas when the browser window is resized?

My goal is to perfectly align a ThreeJS canvas (with a blue background and yellow text) within the transparent cut-out of the green image. So far, everything is working fine, but I've noticed that resizing the browser window or zooming causes the canv ...

The periodLookup array does not have a defined value for periodStr. Why is this error being caught?

Here is a method that I am working with: set_period_help_text: function(periodInput){ var metric = MonitorMetric.getSelectedMetric(); var periodStr = $('select[name=metric_period]').val(); var datapoints = Number(periodIn ...

Tips for updating the left positioning of a Div element on the fly

Although my title may not fully explain my goal, I am dealing with dynamically created div elements. When a user triggers an ajax request to delete one of the divs, I want to remove it from the DOM upon success. The issue arises in adjusting the layout aft ...

Adjust the image to stretch and crop appropriately to perfectly fit the specified dimensions

I have a div with an image inside of it, and the overflow of the div is set to hidden so that the image will be cropped if it exceeds the width or height. It was working correctly, but sometimes it doesn't. What could be causing this issue? Here is th ...

Discovering the required rotation angle for an object to directly face another using JS HTML5 CANVAS

Hey there, I'm currently working on a game project in Javascript where I have a player and a cursor. I already know the positions of both objects, but what I need help with is determining the angle in degrees or radians that the player needs to rotate ...

How come the hamburger icon is not visible in Bootstrap?

I'm facing an issue where my navbar collapses and the hamburger icon disappears, even though it still functions properly. Here's the actual image of the hamburger icon I have attempted the following solution, but it still doesn't seem to wo ...

The email generated by Laravel does not conform to HTML formatting standards

After creating an email using markdown in Laravel, I noticed that the email content received was not displaying correctly. The HTML tags were not being rendered properly. You can view my email here. Any help on this issue would be greatly appreciated. I h ...

The PHP code is experiencing issues on the enquiry/callback form, but it is functioning correctly on the contact form

I'm having trouble understanding why I am only getting the telephone number and missing the name and email fields when submitting a form. I have tried making changes, but I can't get all three fields to work properly. <aside clas ...

MUI: Set the height of the div element to dynamically expand based on its content

I am currently working on styling a React app with MUI and I need help figuring out how to make a div's height adjust to its content. Specifically, I have a Card component that contains a Button. Upon clicking the Button, the content below the Card ge ...

JavaScript validation function stopping after the first successful validation

Script: NewsletterValidation.js function formValidation() { var fname = document.getElementById('firstName').value; var lname = document.getElementById('lastName').value; var pnumber = document.getElementById('phoneNumb ...

Storing data from one file into a different directory in PHP

As a php beginner, I have discovered that the most effective tool for working with MySQL databases and files is php. Currently, I am trying to retrieve a file with the name 'file_name'. <input type=file name="file_name"> and then save th ...

Retrieve the <style> tag response and inject it into the head section of the HTML using React

Although I am aware that this may not be the best practice, it seems to be the most suitable solution for this particular case. The server response contains something like this: <style id="styles">.color_txt{color:green;}</style> I am attempt ...

Div element positioned outside of another div element

I hate to bug you with more css issues, but I'm really struggling with this one. The problem I'm facing is that a sub div is overflowing the boundaries of its parent div. I tried using: display: inline-block;` but then the outer div goes haywir ...

Problem with CSS Classes Styling

Having an issue with CSS in Windows Notepad: <!DOCTYPE html> <html> <head> <title>UniqueScience</title> <style> #title { display: block; ...

What is the process for updating a particular div element?

I am currently developing a webpage that allows users to select an item, and the relevant information will be displayed. On this page, I have incorporated two buttons: btnBuy0 and btnBuy1. The functionality I am aiming for is that when BtnBuy0 is clicked ...

Preventing Height Stretch in CSS Flex-Wrap: A Guide

Is there a way to eliminate the large gaps between box3, box1 and box4, box6? How can we ensure each box adjusts its height dynamically? .wrap { display: flex; align-items: baseline; align-content:flex-start; justify-content: space-between; fl ...