Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stretch the width of an element, so that it's 100% - widths of its siblings?

Tags:

html

css

Say, I have the following unordered list. The button has width: auto. How do I style the elements, so #textField would stretch as much as possible, so the width of #textField and the button would add up to 100%? I.e. #textField's width == (100% of width) - (button's computed width).

<ul>   <li>     <input id="textField" type="text" /><input type="button" />   </li> </ul> 

So, for example, let's say 100% width of li is 100 pixels: if the button's computed width is 30px, #textField's width would be 70px; if button's computed width is 25px, #textField's width would become 75px.

like image 970
William Niu Avatar asked Sep 10 '10 06:09

William Niu


People also ask

How do you make an element full width in CSS?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

How do I stretch a element in HTML?

I.e. #textField 's width == (100% of width) - (button's computed width). So, for example, let's say 100% width of li is 100 pixels: if the button's computed width is 30px, #textField 's width would be 70px; if button's computed width is 25px, #textField 's width would become 75px.

How do you find the full width of an element?

Using width, max-width and margin: auto; The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

Which element occupies full width of the screen?

Note: A block-level element always starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).


1 Answers

You can quickly achieve this effect using a mixture of float and overflow: hidden:

<ul>     <li>         <input class="btn" type="button" value="Submit"/>         <div class="inputbox"><input id="textField" type="text" /></div>     </li> </ul> 

CSS:

ul {    list-style: none;    padding: 0; } .btn { float: right; } .inputbox {    padding: 0 5px 0 0;   overflow: hidden; } .inputbox input {    width: 100%;   -webkit-box-sizing: border-box;   -moz-box-sizing: border-box;   box-sizing: border-box; } 

Preview (with box-sizing): http://jsfiddle.net/Wexcode/E8uHf/546/

Here is how it looks without box-sizing: http://jsfiddle.net/Wexcode/E8uHf/

like image 78
Wex Avatar answered Oct 02 '22 06:10

Wex