Show input fields in a row

In an attempt to align my select form fields on the same line, I have tried adding display:inline; to both the select and label elements in my code. However, it did not produce the desired result.

Here is the HTML code snippet:

<form id ="myform1">
   <label for='orgUnit'>Organization Unit</label>
   <select name="orgUnit" id="orgUnit">
        <option value="Select">Please Select an option</option>
    </select>
    <label for ='org'>Organization</label>
    <select name="org" id="org">
        <option value="Select">Please Select an option</option>
    </select>
    <label for ="bu">Business Unit</label>
    <select name="bu" id="bu">
        <option value="Select">Please Select an option</option>
    </select>
    <label for ="department">Department</label>
    <select name="department" id="department">
        <option value="Select">Please Select an option</option>
    </select>
    <input type="hidden" name="buID" id="buID" >
</form>

The CSS rules applied are as follows:

select {
    width: 175px;
}

label {
    width:130px;
    text-align:right;
    padding-right:10px;
}

After making some adjustments by adding the 'for' attribute and removing the br tags, the layout ended up looking like this:

https://i.sstatic.net/9YCKG.png

Fortunately, upon further investigation, I realized that additional CSS was causing the labels to float and appear as they do in the image above.

Answer №1

Remove the unnecessary <br> tags as they are causing line breaks in your code.

select {
  float:right;
  width: 175px;
}

label {
  float:left;
  width:130px;
  text-align:right;
  padding-right:10px;
}
<form id ="myform">
   <label>Organization Unit</label>
   <select name="orguniy" id="orgUnit">
    <option value="Select">Please Select an option</option>
    </select>
<label>Organization</label>
<select name="org" id="org">
    <option value="Select">Please Select an option</option>
    </select>
<label>Business Unit</label>
<select name="bu" id="bu">
    <option value="Select">Please Select an option</option>
    </select>
<label>Department</label>
<select name="department" id="department">
    <option value="Select">Please Select an option</option>
    </select>
<input type="hidden" name="buID" id="buID" >
</form>

Answer №2

Eliminating the unnecessary <br> tags can greatly improve readability.

 <form id ="myform">
 <label>Department</label>
 <select name="department" id="department">
    <option value="Select">Please choose an option</option>
    </select>
<input type="hidden" name="depID" id="depID" >

Answer №3

Access the JSFiddle here

Here is the HTML code:

<form id="myform">
  <label>Organization Unit
    <select name="orguniy" id="orgUnit">
      <option value="Select">Please Select an option</option>
    </select>
  </label>
  <label>Organization
    <select name="org" id="org">
      <option value="Select">Please Select an option</option>
    </select>
  </label>
  <label>Business Unit
    <select name="bu" id="bu">
      <option value="Select">Please Select an option</option>
    </select>
  </label>
  <label>Department
    <select name="department" id="department">
      <option value="Select">Please Select an option</option>
    </select>
  </label>
  <input type="hidden" name="buID" id="buID">
</form>

The current structure highlights an issue with the <label> tag usage. For a better understanding, check out this Stack Overflow discussion explaining proper label element placement.

In essence, labels should either use a for attribute or wrap around the referenced element (in this case, the <select> tag).

I have eliminated the unnecessary <br/> tags and suggest using CSS for spacing adjustments through properties like margin-top or margin-bottom.

Check out the CSS code below:

label > select {
  width: 175px;
}

label {
  display: block;
}
label:nth-child(1) > select{
  margin-left: 40px;
}
label:nth-child(2) > select{
  margin-left: 72px;
}
label:nth-child(3) > select{
  margin-left: 67px;
}
label:nth-child(4) > select{
  margin-left: 80px;
}

To align the select form fields properly, custom margin-left values were added to each <select> tag in the CSS section.

Why does this solution differ from others? I interpreted your statement about needing the select form fields on the same line as suggesting they be vertically aligned. If you require them horizontally aligned, other answers can accomplish that easily.

Answer №4

Enclose your labels and inputs within a <div> that is styled as an inline-block.

Check out the following for ideas:

Bootstrap inline form

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

Guidelines for determining a screen's location using Javascript

I have integrated a label onto an image and I am trying to figure out how to determine the exact position of each label on the screen. Is there a way to retrieve the screen coordinates for each label? Below is the code snippet that I have uploaded, perha ...

What is the best way to transition an absolute positioned element from right to center?

When hovering over an overlay element, I want the <h3> tag to appear with a transition effect from right to center, similar to the example shown here. Could someone please assist me in achieving this? Thank you in advance. HTML <div class="row m ...

Content within a scrollable div in IE7 only loads once the user starts scrolling

