Alignment of labels and input fields in a vertical manner

Trying to align the label vertically with the input on the right side. After searching for solutions, I couldn't find anything that works perfectly for my scenario. Here is what I have tried so far, but the label text remains aligned to the top.

CSS:

label{
    float:left;
    width:50%;
    min-height: 20px;
    vertical-align: middle;
    clear: both;
    text-align:right;
    display: table-cell;
    -webkit-box-sizing: border-box; 
    -moz-box-sizing: border-box;    
     box-sizing: border-box;    
     padding-right: 10px;
}

input[type="text"]{
    width:50%;
}

.pers_form{
    width:100%;
    overflow:hidden;
    margin-bottom:5px;
    display: table;
}

HTML:

<div class="pers_form">
    <label for "example">Example</label>
    <input type="text" id="example" name="example" />
</div>

Answer №1

Your <input> element is missing the display: table; property, and your <label> has an unnecessary float: left;. Here is the updated code with multiple ways to achieve vertical alignment:

label {
  width:50%;
  min-height: 20px;
  vertical-align: middle;
  clear: both;
  text-align:right;
  display: table-cell;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding-right: 10px;
}
input[type="text"] {
  width:50%;
  display: table-cell;
} 
.pers_form {
  width:100%;
  overflow:hidden;
  margin-bottom:5px;
  display: table;
}  

http://jsfiddle.net/qop7d4av/

Answer №2

What do you think of this styling?

.personal_info{
    width:500px;
}

name{
    width:250px;
}

data{
    width:250px;
    display:inline-block;
}

This code will align the name on the left and the data on the right side inline

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

Spinning divs using JavaScript when the mouse hovers over them, and then returning them to their original position when the mouse moves

I am looking to create a functionality where a div rotates when the mouse enters it and returns to its original position when the mouse leaves. Everything works fine when interacting with one div, but as soon as I try hovering over other divs, it starts gl ...

How can I toggle the radio button based on the dropdown selection?

I'm attempting to enable or disable a radio button based on the selection from a dropdown menu. View my jsfiddle example here For example, if the user selects "persons" from the dropdown, only the options for Anthony and Paul should be available to ...

What exactly does the "data-effect" attribute refer to?

After downloading a code, I came across the following line: <button data-effect="st-effect-4">Slide along</button> I noticed that "st-effect-4" is a class name in the code, but I am curious to know what exactly this data-effect attribute is u ...

Achieving full opacity text within a semi-transparent div: a simple guide

Within my div, I have an a tag. I applied opacity:0.5 to the div, causing the text inside to also appear at 0.5 opacity. I am looking for a way to have the text display with opacity:1 inside my div that has an overall opacity of 0.5, without resorting to ...

discovering the category for the .click function

After retrieving a list of book objects from a JSON object, I am in the process of displaying them on an HTML page by creating buttons with titles. The goal is to alert the URL of the book when a button is clicked. However, I am facing an issue with access ...

Shifting between classes in jQuery only takes effect after scrolling occurs

There seems to be an issue with the transition effect in my jQuery code that changes the background color of the .navbar upon scrolling. Even though it is supposed to start with a transparent background, it always begins with a yellow background. I have fo ...

Centering text on input placeholder

What is the best way to center a placeholder vertically in an input field? input[type='text']{ background-color: rgba(255,255,255,.4); border:3px solid rgba(0,0,0,.5); min-height: 45px; border-radius: 6px; color:#fff; } input[type=&apo ...

Troubleshooting issue markers for q-field component paired with q-input in Quasar Framework

I have a couple of inquiries regarding the error-labels of q-field (in conjunction with q-input) while utilizing the Quasar Framework: How can I prevent the button below from moving further down when the error-label is displayed? When using vuelidate f ...

Activate hover event on UI-Grid for interactive display

I often utilize in the following manner: <div ncy-breadcrumb class="title-shadow"></div> {{ getDeviceTree }} <div> <div ui-grid="gridOptions" ui-grid-resize-columns ui-grid-selection></div> </div> My object ...

How does GitHub create their unique "character portraits" for optimized web performance?

Have you ever noticed that the small images on Github are actually characters instead of traditional image files? Let's take the small person image that is used when assigning someone to an issue as an example. When you hover over the small person ima ...

Tips for adjusting large resolution images to fit all screen sizes without exceeding the boundaries of the screen

I am in the process of creating a set of HTML pages specifically tailored for iPad Air and iPad Mini. These pages will feature several large images, with dimensions such as 1600x300. However, upon testing in Windows browsers, I encountered an issue where t ...

Unable to implement the `omega/theme.css` file within the angular.json configuration

"styles": [ "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.css", "node_modules/primeicons/primeicons.css", ...

Setting the maximum width for images across various Bootstrap responsive sizes: a step-by-step guide

I'm struggling with optimizing images of varying sizes in bootstrap to ensure they are responsive without losing quality. Current Image: https://i.sstatic.net/akoNy.jpg Desired Image: https://i.sstatic.net/37VFX.jpg HTML: <div class="row"> ...

Is it possible to create a webpage where the header and footer remain static, while the content in the body can be dynamically changed

I am in the process of creating a webpage where main.html will load a page with a static header and footer that remains unchanged when navigation links are clicked. I'd like to achieve this using Javascript, with the body content being sourced from an ...

Automated clicking on a child node element in JavaScript

I have been on a quest to find a reliable method to automate a mouse click on a specific element using JavaScript for a user-script. The element structure is as follows: <div id="elementContainer"> <div class="item1" style="width: 50px; height: ...

Changing the width of the file input using css

Clicking just below the word demonstration also triggers a click on the input type file. How can this be avoided so that the click event only fires in the intended area regardless of size? <!DOCTYPE html> <html> <body> <style> in ...

Download and print the embedded PDF file

I am having trouble figuring out how to add 2 external buttons to print and save an embedded pdf on my webpage. Despite searching Google for hours, I have not been able to find a solution that works. The embedded object already has buttons for printing and ...

What is the best way to utilize Content-Disposition: attachment?

I am new to creating websites and I am trying to make it easier for users to download mp3's from my site by allowing them to left click instead of the traditional right click and save as method. In order to achieve this, I need to set the Content-Disp ...

What is the best way to enlarge an element when scrolling downwards within the element?

I am looking to create a div that dynamically adjusts its height based on the user's scrolling behavior. The goal is for the div to expand to the very top as the user scrolls downward, and stop when it reaches 70% of the container/parent element. Is t ...

Utilizing pixel values for Material-UI breakpoints rather than using the default sm, md, lg, xl options

Below is the code snippet that I am using: [theme.breakpoints.only('lg')]: {} The above code works perfectly and shifts at the desired breakpoint. However, when I implement the following: [theme.breakpoints.between('1200', '1021&a ...