HTML/CSS: Customize form labels to appear alongside form fields in a single line

I've searched extensively, but none of the solutions provided have been effective for me.

My issue involves trying to align two form fields and their labels on a single line, ensuring that both elements are in proper alignment.

Despite my attempts using two different methods, I encountered persistent issues. With Method 1, one label consistently failed to align properly even with adjustments to margins or padding. Method 2 also presented challenges as it caused misalignment due to limitations on label positioning.

Solution Attempt - Method 1

label{
  text-transform: uppercase;
  font-size: 0.8em;
  font-weight: bold;
  margin-left: 11px
}

label.right_lab{
  display:inline-block;
}

label#lname{
  float: right;    
  margin-left:  60% !important;
}
<label for="fname" class="right_lab">First name</label>
<label for="lname" class="right_lab">Last name</label><br>
<input type="text" id='fname' name="fname" value="" placeholder="e.g Joe">
<input type="text" id='lname' name="lname" value="" placeholder="e.g.Bloggs"><br>

Solution Attempt - Method 2

label{
  text-transform: uppercase;
  font-size: 0.8em;
  font-weight: bold;
  margin-left: 11px
}

label.right_lab{
  display:inline-block;
}

label#lname{
  float: right;    
  margin-left:  60% !important;
}
<label for="fname" class="right_lab">First name</label>  
<input type="text" id='fname' name="fname" value="" placeholder="e.g Joe">
<label for="lname" class="right_lab">Last name</label><br>
<input type="text" id='lname' name="lname" value="" placeholder="e.g.Bloggs"><br>

Answer №1

Here is a third method to achieve the desired result:

label {
  display: inline-block;
}
label>input {
  display: block;
}
<label>
  First name
  <input type="text" />
</label>
<label>
  Last name
  <input type="text" />
</label>

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

Can a sophisticated text editor be utilized without a content management system?

Many website builders utilize rich text editors as plugins to enhance content creation, such as in CMS platforms like Joomla and WordPress. However, can these same editors be easily integrated into a custom website built from scratch using just HTML, PHP ...

Show the content of a div inside a tooltip message box in an MVC application

I have a MVC application where I need to show tooltip messages when hovering over my HTML div. HTML: <li class="Limenu" data-placement="right"> <span class="LessMenuspan btn-primary" pid="@i.ConsumerNo_" onmouseover="FacebookprofileTip(1, 0,thi ...

Rendering a prototype and setting its display to none

I have created a slideshow with the following code: <div class="row-3"> <div class="slider-wrapper"> <div class="slider"> <ul class="items"> ...

What is the best way to align the inner content to the left and right within a border-radius shape?

.intro h1{ font-family: 'Cambria'; font-size: 16pt; font: bold; text-align: left; } .intro p{ font-family: 'Calibri'; font:italic; font-size: 12pt; padding: 0px 690px 0px 20px; text-align: left; ...

"Designing with Vue.js for a seamless and adaptive user experience

I'm looking to customize the display of my data in Vuejs by arranging each property vertically. Instead of appearing on the same line, I want every item in conversationHistory to be displayed vertically. How can I achieve this in Vuejs? Can anyone off ...

In Chrome, there is a conflict between the screen width when using `position: absolute` and `position: fixed` if there is vertical

I'm in the process of developing a website with a video banner set to fixed positioning in the background, and a div that has absolute positioning covering part of the video on the bottom half. However, I've encountered an issue - when I add this ...

Get rid of the rollover effect on the <a> tag when hovering over an image

I have a snippet of code that is fully functional. <div class="user-pic round-pic"> <a href="<?php echo $userLoggedIn; ?>"><img src="<?php echo $user['profile_pic']; ?>"></a> </div> The issue lies in i ...

CSS Image Placement

If I have an image with a width of 2000px, where the snazzy design aspect is located about 600px in and spans about 900px. My goal is to use this image as the background for a website, ensuring that the snazzy design remains centered even when stretching t ...

How can you fix a navbar that won't stay in place?

I'm facing an issue with this example: When I scroll, the navbar moves along due to the fixed property, which affects the overall look. How can I make it disappear when scrolling, while keeping the logo intact? Can someone suggest if this layout is ...

The form action now triggers the download of the PHP file instead of simply loading

I'm encountering an issue with the form on my website: <div class="searchBar"> <form action="searchDB.php" method="get"> <input type="text" class="searchBarInput" bdo dir="rtl" name="k"> <input type="image" cl ...

Is your WordPress one-page scroll JQuery script failing to function properly?

Currently attempting to develop a single page scroll feature. Within WordPress, my navigation contains anchor tags structured as follows: <a href="#contact">Contact</a> <a href="#about">About</a> The corresponding divs for the li ...

I aim to place my :after content in mobile view or lower resolutions

I have a testimonial section built with HTML and CSS. You can view the demo on JSFIDDLE HERE. In the mobile view, I am facing an issue where the ":after" content is not aligning properly or appears misplaced. I'm looking for ideas to ensure that it re ...

What steps are needed to adjust a modal window so that it perfectly fits the size of the image it contains?

I am currently utilizing Bootstrap 4.2 to create a popup window (modal) featuring an image displayed at its real size (100%) in both width and height, provided that the browser window size permits. If the image surpasses the window dimensions, it should au ...

A PHP drop-down menu maintains a fixed position

I have set up a database called pogoda with a city table containing columns for city and cityid. Using PHP, I am creating a dynamic list and submitting the chosen value into subsequent code. To ensure that the drop-down list has a default starting positio ...

Creating a dynamic navbar with Python (Django), HTML, CSS, and Javascript for a fully responsive

I am currently utilizing a guideline to develop a responsive navbar that adapts its layout based on the screen size. It will display elements in a dropdown list for smaller screens, as opposed to an inline layout used for larger screens. Here is a snippet ...

IE7 causing mispositioning of `<ul>` in jQueryUI Autocomplete results

I have successfully implemented the jQueryUI Autocomplete function and here is how I have customized it: appendTo: "#results", open: function(){ var position = $("#results").position(), left = position.left, top = position.top ...

Template for PHP website including code

I am working on integrating this code into my index.php page in order to streamline the process of creating separate pages for my website using only simple HTML files. For instance: Instead of repeating the same template code on multiple pages like index, ...

There seems to be an issue with Bootstrap: the property this._config is

Here is the button I have: <button class="kv_styleclass_0 btn btn-outline-success btn-lg" data-bs-toggle="modal" data-bs-target="formModal" type="button"> Become a participant </button ...

Tips for creating a consistently positioned div element, regardless of the screen size

Having trouble positioning two bars with green or blue filling in the bottom right corner? They keep moving around and not staying in place regardless of screen size. Check out how they are off-screen here Below is the CSS-Styling + HTML code: .eleme ...

What is the best way to ensure that a specified number of list items (li) will align properly within the width of an unordered list (ul

I'm attempting to make all the li elements' width match that of my ul element. Here is the code I am using: http://jsfiddle.net/4XR5V/2/ I have looked at some similar questions on the website and tried adding: ul { width: 100%; display: tab ...