How can you enable a set of labels to expand and contract alongside their corresponding input fields?

Is there a way to create a group of labels that adjust their width based on the widest label without using tables in HTML or CSS? I need the input fields to shrink accordingly to fit the larger labels, as I deal with translations and cannot predict the width of each label.

Here is an example of what I'm looking for:

form {
  display: table;
  width: 100%;
}

label {
  width: 1%;
  display: table-cell;
  white-space: nowrap;
  padding-right: 1em;
}

input {
  display: inline-block;
  width: 100%;
}

.input-group {
  display: table-row;
}

* {
  line-height: 2em;
}

body {
  padding: 2em;
}
<form>
  <div class="input-group">
    <label for="name">Full name of your family and yourself</label>
    <input type="text" id="name" name="name" placeholder="optional">
  </div>
   <div class="input-group">
    <label for="email">Email</label>
    <input type="text" id="email" name="email" placeholder="optional">
  </div>
    <div class="input-group">
    <label for="phone">Phone</label>
    <input type="text" id="phone" name="phone" placeholder="optional">
  </div>
</form>

You can view this example on Codepen as well:

http://codepen.io/aboutandre/pen/bZRjqB

I am open to suggestions of a more elegant and efficient approach to achieve this layout without relying on tables. Any ideas?

Answer №1

Consider setting the width of the input fields to dynamically inherit from a specified determining input field. For instance, if the name field is the determinant, set its width to 100% and define the other two elements' widths as 'inherit'.

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

Sending data to a React component from regular HTML

I have a question about implementing a method to pass custom attributes from HTML elements as props to React components. Here's an example: function someFunction(props) { return <h1>props.something</h1> } HTML: <div id="someEl ...

Div blur event for editing content

Utilizing a content editable div as an input control has presented some challenges for me. Unlike a textarea or input element, the div does not send information to the server automatically. To work around this issue, I have implemented a solution where I u ...

Adding text identifiers to a clustered bar graph in the R programming language

My dataset displays information as follows: data = structure(list(region1 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), .Label = c("Location1", "Location2&quo ...

Guide on crafting a scrollable and touch-responsive grid using CSS and JavaScript

I am seeking guidance on creating a grid with a variable number of columns and rows. It should be contained within a separate div and should not interfere with other objects or alter the parent's size. The grid needs to be square and I am currently u ...

Setting text alignment in Bootstrap based on breakpoints

Imagine this code: import React from 'react' import DarkPattern from '../Assets/dark-pattern.jpg' const Profile = () => { return ( <div> <section className="bg-dark text-light text-center " ...

The search box does not trigger the Else statement when an invalid text is entered

ASP.NET Engine Upon entering an invalid User ID in the text box and clicking on search, instead of displaying "No Class found", the current output shows "Search Result". It appears that the else statement is not being executed as expected. HTML <div ...

Ensure that the pseudo element remains positioned between the background and main content

I am currently creating buttons using the anchor tag. However, I am facing issues with the desired effect not working as expected. The pseudo-element is overlapping the button and the text is getting hidden. My goal is to have the pseudo-element positioned ...

Instructions for adding a new line in a textarea on Internet Explorer when reading from a file

I'm facing a situation where I need to display the contents of a file (let's call it abc.txt) in a textarea on a JSP page. However, when I try to do so, all the contents are displayed in one line without any line breaks or carriage returns. Inte ...

Utilizing Django in conjunction with Vue to identify and address form validation errors

Utilizing a Django ModelForm with crispy forms for display on the template, upon completion of filling out the fields and clicking Submit, an email is triggered in the backend using Django's send_email functionality. However, the issue arises from th ...

How can I create a pop-out message box in HTML similar to the style used in Gmail or OkC

As someone who isn't very experienced in client development, I hope you'll forgive me for asking what might be a simple question that can easily be solved with Firebug. I'm interested in learning how to create a feature like the OKCupid or G ...

Turn on and off scrolling of DIV content when hovering over it

How can I create a div that automatically scrolls to the bottom on hover? I tried implementing the code below, but it doesn't work as expected. It jumps to the bottom of the div's height on first hover and transitions smoothly on second hover. . ...

Display an element exclusively for mobile users

I am trying to display a text element only when a link is clicked in the mobile view. I suspect there might be a syntax error in this code, but I can't pinpoint where it is. $("#Link1").click( function() { if($(window).width() > 800 ) { $("#T ...

Guide to Angular Directives: Assigning Attributes to innerHTML

Looking to add attributes to the inner HTML myTemplate.html <div class="custom-template"></div> HTML <template></template> directive app.directive('template', ['$compile', function($compile) { retur ...

Using Jquery for a Second Timer

I have a jQuery function that looks like the one below. The result is displayed in a span, but when the page is reloaded, this span briefly disappears and then reappears. Is there a way to prevent this from happening? I need it to stay visible at all tim ...

Modify the display of multiple divs within a main div

I am facing an issue with a parent div that contains multiple child divs. When there are many children, they all remain in a single row without adjusting into columns. Below is my current HTML: item1 item2 item3 ... <mat-card class="hove ...

Mastering the use of divs in CSS-3

Is there a way to position div elements on a webpage in various locations? Which CSS property is most effective for managing this issue? I'm having trouble setting the positions of multiple divs at different points on a page. ...

Create a Dynamic Moving Background Image with Endless Scrolling

Is it possible to make a background image animate infinitely and only move upwards without any jerkiness? I am struggling with setting it up this way. Can anyone provide guidance on achieving this? If you need more context, here is the link to the webpage ...

Locating the top 3 largest values in an HTML data table and highlighting them in red

Issue Description: I am working with data that displays the electricity consumption of different buildings in HTML tables. There is a possibility that these tables may contain duplicate values. With the use of jQuery, I am seeking a method to identify an ...

Unable to change Bootstrap variables within a Next.js project

I'm having trouble changing the primary color in Bootstrap within my NextJS project. I've tried different methods but nothing seems to work as expected. Here is the code snippet from my "globals.scss" file that I imported in "_app.js": $primary: ...

The Bootstrap Modal consistently shows the contents of the first record when used within a SQL While Statement

As a newcomer, I am in the process of converting an operational Bootstrap modal that houses a post form. Initially, the modal would open using a button with a specific Id and would always display the FORM contents from the first record found in the SQL w ...