21

I have the following "line" in my web page

<div style="width:100%">
"Some Text" <DropDown> "Some more text" <TextBox> <Button> <Button>
</div>

The DropDown control I don't really have control over the width, as it sizes to fit whatever the longest option value is. The Buttons are fixed width. How can I get the TextBox to fill all available remaining width?

I tried putting it in a table but run into the same problem, ie how do I make all other columns as small as possible to fit their content and then the TextBox column fill remaining width?

Hacky solutions are fine if necessary, I've long ago given up any pretence of even caring about CSS standards when it's so difficult to do the simplest thing.

Edit: to clarify, by TextBox I mean <input type="text"/>

3
  • You can control the width of the dropdown with a width style attribute. <select style="width:200px;">...</select> et al Commented Jul 7, 2010 at 10:32
  • @Pradyumma: I don't want to set the width of the dropdown. It should size to fit it's contents. Commented Jul 7, 2010 at 10:33
  • For the record, by TextBox you mean an <input type="text"/> not a <textarea></textarea> correct? Commented Jul 7, 2010 at 10:41

7 Answers 7

35

UPDATED ANSWER IN 2018

Just came across this old question and there is now a much simpler and cleaner way to achieve this by using the CSS Flexible Box Layout Model which is now supported by all major browsers.

.flex-container {
  display: flex;
}

.fill-width {
  flex: 1;
}
<div class="flex-container">
	<label>Name:</label><input class="fill-width" type="text" /><span>more text</span>
</div>

Hope this helps someone!

OLD ANSWER BELOW

I know this is an old question but the correct solution isn't here so just in case somebody else finds there way here:

The solution is to wrap the textbox in a span.

HTML:

<label>Input</label>
<span>
    <input/>
</span>

CSS:

label {
    float: left;
}

input {
    width: 100%;
    box-sizing:border-box;
}

span {
    display: block;
    overflow: hidden;
}

See this fiddle for an example (now slightly out of date as I removed padding in favor of using border-box on the input).

Sign up to request clarification or add additional context in comments.

6 Comments

I have created another version of this fiddle which clears out all of the superfluous code: jsfiddle.net/josh_fuggle/EjLfP/1
@josh-fuggle Thanks josh I have updated the answer to include your fiddle as it is indeed much clearer.
No problems, glad it helps :)
@dekin88, a good solution, but did you mean to use padding-left: 5px;? That would make more sense.
@Sheridan Actually the padding in Josh's fiddle is supposed to be on the right as the edge of the text input was being cut off if it is removed. I fixed by applying box-sizing: border-box; to the input element in the example and removing the padding completely as it is simply aesthetic, not functional.
|
6

Here is what worked for me. Note that in some cases I had to use &nbsp; between items otherwise it fell to the next line.

.flexContainer {
    display: flex;
    width:100%;
}
input {
    width: 100%;
}
<div class="flexContainer">
<span>text:&nbsp;</span> <input/> &nbsp; <label>text</label>
</div>

3 Comments

This is not the correct way to use a flex box. You should never need to use non-breaking spaces to make your layout work. I've updated my answer with the correct use of a flex box.
@dekin88 I see you updated your answer based on mine and improved it. OK.
No offence intended. I just wanted to note here for any people who may view this in the future that this isn't the way flex box is intended to be used.
3

I would suggest the following:

<div style="width:100%; white-space:nowrap">
"Some Text" <DropDown> "Some more text" <TextBox style="width:100%"> <Button> <Button>
</div>

1 Comment

That doesn't work either, still makes the textbox 100% of div width so now it just goes off the right of the screen.
1

You can set the width of the textbox to 100% (with css as you did with the div), so it'll be spanned to the full extent of it's parent (assuming the parent tag extends the full screen).

1 Comment

This just puts the textbox on it's own line. I want all the content on a single line.
0

The only way I see it feasible is with Javascript, so you get the width of the div (in pixels), deduct the current size of the other controls and add the result as with of the input. That would require you to put the text inside a span control (so you can get its size).

Comments

0

Set the width of the textbox to 100% with the css property display: inline;?

1 Comment

If you set the display:inline style, the width style will no longer work because the element is inline. To use the width style, you need to have the display as block or inline-block...or something that will let you set the width. If you do not want the width property to effect your style, display:inline works great because the width style no longer applies.
0

This is not doable in CSS as far as Chrome is concerned. The feasible way is by manipulating the size attribute of the form control through JavaScript. If the length of the string is 25, add an extra of at least 10 for the clearance.

HTML

<!-- "The quick brown fox jumps over the lazy dog" is 43 characters long -->
<input type="text" value="The quick brown fox jumps over the lazy dog" id="textbox1" />

JavaScript / jQuery

<script>
    var value = jQuery('#textbox1').val();                  // 43
    jQuery('#textbox1').attr('size', value.length + 10 );   // resizes the textbox
</script>

Another option also is by pre-calculating the size from a back-end script.

PHP / HTML

<?php
$value = "The quick brown fox jumps over the lazy dog";
?>
<input type="text" value="<?php echo $value; ?>" size="<?php echo ( strlen($value) + 10 ); ?>" />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.