What is the best way to position a <label> to the left of an <input> field?

Examining the HTML and CSS code below:

* {
    box-sizing: border-box;
}

.field {
  overflow: auto;
}
  
.field label {
  float: left;
  width: 80px;
}

.field input {
  display: block;
  width: 100%;
}
<div class="field">
  <label>Test</label>
  <input type="text">
</div>

Regardless of using Firefox or Chromium, the output appears as follows:

However, my intention is for the <input> to be displayed on the same line as the <label>. Essentially, like this:

Why does setting float: left; not position the label to the left of the input?

Answer №1

When you set the width of the text input to 100% and make it a block element, the text box will occupy the entire width of the line, causing it to move to the second line. There are multiple ways to achieve what you are trying to do. One approach is to specify a fixed width for the text input (in pixels or less than 100%) to achieve the desired outcome.

.field label {
  float: left;
  width: 20%;
}
.field input {
  display: block;
  width: 80%;
}

UPDATE

I noticed your response to another answer where you wanted to maintain the 100% width on the input. You can accomplish this without arbitrarily selecting a fixed width by using the table/table-cell CSS classes. The field needs to span the full width and be defined as a table. Remove the float property and set the label and input elements as table cells.

.field {
    display: table;
    width: 100%;
}

.field > label,
.field > input {
    display: table-cell;
}

.field > input {
    width: 100%;
}

Fiddle: http://jsfiddle.net/1rhtgzf0/1/

Answer №2

Inspired by Bootstrap's design principles ():

.field {
    border-collapse: separate;
    display: table;
    position: relative;
    width: 400px;
}

label {
    display: table-cell;
    width: 1%;
}
input {
    display: table-cell;
    float: left;
    position: relative;
    width: 100%;
}
<div class="field">
  <label>Test</label>
  <input type="text">
</div>

Answer №3

It is essential to allocate enough space for your design...

* {
    box-sizing: border-box;
}

.section {
  overflow: hidden;
}

.section label {
  float: right;
  width: 15%;
}

.section input {
  display: inline-block;
  width: 85%;
}

Answer №4

Remember that a label and input are separate elements, so setting the width of the input textbox to 100% will force it beneath the label. To fix this, you can set float: left; on the input element and remove the width from .field input.

Here is an alternative solution:

* {
    box-sizing: border-box;
}

.field {
  overflow: auto;
}

.field label {
  float: left;
  text-align:right;
  width: 80px;
}

.field input{
  float:left;
}

<div class="field">
   <label>Test</label>
   <input type="text">
</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

looking to change the background color of a CSS element

Here is a CSS code snippet that I am struggling with: .select, .input, .schedule, .editableFields { cursor: pointer; background-color: blue; } However, the above code overrides this one: #past .layer.color { background-color: #E5D403 !import ...

Programmatically extracting a list of utilized CSS images from IE WebBrowser (IHTMLDocument2)

Exploring the IHTMLStyleSheetsCollection, IHTMLStyleSheet, IHTMLStyleSheetRulesCollection etc. of IHTMLDocument2 to compile a list of all styles in the current document is quite simple. Does anyone have any suggestions on how to generate a list of only th ...

Can someone please help me figure out why the "setInterval" function in my script isn't functioning as expected?

I've been experimenting with controlling the refresh rate of drawn objects in a canvas using JavaScript. Even after going through the tutorials and examples on w3.school, I'm still unsure why the "setInterval" function is not executing the "gener ...

Tips for extending the space between one element and another when the width decreases:

Currently in the process of building a website using HTML/CSS/JS and have run into an issue. My front page features an image with text overlay, where the image has 100% width and a fixed height of 487px. I positioned the text using position:relative; and t ...

Creating sibling classes with tailwind using the map function

Currently, I am combining tailwind, react, and nextjs to develop a website. My goal is to implement the check radio button technique to dynamically display different content on the website without relying on JavaScript. To streamline data management, I am ...

I'm curious about the specific sequence of CSS properties order recommended by Stylelint. Where can I locate

