Alignment of Text Field and Label Positioning

How can I ensure my labels are aligned with the corresponding textfields? The snippet of code that I've posted is resulting in a UI layout that I'm not satisfied with. The formatting of the first row (20 --> First) is actually what I want to achieve.

I have made updates based on Rafael's answer. Due to limited space, it seems that text longer than 6 characters does not fit correctly within the allocated area. I'm unsure how to resolve this issue.

 input[type="text"]{
        display:inline;
        margin-bottom:10px;
        margin-right:3px;
        width: 50px;
        text-align:center;
        clear: left;
}    


label {
       color: white;
       display: inline-block;
}

ul {
padding: 0;
list-style-type: none;
} 


<ul>
<li><label for="first">First</label><input type="text" value="Loading..." id="firstTF"/></li>
<li><label for="second">Second</label><input type="text" value="Loading..." id="secondTF"/></li>
<li><label for="third">Third</label><input type="text" value="Loading..." id="thirdTF"/></li>
<li><label for="fourth">Fourth</label><input type="text" value="Loading..." id="fourthTF"/></li>
<li><label for="fifth">Fifth</label><input type="text" value="Loading..." id="fifthTF"/></li>
<li><label for="sixth">Sixth</label><input type="text" value="Loading..." id="sixthTF"/></li>        <li><label for="seventh">Seventh</label><input type="text" value="Loading..." id="seventhTF"/>  </li>
  </ul>

Answer №1

Have you considered placing your label next to the input field instead? This way, you can remove the float: left from the inputs.

Check out this example on JSFiddle

If you prefer not to change the order of the elements, you could add an overflow: hidden to the li element, like in this JSFiddle

Edit

In response to your edit: It really depends on how you want to handle it. What should happen when the text exceeds the available space for display?

One option is to set a max-width and hide any overflowing text, as shown in this JSFiddle

Answer №2

Get ready for the anticipated outcomes :

<style type="text/css">

input[type="text"]{
        display:inline-block;
        margin-bottom:15px;
        margin-right:5px;
        width: 60px;
        text-align:center;
        clear:both;
}    

label {
       color: blue;
       display: inline;
       text-align:left;
       float:right;
}

ul {
  padding-left:10px;
  list-style-type: circle;
  float:right;
} 
</style>    

<ul>
<li><div id="line"><label for="first">First Name</label><input type="text" value="Loading..." id="firstField"/></div></li>
<li><label for="second">Second Name</label><input type="text" value="Loading..." id="secondField"/></li>
<li><label for="third">Third Name</label><input type="text" value="Loading..." id="thirdField"/></li>
<li><label for="fourth">Fourth Name</label><input type="text" value="Loading..." id="fourthField"/></li>
<li><label for="fifth">Fifth Name</label><input type="text" value="Loading..." id="fifthField"/></li>
<li><label for="sixth">Sixth Name</label><input type="text" value="Loading..." id="sixthField"/></li>        
<li><label for="seventh">Seventh Name</label><input type="text" value="Loading..." id="seventhField"/>  </li>
  </ul>

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

Guide on organizing items into rows with 3 columns through iteration

Click on the provided JSFiddle link to view a dropdown menu that filters items into categories. Each item is stored as an object in an array for its respective category. Currently, all items are displayed in one column and I want to divide them into three ...

Issues with implementing @font-face on a singular font

I've encountered an issue while setting up a website for a client concerning the fonts. Most fonts are displaying correctly, but there is one font, "chunkfive", that is not working as expected. The problem becomes more severe in Internet Explorer wher ...

Attempting to prevent the use of the <p> tag solely before the text

My toggle function is not working correctly in my FaQ section. When a user clicks on a topic, the bottom section should appear, but the <p> tag is being displayed across the entire line instead of just one word.. Here is an image to help illustrate ...

Is it possible to craft poetic lines with indents in HTML using blockquotes, pre, or dd tags?

I am struggling to format a few lines of text with indentations using HTML. Here is an example I found: HERE Below is my attempt: <p><code>"Knowing is not enough. We</code></p> <p><blockquote><code>must apply. Wi ...

