The alignment of labels and text inputs using CSS flexbox is not correct

I am attempting to create a responsive layout for the labels and text inputs in my HTML code. I want them to align in a single row, with one label and one text input per row when the screen width is below a certain threshold. Once the screen width exceeds this limit, I would like the elements to line up in rows of three. Currently, the elements are displaying in rows of three; however, when the screen reaches the maximum width, the labels and inputs appear misaligned on separate rows. It seems as though they are behaving as if the display property was set to block for the labels. Below is a snippet of my HTML code where I am trying to implement flexbox:

@media only screen and (max-width: 696px) {
#boxes {
display: flex;
flex-direction: row;
}
}
<div id="boxes">
<h3 id="question"></h3>
<label id="S" for = "Sn"></label>
<input type = "text" id = "Sn" class='input' autocomplete="off" placeholder="Answer">
<label id="at" for = "atomic"></label>
<input type = "text" id = "atomic" class='input' autocomplete="off" placeholder="Answer">
<label id="elec" for = "electrons"></label>
<input type = "text" id = "electrons" class='input' autocomplete="off" placeholder="Answer">
<h3 id="question2"></h3>
<label id="pro" for = "protons"></label>
<input type = "text" id = "protons" class='input' autocomplete="off" placeholder="Answer">
<label id="ma" for = "mass"></label>
<input type = "text" id = "mass" class='input' autocomplete="off" placeholder="Answer">
<label id="ele" for = "element"></label>
<input type = "text" id = "element" class='input' autocomplete="off" placeholder="Answer">
<h3 id="question3"></h3>
<label id="pro2" for = "protons2"></label>
<input type = "text" id = "protons2" class='input' autocomplete="off" placeholder="Answer">
<label id="at2" for = "atomic2"></label>
<input type = "text" id = "atomic2" class='input' autocomplete="off" placeholder="Answer">
<label id="new" for = "neutrons"></label>
<input type = "text" id = "neutrons" class='input' autocomplete="off" placeholder="Answer">
</div>

As illustrated in this screenshot https://i.sstatic.net/UlIHc.png, the labels are currently above the text inputs. My goal is to have the labels aligned with their corresponding text inputs.

Answer №1

Consider using flex-direction: column; in place of row for your media query.

@media only screen and (max-width: 696px) {
#boxes {
  display: flex;
  flex-direction: column;
  align-items: center;
 }
 #boxes > div {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
 }
}

label {
  text-align: left;
}

label#S {
  margin-right: .65rem;
}

label#elec {
  margin-left: -1px;
}
<div id="boxes">
   <h3 id="question"></h3>
   <div>
     <label id="S" for = "Sn">Proton</label>
     <input type = "text" id = "Sn" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <div>
     <label id="at" for = "atomic">Neutron</label>
     <input type = "text" id = "atomic" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <div>
     <label id="elec" for = "electrons">Electron</label>
     <input type = "text" id = "electrons" class='input' autocomplete="off" placeholder="Answer">
   </div>
   
   <h3 id="question2"></h3>
   <div>
     <label id="S" for = "Sn">Proton</label>
     <input type = "text" id = "Sn" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <div>
     <label id="at" for = "atomic">Neutron</label>
     <input type = "text" id = "atomic" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <div>
     <label id="elec" for = "electrons">Electron</label>
     <input type = "text" id = "electrons" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <h3 id="question3"></h3>
   <div>
     <label id="S" for = "Sn">Proton</label>
     <input type = "text" id = "Sn" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <div>
     <label id="at" for = "atomic">Neutron</label>
     <input type = "text" id = "atomic" class='input' autocomplete="off" placeholder="Answer">
   </div>
   <div>
     <label id="elec" for = "electrons">Electron</label>
     <input type = "text" id = "electrons" class='input' autocomplete="off" placeholder="Answer">
   </div>
</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

continuously refresh choices available for selection

I am looking for a way to dynamically populate the second select option based on the selection made in the first select option. While I know how to achieve this using traditional jQuery and HTML, I was wondering if there is a way to update options_for_sele ...

Checking for correct HTML tags using JavaScript

Looking to validate some input in javascript and confirm if it is a valid HTML tag. Any straightforward methods with JQuery for this task? If not, any suggestions on how to achieve this using Regex? Thank you! ...

Stopping a div from causing the parent table's width to increase

I've encountered an interesting challenge with a project I'm working on. I need to inject markup into pages that I don't have control over, specifically within tables. The issue arises when my injected div causes the parent table to expand u ...

