Creating Dynamic Bootstrap 4 Forms with Two Columns

I have a user registration form (Bootstrap 4 - in Razor Template)

Depending on the user's selections, I will hide certain parts of the form. The registration form consists of two steps, and this is the second step.

If a user selects a country without states, I will hide the states field.

Here lies the issue:

When I implement code like here, hiding the states section causes that part to be missing entirely.

To address this problem, I wrote code as seen below using float-left. If one part is hidden, the other fields move and occupy the empty space.

<div class="container py-5 bg-warning">
<div class="row">
    <div id="register" class="col-md-10 mx-auto">
        <form asp-action="Create" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            @if (@Model.StateList != null)
            {
                <div class="form-group col-sm-6 float-left">
                    <label asp-for="State"></label>
                    @Html.DropDownListFor(m => m.StateList, @Model.StateList, "- Please Select -",
                        new {@class = "form-control"})
                    <span asp-validation-for="State" class="text-danger fixed-span"></span>
                </div>
            }
            <div class="form-group col-sm-6 float-left">
                <label asp-for="City"></label>
                <input asp-for="City" class="form-control" />
                <span asp-validation-for="City" class="text-danger fixed-span"></span>
            </div>
            ... <!-- Other form fields omitted for brevity -->
            <button type="submit" class="btn btn-primary px-4 float-right">Register</button>
        </form>
    </div>
</div>

As you can see, there are span elements for error messages beneath each input field. However, if an error occurs, the form layout appears distorted. Do you have any suggestions on how to fix this? Image reference attached https://i.sstatic.net/LpiEI.png

Answer №1

A straightforward CSS solution fixed the issue I was facing.

<style>
    .form-group {
        height: 80px !important;
    }
</style>

Answer №2

Replace float-left with parent row class -

<div class="row">
@if (@Model.StateList != null)
            {
                <div class="form-group col-sm-6">
                    <label asp-for="State"></label>
                    @Html.DropDownListFor(m => m.StateList, @Model.StateList, "- Please Select -",
                        new {@class = "form-control"})
                    <span asp-validation-for="State" class="text-danger fixed-span"></span>
                </div>
            }
            <div class="form-group col-sm-6">
                <label asp-for="City"></label>
                <input asp-for="City" class="form-control" />
                <span asp-validation-for="City" class="text-danger fixed-span"></span>
            </div>
            <div class="form-group col-sm-6">
                <label asp-for="Phone"></label>
                <input asp-for="Phone" class="form-control" />
                <span asp-validation-for="Phone" class="text-danger fixed-span"></span>
            </div>
            <div class="form-group col-sm-6">
                <label asp-for="Company"></label>
                <input asp-for="Company" class="form-control" />
                <span asp-validation-for="Company" class="text-danger fixed-span"></span>
           ...

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

Is it possible for amCharts to show the data properly?

Starting out with amCharts and javascript can be a bit overwhelming. Here is the structure of my html file: <!DOCTYPE html> <html> <head> <link rel="shortcut icon" href=""> <title>chart created with amCharts | amChar ...

how to handle form submission using JavaScript's return method

As a developer, I have created a single page that fetches data from a registration page. On this page, each data row has an "add" and "unfriend" button, with the latter initially disabled. When a user clicks the "add" button, a prompt box appears asking ...

What could be the reason behind the background-image not functioning properly in this case?

Would you mind taking a look at this link? http://jsfiddle.net/BbkBf/ I have a couple of questions: 1. Why is the background-image not displaying for the first one? (Even if the a tag is empty, it should still work) 2. Why isn't the image being ...

Using the Table-multiple-sort feature in boostrap-table is not functioning properly when there are multiple tables present on a single page

I have implemented bootstrap-table along with the extension table-multiple-sort. The issue I am facing is when I include two tables on a single page (with the second table within a modal window), the multisort feature does not seem to work on the second ta ...

Using jQuery to access the value of the CSS property "margin-left"

