Several rows of forms that are nested horizontally

I am currently using Bootstrap 4.0 beta 2 and attempting to align a label and form control horizontally in one line by utilizing the form-row and col-* classes.

My next challenge is trying to align two of these form-row structures in one line. You can view my progress on this fiddle (please use HD resolution when viewing). Unfortunately, I am facing an issue where the first input in the first row is not vertically aligned with the input field of the second row. Despite spending hours experimenting with different classes such as form-inline, row, and adjusting margin and padding classes, I have had no success.

I am restricted to only changing the classes applied, so I am reaching out for assistance. Can someone help me resolve this?

Answer №1

It seems that the issue lies in your first column having an additional form-row on it, resulting in a row within another row. It's crucial to avoid nesting rows inside rows; instead, utilize columns within rows. These nested rows introduce extra padding (15px for row and 5px for form-row), which can disrupt the percentage-based widths and offsets when used together.

To resolve this, consider taking your second label + div out of its current form-row and placing it inside the form-row associated with the first label + div. Ensure you adjust the col-md counts for both label+div combinations accordingly. I've assigned them as 2 and 3 respectively, totaling 10 ((2 + 3) + (2 + 3)), matching the total width of your second row (2 + 8).

You can view the updated layout in action through this fiddle: here. Additionally, maximize the snippet below to see the changes:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>

<form>
  <fieldset>
    <div class="form-row">
      <label class="col-form-label col-md-2 col-sm-12">LABEL</label>
      <div class="col-md-3 col-sm-12">
        <input type="text" class="form-control">
      </div>
      <label class="col-form-label col-md-2 col-sm-12">LABEL</label>
      <div class="col-md-3 col-sm-12">
        <input type="text" class="form-control">
      </div>
    </div>
    <div class="form-row mt-2">
      <label class="col-form-label col-md-2 col-sm-12">LABEL</label>
      <div class="col-md-8 col-sm-12">
        <input type="text" class="form-control">
      </div>
    </div>
  </fieldset>
</form>

I hope this resolves your issue! :)

CSS Only Alternative:

In cases where modifying the DOM is not feasible, CSS can offer a solution. Check out this example implemented using CSS only: CSS Solution:

.form-row.mt-2 {
  width: 100%;
}

@media screen and (min-width: 768px) {
  .form-row.mt-2>div {
    padding-left: 4px;
    padding-right: 13px;
  }
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" />

<form>
  <fieldset>
    <div class="form-row">
      <div class="form-row col-md-6 col-sm-12">
        <label class="col-form-label col-md-4 col-sm-12">LABEL</label>
        <div class="col-md-4 col-sm-12">
          <input type="text" class="form-control">
        </div>
      </div>
      <div class="form-row col-md-6 col-sm-12">
        <label class="col-form-label col-md-4 col-sm-12">LABEL</label>
        <div class="col-md-4 col-sm-12">
          <input type="text" class="form-control">
        </div>
      </div>
    </div>
    <div class="form-row mt-2">
      <label class="col-form-label col-md-2 col-sm-12">LABEL</label>
      <div class="col-md-8 col-sm-12">
        <input type="text" class="form-control">
      </div>
    </div>
  </fieldset>
</form>

While the CSS-only approach is viable, adjusting the DOM structure remains the preferred solution. :)

Answer №2

Take a look at this code snippet.

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" />
<form class="col-12">
<div class="form-row">
<div class="col-md-6 col-12">
       <div class="form-row">
        <label class="col-form-label col-md-4 col-12">LABEL</label>
        <div class="col-md-8 col-12">
          <input type="text" class="form-control">
        </div>
       </div>
</div>
<div class="col-md-6 col-12">
       <div class="form-row">
        <label class="col-form-label col-md-4 col-12">LABEL</label>
        <div class="col-md-8 col-12">
          <input type="text" class="form-control">
        </div>
       </div>
</div>
</div>
<div class="form-row align-items-center mt-2">
<label class="col-form-label col-md-2 col-12">LABEL</label>
<div class="col-md-10 col-12">
<input type="text" class="form-control">
</div>
</div>
</form>

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

Add the dropdown list selection to the formData object

