Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have an element fill remaining horizontal space

Tags:

html

css

width

I have been asked to create a form where each input has a label at its natural width (to the left) and the input alongside (on the right, within the same row) filling the remaining space so that the right edge of each input is vertically aligned.

Other than setting an explicit width on each input is there a way of achieving this? In essence, forcing the input to take up 100% of the remaining space of its parent?

None of the answers for How to make a div to fill a remaining horizontal space? seem to work for me but I'm not sure why my situation would be different.

<div class="name-field">
     <label for="name">Name:</label>
     <input type="text" id="name" />
</div>

<div class="email-field">
     <label for="email">Email address:</label>
     <input type="email" id="email" />
</div>

In the situation above I want each wrapping div to be 100% width and the elements within to fill that width entirely without any space between them.

like image 882
rushtoni88 Avatar asked Oct 29 '25 14:10

rushtoni88


1 Answers

One approach is to use flexbox:

div {
  display: flex;
}

div input {
  flex: 1;
}
<div class="name-field">
     <label for="name">Name:</label>
     <input type="text" id="name" />
</div>

<div class="email-field">
     <label for="email">Email address:</label>
     <input type="email" id="email" />
</div>
like image 149
sol Avatar answered Oct 31 '25 03:10

sol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!