What is the best way to position two form input boxes so that they line up perfectly?

Having a modal with a form (using Bootstrap-Vue and Vue2) where each row contains two fields. If the user does not input a valid value into a field, the state of that field turns false, causing the box to be highlighted in red and displaying an error message below the field. However, the challenge is maintaining alignment between both fields in the same row:

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

Here's how it looks without any errors:

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

The code snippet is as follows:


<div class="text-right">
  <div class="row">
    <div class="col-md-6">
      <div class="modal-label">Field 1</div>
      <b-form-input
       :class="{ 'invalid-form-box': !stateField1(), 'modal-input': true}"
       v-model="course_number"
       type="number"
       placeholder="Field 1"
       :trim="trimInput"/>
      <p v-if="!stateField1()" class="invalid-form-text">
        Not valid input for Field 1
      </p>
      <p v-if="!stateField2() && stateField1()" class="invalid-form-text"></p>
    </div>

    <div class="col-md-6">
      <div class="modal-label">Field 2</div>
      <b-form-input
       :class="{ 'invalid-form-box': !stateField2(), 'modal-input': true}"
       v-model="score"
       type="number"
       placeholder="Field 2"
       :trim="trimInput"/>
      <p v-if="!stateField2()" class="invalid-form-text">
        Not valid input for Field 2
      </p>
      <p v-if="!stateField1() && stateField2()" class="invalid-form-text"></p>
    </div>
  </div>
...
</div>

<style scoped>
  div {
    margin: auto 0;
    direction: rtl;
  }

  input {
    direction: rtl;
  }

  .modal-label {
    margin-bottom: 0.5rem;
  }

  .modal-input {
    margin-bottom: 0.5rem;
  }

  .modal-header .close {
    padding: 1rem 1rem !important;
    margin: -1rem auto -1rem -1rem !important;
  }
  
  .invalid-form-text {
    width: 100%;
    margin-top: 0.25rem;
    font-size: 80%;
    color: #dc3545;
  }

  .invalid-form-box:focus {
    border-color: #dc3545;
    box-shadow: 0 0 0 0.2rem rgb(220 53 69 / 25%);
    background-repeat: no-repeat;
  }

  .invalid-form-box {
    border-color: #dc3545;
    background-repeat: no-repeat;
  }
</style>

To address the alignment issue, attempts were made to replace <p> with <div>, <label>, and <span>. The use of <br> was also tested.

Answer №1

The shifting occurs due to the margin: auto 0; rule in:

div {
  margin: auto 0; ❌ results in layout shift when error message is displayed
}

This rule sets the top and bottom margins of all divs to auto. Look at how the vertical margins of the divs (div.col-md-6 and div.modal-label) change when there is no error present:

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

But, when an error is present, the outer margins adjust to accommodate it:

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

Solution

I believe you only intended to set the horizontal margins to 0. You can achieve that by using:

div {
  margin-right: 0;
  margin-left: 0;
}

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

demo

Answer №2