Within my HTML code, I have a form with a file input element like this: <input type="file" id="file-select" name="photos" /> To handle this in JavaScript, I append the selected file to my formData object as shown below: formData.append('photo ...

Is there a way to make all DIVs in the same class have the same height as the tallest div?

I have 3 identical divs with an auto height value. How can I set the height of all divs in the same class to be equal to the highest div's height? Same Class Div Thank you in advance. ...

Issues with jQuery UI droppable feature not functioning as expected

I'm currently working on developing a form building tool and I've encountered an issue. Initially, everything was functioning correctly - I could drag my first icon and drop it onto the main form body without any problems. However, when I tried t ...

Are there drawbacks to utilizing large integer values as array indexes?

Simply put. We are discussing javascript here. I am wondering about the potential performance drawbacks, memory issues, and so on when using high value integers as array indexes instead of low value ones. Some programming languages allocate enough memory ...

Unusual spacing between lines on Chrome

I seem to be facing an issue related to line height and block height ratios. My font-size is set at 15px, line height at 1.5em (22.5px), and div height at: 16 lines * 1.5em = 24 em. Everything appears fine in Firefox and MS Edge - the div contains exactly ...

Show a component when there is no input value, then conceal it when an input value is present

Recently, I attempted to position a paragraph absolutely in relation to an input element. My goal is to conceal the paragraph when the input has a value, and reveal it when the input is empty. Here is the code snippet I created, however, it doesn't ...

Ways to navigate a div within an iframe that has been loaded

As I load a page(A) inside an iframe, the HTML structure of the embedded content is as follows: <html><body> <div id="div1"></div> <div id="div2"><button>Hello</button></div> </body></html> The ...

Implementing a Fixed Navbar in VueJS on Scroll

I am seeking help with creating a Fixed Navbar on Scrolling using Vue.js. I initially wrote some jQuery code for this functionality, but now I want to transition it to Vue.js. The updated code can be found in a file named navbar.js. Previous jQuery CODE ...

I struggle to format a classic HTML form dropdown menu that is populated using JavaScript

Hey everyone, I'm having an issue with styling a drop down menu that is populated by JavaScript. I've tried different things but nothing seems to be working. Any suggestions on how I can style the elements in the drop down? <form action="" me ...

Can a line be created using only CSS without the need for an additional HTML element?

There have been many inquiries regarding drawing lines without adding an additional HTML element. Consider this scenario with an HTML div element: <div class="myDiv"></div> Accompanied by the following CSS: .myDiv{ width: 100px; height: ...

Maintain the aspect ratio of DIV or image

I am looking to maintain the aspect ratio of a DIV element, similar to how it's done on Google Maps. <div id="map" style="background-color: grey;width:100%;"> Map here </div> The current setup adjusts the width based on the window size o ...

What is the best way to apply multiple text shadows to a single piece of text using the jQuery ".css()" method?

Is it possible to create multiple text shadows for a single piece of text using the jQuery ".css()" method? I'm currently working on animating the title of a page on my website, which includes adding a 3D effect with multiple shadows. I also want to ...

Utilizing jQuery to dynamically calculate real-time output from user input in a form

My task involves creating a form where the user fills out certain values, and I want to display a calculated value based on those inputs at the bottom of the form. Unfortunately, I'm not seeing any output at all. Here is the HTML form I have attempte ...

"Vertical Line Encoding: A Strategy for Improved Data

Can someone help me with a coding issue I'm facing on my website? I am trying to create three vertical lines to separate images in columns, but every time I exit the editor and preview the site, the position of the lines changes. I'm new to HTML ...

I would like to include two separate divs within one larger div element

I need to include two div elements within a single div, one positioned to the right and the other to the left. <html> <div> <div><button class="mat-button menu-button"> <mat-icon color="primary"> remove &l ...

Display title using a jQuery toolbar

Looking to spruce up a Toolbar with a title using jQuery UI. This is what I have so far: HTML: <div class="demo"> <span id="toolbar" class="ui-widget-header ui-corner-all"> <label>go to beginning</label> <button id= ...

Steps for Aligning the Label and Textbox Horizontally

<div class="form-group row"> <div class="col-6 "> <h4> <label class=""> Can you meet the required amount of money? @(HelpSeeker.money_needed= He ...

Disabling vertical scrollbar in textarea within Bootstrap modal using CSS, jQuery, and C# in .NET MVC framework

I am passing data to the Bootstrap modal using jQuery from my database. The form values can change from a single row to multiple rows. I do not want a scrollbar, but instead I want the textarea element to automatically adjust its height to fit the content. ...

Include some text before and after the bar element

I am attempting to insert text preceding and succeeding the bar, as depicted in the image below. <div class="container"> <div class="progress progress-info progress-striped"> <div id="storage-component" class="bar" style="wi ...

Is there a way to change tailwincss into regular CSS?

I recently received an HTML file that was created using Tailwind CSS and now I am looking for a way to convert everything to regular CSS. I found a tool that converts individual classes, but it would require me to extract and paste thousands of classes o ...