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

Troubleshooting the onExited callback issue with Popover component in Material UI

<Popover key={element.name} classes={{ paper: classes.paper }} open={open} anchorEl={this.myRef.current} anchorOrigin={{ vertical: ' ...

Selenium webdriver unable to locate an empty element

I'm currently trying to find a specific element using selenium webdriver: <div class="lv-product__details"><div class="lv-product__details-head"><span class="lv-product__details-sku"> M40712 ...

Peeling back the layers of a particular element

This is my code snippet: <pre id='code'> <ol> <li class='L1'><span>hello</span></li> <li class='L2'><span>Hi</span></li> <li class='L3&apos ...

Safely render user-generated HTML content within a React application to prevent cross-site scripting vulnerabilities

A unique situation has arisen in our React app where a user inputs a string that is displayed to other users. The challenge lies in searching for specific characters within the string and replacing them with HTML elements. For example, transforming the wor ...

Achieving nested classes in CSS: a comprehensive guide

I am encountering an issue with my CSS while trying to create a dropdown on hover. The problem seems to be related to nested classes inside a div structure. It appears that the dropdown list is not displaying properly, leading me to believe there might be ...

Why is PHP unable to identify the custom-designed CSS button upon submission?

I'm having a design issue where the button on my form is not being detected. I've tried various methods like changing the class name, assigning it a name via HTML, but nothing seems to work. Here is my button: .blue_button { background: #005b66; ...

What is the best way to conceal a div when there are no visible children using AngularJS?

I need help with hiding a parent div only if all the child items are invisible. I have successfully hidden the parent when there are no children, but I am struggling to figure out how to hide it based on visibility. <div ng-repeat="group in section.gro ...

Issue with VueJS where the datalist input does not reset the value

I am currently working on a Vue component that scans QR codes and adds information to a database upon successful scanning. The scanning process works perfectly fine. However, after successfully sending the data, I need to clear the input field in my datali ...

Leveraging class names to dynamically apply CSS styles to components based on their props value

In the process of developing a collection of reusable components, I have utilized material-ui and styled them with CSS. One specific requirement is to dynamically set the width of a component through a prop passed into a custom button. My goal is to lever ...

Attempting to implement an EventListener to alter the navbar upon scrolling, unsuccessful at the moment

Exploring ways to update the navigation bar upon scrolling to shrink its size and modify the color scheme (specifically, transitioning from a transparent background to white and altering font colors). Below is the HTML snippet: /* Defining the overa ...

Safari is not functioning properly with the css display:none property

My HTML code looks like this: <div id="loadinganimationsearch"> <div style="color: #28ABE3;font-size: 14px;float: left;">Fetching Textbooks</div> <div id="block_1" class="barlittle"></div> <div id="block_2" ...

The issue with res.sendFile is that it is failing to display

I have encountered an issue while attempting to render HTML with res.sendFile using an absolute path. The encoded HTML is being displayed unrendered within a pre tag in the response. Below is the express code I am currently using: app.get('/', ( ...

Stylishly Select with Bootstrap 4

Currently, I am utilizing Angular 2 with bootstrap 4 and have implemented the following select element: <div class="form-group"> <label class="col-md-4 control-label" for="OptionExample">Choose an option:</label> <div class="c ...

What measures can be taken to safeguard this hyperlink?

Is there a way to conceal HTML code from the source code? For instance: jwplayer("mediaplayer").setup({ file: "http://example.com/Media.m3u8", autostart: 'true', controlbar: 'bottom', file: "http://exa ...

Is there a way to access the rear camera on a mobile device using webcam.js?

Currently, I am utilizing webcam.js from the following link: https://github.com/jhuckaby/webcamjs. When accessing the website on mobile devices, the front camera tends to open by default. My objective is to switch this default setting to access the rear ...

An easy way to enable mobility for BootstrapDialog on mobile devices

Currently, I am utilizing the library available at https://github.com/nakupanda/bootstrap3-dialog in order to create a dialog box. However, my issue arises when viewing the dialog on mobile devices where it exceeds the screen size. On desktops, adjusting t ...

Enter a keyword in the search bar to find what you're looking

I am working on a form where users can select their occupation from a list that is stored in a separate .js file. The list includes various occupations like 'AA Patrolman' and 'Abattoir Inspector'. var occupationSelect = "<select id ...

The image showcases interactive hover effects that resemble a button popping up

Welcome to my HTML code showcase: <div class="container"> <a target="_self" href="flower_gallery.htm"> <img data-src="holder.js/180x180" style="height:20%;width:100%;float:left;" ...

What is the best way to create a toggle effect for a <nav> bar that appears from beneath a div?

I need assistance with a navigation setup where the nav (located inside the header) needs to be connected to the bottom of a div named .menu_bar. The desired behavior is for the nav to slide down from directly underneath the .menu_bar when toggled, but cur ...

mandating the selection of checkboxes

Currently, I am exploring the possibility of automatically selecting a checkbox when an option is chosen from a dropdown menu. Below is a code snippet that demonstrates what I am aiming to tweak: $('.stackoverflow').on('change', func ...