TL;DR: How can I make IE7 load all hidden elements within a scrollable div? I'm creating a collapsible menu for easy navigation through a series of pages. This menu has two states: collapsed and expanded. Think of the menu items as chapters in a b ...

ways to change the font color style of a header element using css

Below is the HTML code that I need to work with: <h5 class="post-title">Welcome To The Site</h5> I am attempting to change the font color to red using this CSS: .post-title{ color:red ! important; } However, my attempt was unsuccessful ...

The issue of elements flickering while being hidden and shown with jQuery has been observed in both Chrome and

Having trouble with a simple animation using jQuery? While it may work smoothly in Firefox, you might be experiencing flickering in Chrome and Edge. Check out this jsfiddle for reference: HTML <div id="boxes-wrapper"> <div class="box"></ ...

Set the div element to be horizontally aligned

Is there a way to make this display horizontally instead of vertically, from left to right rather than top to bottom? I attempted using display:flex but only the extra-text class is affected and not the outer div. https://i.stack.imgur.com/2DNoC.png .m ...

Resizing a webpage to fit within an IFRAME

Having just started learning HTML, CSS, and JavaScript, I am attempting to incorporate a page as a submenu item on my website. Below is the code that I have so far: <!DOCTYPE html> <html> <meta name="viewport" content="width=1024"> ...

Adjusting Bootstrap card content on hover

Currently, I am in the process of developing a website that features a list of products presented in bootstrap cards. I am seeking advice on how to dynamically change the text displayed on these cards when a user hovers over them. Specifically, I want to ...

What is the best way to stop div animations when clicking with jQuery?

Upon loading the page, a div animates automatically. There is also a button present. When the button is clicked, I would like to create a new div and animate it the same way as the first one. However, when this happens, the position of the first div also ...

The functionality of jQuery .Show() may be affected when a negative margin is present in the

Check out this demo on Codepen that showcases the issue: http://codepen.io/theclarkofben/pen/xKhsd The .Show() effect isn't working as expected due to the negative margin applied to the div. Can someone shed light on why this behavior is occurring? ...

I am facing an issue where my Javascript hide and show function is not working properly when clicked. Despite not giving

I am currently working on a Javascript onClick function to toggle the visibility of content in a lengthy table. I initially set part of the table's class to display: "none" and added a button to show the hidden content when clicked. However, nothing i ...

Implementing Google Fonts into Next.js with the combination of Sass, CSS, and Semantic UI React

I have set up my next.config.js file with the following configuration: const withSass = require('@zeit/next-sass'); const withCss = require('@zeit/next-css'); module.exports = withSass(withCss({ webpack (config) { config.module. ...

Is there a way to arrange an HTML list in this specific manner using CSS or JavaScript?

I need to arrange a list of items in columns with 5 rows each, as shown in the attached image. This list is generated dynamically using an SQL query with a loop on the li tag. I am looking for a solution to order the list in this way using javascript or ...

Struggling with the adaptability of my website's footer section

I'm facing a dilemma... No matter what I do, I can't seem to position my contact form over my Google iframe perfectly. It works fine if I don't mind sacrificing responsiveness, but as soon as I try to make it responsive, the absolute assign ...

Stop the page from scrolling when scrolling within a specific DIV to address the [Violation] warning appearing in the console

Initially, it may seem like a duplicate question that has been answered here, but there are additional aspects that need to be addressed. How do I resolve the following [Violation] warning in the Google Chrome console? [Violation] Added non-passive eve ...

Maximizing Content Width in FullCalendar v5 to Ensure Screen Compatibility

As I develop a calendar and timeline view using fullcalendar v5, my clients are requesting a month view that displays the entire month without any scroll bars. While I am aware of setting the contentHeight to auto, there seems to be no option for adjusting ...

Improving the method for adding an element to an HTML document with jQuery

What is the best way to add this element to a specific DIV Class using JQUERY in an optimized manner? The elements are generated dynamically and I want to use .AppendTo to display them inside <div class='parent-list-workorder'>. This is wh ...

Every time I hover, my jQuery code keeps repeating the hover effect

I am currently facing an issue that has me stumped on finding a solution. The problem arises when I hover over the .div multiple times, the animation just doesn't stop and keeps running continuously. What I aim for is to have the .hidden element fad ...

Vibrant diversity in a solitary symbol

I have customized styles to incorporate Fontawesome icons with their brand colors - for instance, the Opera icon appears in red, IE in blue, and Firefox in orange. Chrome, however, presents a unique challenge with its four distinctive colors. I am curious ...

Issue with exchanging columns in Bootstrap version 5.0

Seeking assistance! I am a beginner with bootstrap and struggling to figure out an issue I'm experiencing. In the attached image(s), my goal is to have the coin image positioned above its corresponding text on the left side when the screen size is red ...