To achieve the desired outcome, there are two possible methods. My recommendation is to start with the first option.

  1. Adjust the 'invalid-form-text' tag's position to absolute and fix it in the desired location to eliminate any layout shifts.

  2. If you are utilizing flex, consider using align-items: flex-start so that when an error occurs, the alignment will place them at the flex start, giving the appearance of being on the same line. (While this should work, there may be conflicts with other CSS - if it doesn't work, feel free to inquire further)

Answer №3

Utilizing BootstrapVue, you have the ability to accomplish something similar to this:

<b-row align-v="start">
  <b-col cols="6">
    // Insert content for your first column here
  </b-col>
  <b-col cols="6">
    // Add content for your second column here
  </b-col>
</b-row>

Answer №4

To insert this into the parent container, follow these steps.

display: flex;
align-items: center;
justify-content: center;

By applying these CSS styles, the two inputs will be positioned side by side.

Answer №5

This particular piece of code handles the display or concealment of errors.

<p v-if="!stateField2()" class="invalid-form-text">

You can include this paragraph below every input box.

Also, your invalid-form-text class can be defined as follows:

.invalid-form-text{
     visibility: hidden;
}

The key point to note here is that using the visibility style to show/hide elements ensures they still occupy space in the DOM, regardless of their visibility status, thus not disrupting the alignment of other elements.

Answer №6

To address this issue, one method is to adjust the height of the .invalid-form-text element to 0 pixels and ensure its overflow property is set to visible if it's not already.

If you want to incorporate some spacing into the element, consider modifying the margin by using transform: translateY instead. This will prevent any impact on the layout of other elements in the DOM.

.invalid-form-text{
   height: 0;
   overflow: visible;
   margin: 0px;
   transform: translateY(0.25rem);
}

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

Adjusting the width of a DIV element within a table cell

In my ASP.NET application, I am facing an issue with table column widths not rendering as expected in the browser. Despite setting the column width to 100px, it appears as either 96px or 108px. The problem seems to be related to having a div inside the th ...

Click on a table cell in Vue to display its corresponding data in a modal

I have successfully implemented a modal in my Vue component that works well with static text inside it, confirming its functionality. However, when attempting to pass data from the table cell that is being clicked into the modal, I encountered an error st ...

Is there a way to utilize %title in place of $text->title?

Looking to streamline my workflow, I'd like to replace <?php echo $text->title ?> with {%title} in order to achieve the same output. How can I make this change? I'm open to alternative suggestions for accomplishing this as well. ...

Mastering the utilization of the Data object in VueJS with Decorators can be tricky. One common error message you might encounter is, "Class method 'data' expected 'this' to be used."

Error > The class method 'data' should use 'this' but it is not. I encountered this issue and believed I fixed it as shown below: TypeScript Unexpected token, A constructor, method, accessor or property was expected <script lang ...

What is the process for saving selected values from a drop-down list into a database?

Hey there, I'm a newcomer to PHP and ajax. When choosing an option from the drop-down list, a separate div function should appear where I need to insert both the selected option and input data in different divs. Sorry for my poor English. Any help fro ...

Steps to correct alignment in a numbered list when the dot is removed using CSS

Currently, I'm working on developing a rule page for a game. The specific layout requirements dictate that the rules be presented in an ordered list without periods following each number: 1 Rule one 2 Rule two 3 Rule three To achieve this format ...

Footer covering up content on small screen display

I've encountered an issue with a footer that won't stick to the bottom of the page. I'm pretty sure it's just a small oversight on my part, as I've successfully implemented this feature before. I've cleaned up the page to make ...

Guide on integrating Vue.js with Sails.js

I attempted to set up Vue.js as the frontend for Sails.js by following the method outlined in this blog post: . However, I encountered issues with the latest version of Vue. I'm unsure why this is happening – can anyone clarify why it's not wor ...

Some devices experiencing website update issues while others are successful

My website is functioning properly on my laptop and phone, but not on my school iPad (managed by the district). I have already cleared the cache, but the site does not update. It was working fine earlier, and I need to present it next Monday. Please help a ...

Does Xamarin.Forms have a feature similar to html datalist that enables the selection of predefined values while also allowing for the input of free text?

We are currently developing a Xamarin forms application. One of the necessary features is a selection field where users can either choose from predefined options or enter their own text value, similar to using a text field. In HTML, this could be easily ...

Tips for modifying the hue of the hint attribute within vue.js?

`<v-text-field id="loginPasswordId" ref="password" v-model="password" class="login-input" dense :disabled="loading" :hint="hello world" :loading="loading" maxlength= ...

What is the best way to change the active class using jquery?

Excuse me, can I get some help? I'm still learning html and jquery, and I've seen this question asked before, but none of the solutions seem to work for me. I'm trying to change the active class in my navbar when I click on another link. Wh ...

Having trouble adding a button to a GridLayout in my NativeScript application

My attempt to use a Button alongside some Images in a single row within a GridLayout resulted in the Button covering the entire row along with the images. Here is the HTML code I used: <GridLayout columns="4*,*,*,*,*,*" rows="*"> <Button text ...

"Make Vuetify's v-list-group stand out by adding an active state and

Trying to dynamically change the append-icon based on group status (open/close) isn't working as expected: v-list-group( color="success" @click:append="this.marker = !this.marker" :append-icon="marker ? ...

Mapbox GL JS stops displaying layers once a specific zoom level or distance threshold is reached

My map is using mapbox-gl and consists of only two layers: a marker and a circle that is centered on a specific point. The distance is dynamic, based on a predefined distance in meters. The issue I'm facing is that as I zoom in and move away from the ...

How can I create a new PHP table using data from an existing table?

I have a table displayed on my website with the code snippet providedview the table image here Here is the code for generating this table: <?php $query = $db->query("SELECT * FROM bit_exchanges ORDER BY id DESC LIMIT 20"); if($query-> ...

Utilize Angular's ngFor directive to loop through HTML elements and

<div (click)="switchImg($event)" class="containerImgSmall"> <img [ngStyle]="imgs.img1.zero.style" id="1" src="{{ imgs.img1.zero.src }}" alt="" ...

JavaScript button click changes the selected row's color in a DataTable

I am looking to dynamically change the color of a row in my Datatable when a specific button is clicked. Here is the HTML code snippet for the rows: <tr role="row" class="odd"></tr> <tr role="row" class="even selected"></tr> & ...

Issues with Jquery datatable causing column header to overlap the table header

I am facing an issue with my datatable as the table is not displaying correctly. Could someone advise me on how to troubleshoot and fix this problem? Below is the code snippet in question: $(tbName).dataTable({ "sScrollX": "100%", "sScro ...

Ways to detect events even after the object has been swapped out in JavaScript

I am currently developing a JavaScript-based Scrabble game and encountering a puzzling issue. The problem arises when tiles are generated within a tile rack div using a specific function, and an event listener is set up to respond to clicks on the tile div ...