ng-required is ineffective when used with number inputs that have a minimum value requirement

In my form, I have implemented a checkbox that, when checked, toggles the visibility of a div using AngularJS's ng-show. Within this div, there is an input field of type "number" with a validation setting of min="10000". I am trying to prevent the f ...

What could be causing my video not to display a thumbnail on mobile devices until it is played?

When I view my website on mobile devices, there is no background image displayed before playing a video. Below is the HTML code I am using: <div class="row " > <div class="col-12 text-center"> <video control ...

Troubleshooting overflow problems when centering a UL menu with an unspecified width

Trying to create a demo where the scrollbars are removed when resizing smaller, but adding overflow: hidden causes issues with sub-menus. Demo Link HTML <nav> <ul> <li><a href="#">Menu Item</a></li> ...

To reveal the secret map location, just tap on the button - but remember, this only applies to

I am encountering an issue with duplicating a card and toggling the hidden location map. Currently, the location button only works for the first card and not for the rest. I need each card to independently open and hide the location map when clicked on the ...

The overflow hidden property in Tailwind CSS is not functioning properly during animations

I'm attempting to animate an image by moving it to different positions, but when it goes off the screen, the scrollbar appears. I tried adding overflow hidden to the parent element, but it didn't work as expected. Here is the relevant code snippe ...

Hiding the overflow conceals the entire image in one direction, while leaving the other exposed

Here is the code I have written for my project (view fiddle here): img { width: 200px; height: 200px; } .image-container { width: 600px; height: 200px; overflow: hidden; } I am working with multiple images, all sized at 200px by 200p ...

What is the process for incorporating themes into CSS?

At the moment, I have set up variables to manage colors for both light and dark themes (such as --light-background-color, --dark-background-color). Handling two themes is manageable with this approach, but it feels a bit labor-intensive. If more themes wer ...

Is there a way to change the fill color of an SVG circle using Thymeleaf?

I am struggling to change the background color of a circle in an SVG image on my HTML page using Thymeleaf. I attempted to use inline style tags, but it resulted in the circle's background color turning black. <svg id="svg" height="50" width=" ...

Develop a DIV element that remains constant throughout different web pages without having to reload

How can I create a non-reloading div at the top of my website that contains an MP3 player which plays continuously while users browse through the site? I am working with Joomla and would like to avoid using Iframes for search engine optimization purposes ...

Form in HTML - Transmit rows consecutively as an array

Hey there, I have a bit of a perplexing issue that I'm hoping to get some help with. I have a form that pulls data from my database. It displays names in one column and two columns of checkboxes. My goal is to update the database based on the checkb ...

Minimize the gap between col-lg elements in responsive mode within Bootstrap

My website is built using Bootstrap 4 and the following is the HTML code: <div class="container"> <div class="row text-center "> <div class="col-lg-6">A</div> <div class="col-lg-6">B</div> </div> < ...

Modify the background color of the disabled checkbox in Vuetify

Check out this example where I found a disabled checkbox. Is there a way to set a custom background color for the "on" and "off" states of a disabled checkbox? ...

Navigate through AJAX Live search using arrow keys and mouse controls

I have encountered an issue with the autocomplete feature on my webpage. Although I am able to input suggestions, I am unable to navigate through them using the mouse pointer initially. Oddly enough, if I press the up/down keys once after the list items ar ...

"Can you provide instructions on placing a span within an image

I am trying to figure out how to insert a <span> into an <image>, similar to the effect on the page . However, I do not want this effect to occur on hover(), but rather on click(). My goal is to insert "data-title" into the <span> and hav ...

Tips for returning an element to its starting position following animation

Hey there, I’m fairly new to the world of HTML and CSS. Recently, I was working on creating a YouTube clone website and I’ve run into an issue with the navigation. On the official YouTube website, when you click on the hamburger menu icon, the naviga ...

Tips for adjusting the time format within Ionic 3 using TypeScript

I currently have a time displayed as 15:12:00 (HH:MM:SS) format. I am looking to convert this into the (3.12 PM) format. <p class="headings" display-format="HH:mm" > <b>Time :</b> {{this.starttime}} </p> In my TypeScript code t ...

How to override the styling of a parent element in CSS

I'm encountering an issue with my website's sidebar. I've set the background color to yellow for elements with the currentPage class. This works fine for the 'Home' tab, but when I try to do the same for a nested tab like 'tag ...