I created a JavaScript game with moving divs that have different transition times. The game includes a function that is called every 1/100 seconds, which checks the position of the divs using: $("class1").css("margin-left") Here's the strange part: ...

Animation for Images in Angular v1.2

After reviewing tutorials and previous questions, I have been trying to pinpoint where my code is going wrong in applying an animation to transition between the images I want to display. The ng-show function effectively displays the selected picture, but ...

CSS radio button not showing up on Safari browser

I am encountering an issue with my vue app where it functions correctly on Google Chrome but experiences problems on Safari. Specifically, the issue arises with components containing radio buttons. Upon clicking on a radio option, the selected color (blue) ...

Why won't the div move when I click it?

Could you please explain why my JavaScript code isn't functioning as expected? The intended behavior is for the 'mark' div to move to the current mouse coordinates upon clicking within the map. ...

Alignment of button within bootstrap column is skewed when screen size is reduced

My button is perfectly centered in one of my bootstrap columns, but things start to get messy when the screen size shrinks and the button overlaps with another column containing text. Adjusting the margins only seems to make things weirder. .center-btn- ...

Learn how to access an array within an object using JavaScript and an API URL

Trying to fetch data from the swapi API to display film titles using jQuery. Below is the JavaScript code: $(function(){ function promiseTest(){ return $.ajax({ type: 'GET', url: 'https://swapi.co/api/people/', } ...

When a page is changed, the Vue.js Active Menu color remains enabled

Check out my website at . I want to customize the navigation bar so that only the active page's navbar li is colored in red. <div class="navigation-items"> <ul class="nav-list"> <li class="nav-item"><nuxt-link to="/en" ...

Troubleshooting SCSS Issues in NextJS

Can anyone help me with debugging SCSS in NEXT.JS? I've been trying to figure it out but haven't had any luck. When I use @debug on a SCSS module, nothing shows up in the console. Do I need to configure something in the next.config.js file or is ...

When attempting to specify the path in the angular.json file, Angular encounters difficulty accessing Bootstrap from the node_modules directory

I have been attempting to integrate Bootstrap into my Angular project. Firstly, I used npm install --save bootstrap to add Bootstrap to my project. Following that, I installed jQuery as well. I then specified the path for Bootstrap. Displayed below is an ...

Using a CSS nth-child selector to eliminate the bottom margin of the final row

As I work on creating lists that are displayed in columns of two, each .customer-review-container includes a margin-bottom CSS property like this: <div class="col-md-6"> <div class="customer-review-container"> </div> <!-- en ...

Place the item at the center

My question may be simple, but I'm struggling to find the solution. I have a slider with an image in the middle and want to display some text alongside it like this: https://i.sstatic.net/v2Rck.jpg However, I can't seem to get the text to align ...

Is it possible to prioritize CSS selectors in a specific sequence?

I possess a pair of div elements: div class="one two three" div class="three two one" Is it probable to formulate a CSS regulation that exclusively focuses on ".one.two.three" and does not encompass ".three.two.one" ? ...

Swap out the div block with a new one

I am currently facing an issue with my JavaScript code. The code is supposed to remove block1 and replace it with block2 when an onclick function is triggered. function buyerclick() { div = document.getElementById('block2'); div.style.displa ...

Revamping Tab Styles with CSS

Currently, I am working on a mat tab project. The code snippet I am using is: <mat-tab-group> <mat-tab [label]="tab" *ngFor="let tab of lotsOfTabs">Content</mat-tab> </mat-tab-group> If you want to see the liv ...

Troubles arise when trying to encapsulate a ReactJS embedded application with Material ui, causing issues with the

Currently, I am integrating a ReactJS application using Material UI styled components within a Wordpress page. To achieve this, I am utilizing webpack for transpilation of the JavaScript. After generating the bundle for the embedded version of the applica ...

Steps to hide a div after it has been displayed once during a user's session

After a successful login, I have a div that displays a success message and then hides after 4 seconds using the following JavaScript code: document.getElementById('success').style.display = 'none'; }, 4000); While this functionality wo ...