Arrange three input fields horizontally with labels positioned above each

I am facing a minor problem with aligning three input fields in the same row while maintaining equal spacing between each of them.

I attempted to accomplish this using display: flex and space-between, but then the label appears to the left of the input instead of above it.

My desired outcome is as shown in the image below: https://i.sstatic.net/ic1rA.png

If there are more inputs, I would like them to "break a row" and begin a new row of three inputs.

How can I achieve this?

Here is a snippet of the code:

/*Stage 3 input group*/
.dInput{
  margin-top: 15px;
}

.dInput label {
  display: block;
  padding: 6px;
  color: #f8f9fa;
}

.dInput input {
  padding: 5px;
  margin-left: 5px;
  width: 33%;
  background-color: #495057;
  border-radius: 4px;
}
<div class="dInput" v-if="activeStage == 3">
  <label for="name">Username*</label>
  <input type="text" id="name">
  <label for="name">Username*</label>
  <input type="text" id="name">
</div>

Answer №1

It seems like this is the solution you are looking for. I trust that my code will be beneficial to you.

/*Custom CSS Input Group*/
.dInput{
  display:flex;
  justify-content:space-between;
  align-items:center;
  width:100%;
  margin-top: 15px;
}

.dInput label {
  text-align:center;
  display: block;
  padding: 6px;
  color: #000;
}

.dInput input {
  padding: 5px;
  margin-left: 5px;
  background-color: #495057;
  border-radius: 4px;
}
<div class="dInput" v-if="activeStage == 3">
  <div><label for="name">Username*</label>
    <input type="text" id="name"></div>
  <div><label for="name">Username*</label>
    <input type="text" id="name"></div>
  <div><label for="name">Username*</label>
    <input type="text" id="name"></div>
  
</div>
<div class="dInput" v-if="activeStage == 3">
  <div><label for="name">Username*</label>
    <input type="text" id="name"></div>
  <div><label for="name">Username*</label>
    <input type="text" id="name"></div>
  <div><label for="name">Username*</label>
    <input type="text" id="name"></div>
  
</div>

Answer №2

Utilizing the power of display: flex

