Align the input labels to the right

In the demonstration below, an example is provided for aligning labels to the right of an input field. Is there a way to ensure that the input field takes up 100% of the width? Additionally, can an input be replaced with a textarea or another div element with varying height?

<!DOCTYPE html5>
<html>
    <head>
        <style>
div.a {
    flex:1 1;
}

div.b {
    flex:1 1;
}

div.form {
    /*display: table;*/
    width: 100%;
}

div.form div {
    display: table-row;
    width: 100%;
}

div.form div label {
    display: table-cell;
    text-align: right;
    padding-right: 5px; 
}

div.form div input {
    width: 100%;
}
        </style>
    </head>
    <body>
        <div style="display:flex">
            <div class="a">
                <div class="form">
                    <div>
                        <label>a</label>
                        <input/>
                    </div>

                    <div>
                        <label>askldak  asd ls</label>
                        <input/>
                    </div>
                    <div>
                        <label>askldakasda   asdls</label>
                        <input/>
                    </div>
                </div>
            </div>
            <div class="b">b</div>
        </div>
    </body>
</html>

Edit 1

Check out this image from the second post

After attempting to implement the provided example, I noticed that the labels did not align correctly as expected.

Answer №1

To accomplish this, utilize the power of flexbox

.form div {
  display: flex
}
.form div input {
  flex: 1
}
<div>
  <div class="a">
    <div class="form">
      <div>
        <label>a</label>
        <input/>
      </div>

      <div>
        <label>askldak asd ls</label>
        <input/>
      </div>
      <div>
        <label>askldakasda asdls</label>
        <input/>
      </div>
    </div>
  </div>
  <div class="b">b</div>
</div>

If you desire for all label elements to have equal size, follow these steps:

.form div {
  display: flex
}
.form div label {
  flex: 1
}
.form div input {
  flex: 4
}
<div>
  <div class="a">
    <div class="form">
      <div>
        <label>a</label>
        <input/>
      </div>
      <div>
        <label>askldak asd ls</label>
        <input/>
      </div>
      <div>
        <label>askldakasda asdls</label>
        <input/>
      </div>
    </div>
  </div>
  <div class="b">b</div>
</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

Struggling to eliminate the unseen padding or white space preventing the text from wrapping under the button

Just starting out with html and css and could use some assistance with a small issue I've come across. While redesigning a website, I have a three-button form (Donate, Find, Subscribe). I'm trying to get the paragraph underneath the Donate and Fi ...

What could be the reason that the style tag present in this SVG is not impacting any of the elements?

My SVG file consists of a single element with a fill set to red: <svg width="100" height="60" viewBox="0 0 100 60" fill="none" xmlns="http://www.w3.org/2000/svg"> <path fill="red" d=&qu ...

The Django form isn't displaying properly after being imported into the HTML file

Can anyone help me figure out why my Django form isn't appearing in my HTML template? I've tried everything on this site and nothing seems to work. Any suggestions? views.py def handleForm(request): context = {} if request.method == &apo ...

What are the guidelines for incorporating tag names in CSS selectors?

I created a button that when clicked, displays a table containing actors. I have named them as follows: <button class="actors">Show actors</button> <table class="actors">...</div> Next, I want to style and access them in my CSS (a ...

How can I upload a folder with subfolders and keep the structure intact?

I'm working on a form that allows me to upload a folder containing both files and subfolders with more files inside. I managed to upload the folder successfully, but when I check my directory, all the files are in one folder without the subfolders. My ...

Creating a personalized layout for your WordPress posts with custom card designs

As a new developer diving into the world of WordPress, I am aiming to create a sleek and minimalistic grid/card layout for my website. Struggling to achieve this, I seek guidance on how to replicate the design seen in the following link: desired result H ...

HTML - Lists Not Displaying Properly in Numerical Order

My goal with HTML is to: Produce 2 Numbered Lists Nest an Unordered List within Each Ordered List and add elements inside Unfortunately, my numbering isn't displaying correctly. Instead of 1. 2. etc., I'm seeing 1. 1. Below is the code I am u ...

What is the best way to transform Adobe XD designs into HTML and CSS with Vue.js?

I am looking to create a single page application with vue.js and came across this helpful reference at . A colleague has created a prototype using Adobe XD, and now my task is to transform it into HTML/CSS while making it functional and connecting it to an ...

Unable to insert flag image into the <option> tag

Struggling to add a flag image to the options using any method (html,css)! <select> <option value="rus" selected>Rus</option> <option value="Uzb" >Uzb</option> <option value="eng">Eng</option> </s ...

What are the advantages of implementing HTML5 canvas for animation?

I'm a beginner in html5 and I've recently been experimenting with the canvas element. Can anyone provide insight on when the canvas would be most beneficial or practical to use? Essentially, what are some scenarios where it is recommended to util ...

Understanding the behavior of the enter key in Angular and Material forms

When creating forms in my MEAN application, I include the following code: <form novalidate [formGroup]="thesisForm" enctype="multipart/form-data" (keydown.enter)="$event.preventDefault()" (keydown.shift.enter)="$ev ...

Can someone please share the steps on how to insert a new row into a PrimeNg DataTable component

I am currently working on enhancing a table by adding a new row underneath the existing data list. This new row will contain a save button and allow users to insert information, such as saving a phone number. While I have indicated where the new row should ...

Hiding the last row of floated elements only if it is not an even row can be achieved using CSS

I am trying to find a quick and easy solution (either using CSS or jQuery) to hide the last row of floated elements if they are not even. Take a look at this JS Fiddle example: http://jsfiddle.net/cohhvfjn/ There is an issue when resizing the HTML contai ...

I utilized the "data-target" attribute to ensure the active link retains its style. Is there a way to preserve the style when navigating away from an active link?

Can someone assist me with this re-upload? I need help tweaking my code to maintain style when navigating between pages using the "data-target" attribute. Currently, the style is being lost when moving from the original link (e.g., "link/sub01.html") to a ...

Map on leaflet not showing up

I followed the tutorial at http://leafletjs.com/examples/quick-start/ as instructed. After downloading the css and js files to my local directory, I expected to see a map but all I get is a gray background. Can anyone advise me on what might be missing? T ...

Django (HTML) - Interactive tag in template not functioning

Currently, I am immersed in a Django project that involves incorporating custom checkbox forms. However, I ran into an issue where two identical code chunks are used with different forms. The problem arises when the label in the first sample is clickable ( ...

Troubleshooting issues with the sidebar navigation in Laravel project using Vue and AdminLTE

I successfully installed AminLte v3 via npm in my Laravel + vue project and everything is functioning properly. However, I am facing an issue when I attempt to click on the main menu item in the Side navbar that is labeled as <li class="nav-item has-tr ...

`I noticed that the CSS appears differently when viewed in Mozilla compared to Chrome``

Why do the images with the same CSS property appear different in Chrome and Mozilla? I want them all to float left with equal margins between them, covering the complete area horizontally despite having varying image widths. I'm unsure why there is a ...

What is the method for changing the icon color to dark in Twitter Bootstrap?

I am facing an issue where the icon in my menu tab turns white when active, making it difficult to see. Is there a way to change the color of the icon to black? I have found a class icon-white to make it white, but there is no corresponding class to make ...

Increased height of iPad screen in landscape mode with no body content displayed (iOS7)

The issue: An unusual problem occurs when the specified URL is loaded on an iOS7 iPad in landscape mode; a vertical scrollbar appears. There is absolutely no content within the body, and it seems to be adjusting the body/html margin/padding. It should be ...