With Stylelint's CSS order option enabled, I'm having trouble getting Visual Studio Code to display the errors. Where can I locate the declaration/property order being used? Below is a snippet of my .stylelintrc.js configuration file: module.exp ...

Using a single form, the rest of the form fields are restricted when uploading images with Ajax

I am facing a challenge with uploading an image using ajax. I have other fields in the form that require validation before submission is allowed. However, whenever I try to upload an image, all the fields in the form show errors. $('#my_thumbnail&apo ...

What could be causing the tooltip to malfunction in jQuery?

The tooltip is not appearing as expected, only the title is displaying in the browser. What can be done to fix this? Below is the code snippet: HTML Code: <form action="/validate.php" method="post" id="myform"> <table width="100%" border="1"> ...

Enter the ISO code for the country in an HTML form to submit

I'm currently working on a form that requires users to select a country from a list. I came across a website that provides a comprehensive list of countries in HTML format. However, the site displays the full country name but sends the ISO code to the ...

Understanding the Intrinsic Dimensions of Replaced Elements in CSS

I'm currently facing some challenges with the CSS Intrinsic & Extrinsic Sizing Module Level 3, specifically chapter 4.1 on Intrinsic Sizes. This section is proving to be quite difficult for me: When a block-level or inline-level replaced element h ...

Instructions on placing the bottom of an inline-block element flush with the bottom of a block element

In the scenario where I have the following HTML and CSS code: .something{ width: 200px; } .display-block { display: block; } .req-field{ float: right; font-size: 5px; } <div class="something"> <span class="display-block">First name ...

What are the different approaches for creating a website that adapts to different screen sizes and devices

The popularity of responsive web design is growing rapidly. While many recognize Twitter Bootstrap as a top framework, there are other options worth exploring. Several impressive examples of responsive websites, such as Smashing Magazine and CSS Tricks, ...

What is the best way to convert this JavaScript iteration function into jQuery?

I recently encountered an issue with my JavaScript function that returns a list of elements with the class ".youtube", loops through them, and calls another function. The JavaScript logic is flawless, but I wanted to convert it into jQuery for better reada ...

Having trouble with blueprintjs icons not appearing as they should on certain pages

I have recently updated an older React application from blueprintjs version 1 to version 4 by following the documentation. However, I am now facing an issue where the icons are not displaying correctly as shown in the image below. Could someone please poi ...

Jquery deactivates the CSS hover effect

Having a dilemma with linked photos on my website. I have set their opacity to 0.45 by default, but want them to change to full opacity (1) when hovered over or clicked. I've implemented a JQuery function that resets the rest of the photos back to 0.4 ...

I'm experiencing an issue where the links in my dropdown button are not showing when I hover over it. What could

I have been attempting to include a dropdown button that reveals several links when the mouse hovers over it. Despite successfully implementing the CSS for this feature, I am encountering an issue where the links are not displayed beneath the button. Afte ...

Looking to access the layout details of an HTML element similar to what can be done in Firefox's debugger tool?

I am facing a challenge with a aspx.net grid control where I need to extract the first row's information and copy it into another table using JavaScript. To achieve this, I am attempting to calculate the width of each cell in the first row by accessin ...

What are the steps to position this bootstrap drop-down menu above a fixed page header?

Could someone please advise on what changes need to be made in the code snippet below from http://jsfiddle.net/ufkkdbja/? I am looking to ensure that when the hamburger menu is clicked, the entire menu appears without being cut off by the fixed page header ...

Using Data URIs can negatively impact the loading speed of a webpage

I created a custom script to replace all inline images with data URIs in order to minimize http requests and speed up loading times on mobile devices. Surprisingly, I noticed a decrease in loading speed. Could it be because the HTML file was larger (aroun ...

Challenge with modifying CSS variable names in Angular SSR

Recently, I noticed that some of my CSS variable names are being changed to something else on the server-side rendering build, causing them not to work as expected. An example of this is: .color-black-75 { color: var(--black-75-color); } In my styles. ...