.dInput {
  margin-top: 15px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.input-wrapper {
  display: flex;
  flex-direction: column;
  max-width: 33%;
  flex: 33%;
}

.dInput label {
  display: block;
  padding: 6px;
  /* color: #f8f9fa; */
}

.dInput input {
  padding: 5px;
  margin-left: 5px;
  width: 33%;
  background-color: #495057;
  border-radius: 4px;
}
<div class="dInput">
  <div class="input-wrapper">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="input-wrapper">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="input-wrapper">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="input-wrapper">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="input-wrapper">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
</div>

Answer №3

Using CSS grid:

/*Custom input group style using CSS Grid*/
.dInput{
  margin-top: 15px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  justify-content: space-between;
  justify-items: center;
  background-color: violet;
  gap: 1em;
  padding: 1em;
}

.dInput label {
  display: block;
  color: #f8f9fa;
}

.dInput input {
  padding: 5px;
  background-color: #495057;
  border-radius: 4px;
  max-width: 8em;
  margin-top: 1em;
}
.dInput-item {
  justify-self: center;
  text-align: center;
}
<div class="dInput" v-if="activeStage == 3">
  <div class="dInput-item">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="dInput-item">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="dInput-item">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div class="dInput-item">
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
</div>

Answer №4

Enclose each label and input within their own div containers

<div class="dInput">
  <div>
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div>
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
  <div>
    <label for="name">Username*</label>
    <input type="text" id="name">
  </div>
</div>

CSS Style:

.dInput{
  margin-top: 15px;
  display:flex;
  justify-content: space-between;
}

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

What is the best way to create a number input field that also accepts text input?

The goal I have in mind is to create an input field that will contain text like "100%", "54vh", or "32px". I am attempting to utilize the number input's up and down arrows, while still allowing the user to edit the text. However, it should only allow ...

JavaScript incorporates input range sliding, causing a freeze when the mouse slides rapidly

Currently working on a custom slider and encountering an issue. When quickly moving the mouse outside the slider's range horizontally, exceeding its width, the slider doesn't smoothly transition to minimum or maximum values. Instead, there seems ...

The customized styling for Material Angular does not seem to properly apply to <md-card> elements within ui-views

I am trying to achieve a specific effect on the ui-views <md-card id="sidebar" flex="20" class="sche-card" layout-padding="true"> <div ui-view="card" autoscroll="false"></div> </md-card> However, I am facing an issue where the car ...

Tips for customizing Material-UI Switch button appearance

Is there a way to remove the box-shadow on the thumb of the Switch button when it's disabled? I've tried various methods like adding the box-shadow in the switchBase and removing it from the thumb, but it affects the parent element as well. Anoth ...

Ensuring Consistent Height for Bootstrap Card Headers

I am currently working with bootstrap cards on two different web pages. The challenge I am facing is that on one page, the header text has a fixed height so I can easily match their card-header height using min-height. However, on the second page, the card ...

Preventing the window from resizing while an AJAX query is in progress

I have a script in jQuery that serves as a search tool and also has the capability to resize an element to match the screen height minus 40 pixels whenever the window is resized. However, I am looking for a way to turn off the resizing feature when a searc ...

Upgrading from Vue.js 1.x to 2.x: Understanding the Compilation Process

Code snippet 1: It seems to be functioning properly. this.$compile(this.$els.ajaxcontent); Migration notes: this.$compile(this.$refs.ajaxcontent); // Issue encountered: $compile function not found. Vue.compile(this.$refs.ajaxcontent); // Problem: temp ...

Choose and output all the tables contained within the database

I've checked my code and I don't see any errors, but for some reason, no data is being displayed on my page. Can anyone offer some help or insight into what may be wrong with my code? Thank you in advance! <?php if(logged_in() === tru ...

What is the best way to start tiny-slider automatically once the video has ended?

I am currently using the tns-slider plugin and have a setup with 3 slides (2 photos and 1 video). <div class='tiny-slider'> <div class='slide slide1'> <div class='video-slide'> <video id=&qu ...

The styling of a CSS class in Internet Explorer may not be applied correctly if there are multiple elements sharing the same class name

For nearly a full week now, I've been plagued by a persistent issue! Here's the situation: I have 6 step tabs - step 1, step 2, and so on. Each tab has a CSS class called "locked" and "active." "Locked" - this style features top: 3em;, causing ...

Passing slots to child components within a VueJS application - A helpful guide

Within my application, I frequently utilize a list and list_item component. To provide a general idea: contact_list.vue <template lang="pug"> .table .table-header.table-row .table-col Contact .table-col Info .tabl ...

Managing various types of click events on a single element

I'm working on a solution to attach 3 different handlers to an element without triggering each other. I found some guidance at https://v2.vuejs.org/v2/guide/events.html <div @click.ctrl="methodA" @click="methodB" @contextmenu.ct ...

Is there a solution for the error I encountered while processing data in Nuxt.js?

Looking to create a template with a password input and checkbox functionality. When the checkbox is checked, I want the input field to display text instead of masked as a password. <!--template--> <template> <div> <div ...

Optimizing Backend Access with Laravel and Vue JS: How to Choose the Most Effective Approach

Currently, I am utilizing Laravel API passport to handle authentication in my Single Page Application (SPA) built with Vue. At the moment, whenever I need to access the backend server, I have to include a header in order to be allowed through the protected ...

Enhance text by hovering over it

I am currently working on implementing a unique feature using jQuery and CSS. Rather than just inheriting the width, I want to create a magic line that extends to the next index item. Scenario: 1: Hover over Element one ELEMENT ONE ELEMENT TWO ELEM ...

Persisting Big IndexedDB in the Browser

As we embark on the development of a Line of Business (LOB) HTML5 web application, our key objective is to ensure that the application has offline capabilities. Our plan involves retrieving a substantial amount of SQL data from the server and storing it in ...

Encountering permission issues while attempting to add `@nuxtjs/sentry` in a Docker container running Node 16.14. Installation

While attempting to add @nuxtjs/sentry to my project by running npm install @nuxtjs/sentry, I encountered some issues. Here is the error message I received: npm ERR! code 1 npm ERR! path /app/node_modules/@sentry/cli npm ERR! command failed npm ERR! comm ...

Connect a series of numerical input fields in Vue.js

One of the tasks I'm working on involves managing a table filled with rows of information. Users can choose multiple rows by checking checkboxes in each row, and I need to keep track of how many rows are selected by updating the totalSelected value. ...

Problem with Labels Overlapping and Blocking Text Fields

My issue is that when I interact with the text field, the label "Type your New Task here" moves up and gets covered by what looks like a white overlay. How can I fix this so that the label stays visible at all times? Prior to engagement: https://i.stack.i ...

Creating evenly spaced PHP-generated divs without utilizing flexbox

My goal is to display images randomly from my image file using PHP, which is working fine. However, I am facing an issue with spacing the images evenly to fill the width of my site. This is how it currently appears: https://i.stack.imgur.com/AzKTK.png I ...