Is there a way to dynamically convert a css rule like "select::-ms-expand" to JavaScript?

Looking for assistance with implementing the following code snippet in JavaScript: "$('select').css('-moz-appearance','none');". I've tried searching on Google for solutions, but haven't been successful. Any tips or ...

Showing the current HTML page using JavaScript when clicking?

Currently, I am working on changing the page layout using JavaScript when a function is triggered by clicking. My question is, how can I make the JavaScript display the current HTML body? The process is as follows: First, there is the page's HTML an ...

Creating a Horizontal Scrollbar in HTML

Recently, I came across a family treeview design like the one showcased here. However, the layout seems to fall apart when there are numerous children and doesn't fit on the screen properly. Is there a way to implement a horizontal scrollbar to addres ...

Ensuring proper alignment between labels and input

How can I align a label and a range input field on the same line? I have tried using display: inline-block, but it does not seem to work. There doesn't appear to be any conflicting styles, and all sources indicate that this approach should be effect ...

Ways to retrieve JSON data through multiple levels of nested if statements

Currently, I am using CodeIgniter to work on a small project involving creating batch lists. When an admin attempts to create a new batch list, they must input the start date, end date, start time, and end time. The system will then check the database to s ...

I am having trouble getting my CSS styles to apply to nested components within Bootstrap React components

Currently I am learning react and in the process of creating a website using react-bootstrap. While working on my navbar, I encountered an issue where I was unable to change the font and font-color of the navbar items. The CSS that I applied only affected ...

What is the best method for extracting or filtering data from HTML using PHP?

Every day, I receive an email containing an HTML document with statistics from the previous day. The HTML format is difficult for all users to read, so I'm looking to automatically extract information from this table using a PHP script. Below, you&apo ...

What are the steps to prevent a redirect?

I have two forms with different functionalities - one for submitting/adding and the other for selecting/redirecting. Currently, both forms are redirecting. How can I prevent the top form from redirecting after submission? <?php if($_POST){ / ...

Is the button with glyphicon malfunctioning on Mozilla Firefox?

Everything seems to be running smoothly on various browsers, except for Mozilla Firefox. <button type="button"> <a href="edit_teacher.php?edit=<?php echo $row['teach_id']; ?>"> <span class="glyphicon glyphicon-edi ...

Storing data in the browser's local storage using jQuery without requiring any CSS

I have a form that receives data and I am trying to save the first two pages of received data into localStorage: <form id="myForm" method="post"> Font Size (px): <input id="fontsize" type="text" name="fontsize" /> </form> <s ...

What is the best way to position a <div> in the center of a container?

I am currently designing a cart page for an online store, and I have decided to showcase all the items in the cart using a table format. Here is my plan: https://i.sstatic.net/PJBpo.jpg My goal is to have the name, quantity, and price of each product dis ...

The data variable in Vue.js does not show up in the view even when it is populated through an AJAX request

I am facing an issue with my code, where the 'tarefas' variable is not appearing in my v-for loop. I have verified that the response from the server is correct and the data is being received. Interestingly, when I add new data in the input field, ...

Explaining the precise measurements of font size in CSS

I recently started diving into the world of CSS and picked up a copy of Eric Meyer's 3rd edition of CSS: The Definitive Guide. While reading through his section on font sizes (page 107), I came across an interesting concept - that the font size deter ...

On a desktop screen, there is a single tall div positioned next to two shorter divs. On mobile devices, the divs

Similar to the challenge posed in this thread, my query is slightly different. I am looking to rearrange three divs on mobile devices while having them adjacent on desktop screens. Please refer to the linked images for a visual representation of the desir ...

What is the best way to ensure all table rows have consistent heights that are not affected by image sizes?

In the provided code snippet, you will notice that the height of the table rows varies slightly based on the height of the images. Currently, the image tags are set to height: auto;, and changing it to 300px would make all images the same size, however, m ...

Tips for making a bookmark for your iframe content

Within this HTML code, there is a list element with the ID "about_ma" and an iframe named "page" that is initially hidden. <div id="navigation"> <ul id="nav"> <li id="about_ma"><a href="index1.php?name=about.php">Ab ...