Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make HTML input stretch to fill space in container

Tags:

html

css

stretch

There is a <div id="MyDiv"> element which is resized depending on page width. It contains <input> and <a> elements.

How can I make the <a> align by the right side of the <div> and the <input> stretch to fit the free space between <div>'s left side and <a>'s right side?

Also the <a> should not jump to the next line and there are also defined min-width and max-width on the container. Here is the code:

<div id="MyDiv" style="background:#AAAAAA; width:100%; min-width:300px; max-width:600px;">
    <input type="text" id="MyInput"/>
    <a id="MyButton" href="#">Button</a>
</div>

and demo.

like image 692
va. Avatar asked Mar 08 '13 09:03

va.


People also ask

How do you enlarge an input in HTML?

If you simply want to specify the height and font size, use CSS or style attributes, e.g. //in your CSS file or <style> tag #textboxid { height:200px; font-size:14pt; } <! --in your HTML--> <input id="textboxid" ...> Save this answer.

Why is input bigger than div?

It means that border on the input box is actually inside the width of the input rather than being added onto the outside. This is what is making the input larger than the container. The points he makes about padding also apply for the border. As noted in other answers, it may need width: 100%; height: 100% .

How do you enlarge input in CSS?

The simple way to make an input resize freely is to set it to it to have width: 100% , and then place it inside an inline-block container with a min-width ( min-width sets the default width for the input ). Resize the container to resize the input .


2 Answers

This can be done easily with CSS3 although it will not work in IE9 or below. IE10 is the first Internet Explorer version to support the standard. The other browsers already support the relevant Flexible Box properties.

div {
  background: #AAAAAA;
  width: 100%;
  min-width: 300px;
  max-width: 600px;
  display: -webkit-flex;
}

input {
  -webkit-flex: 1;
}

a {
  margin: 0 10px;
}
<div>
  <input type="text" />
  <a href="#">Button</a>
</div>
like image 87
andyb Avatar answered Sep 21 '22 14:09

andyb


Best option is to use jQuery to calculate the width. Try this:

$( window ).bind("resize", function(){
    fitInput();
});
//resize on window change

$(document).ready(function(){
   fitInput(); 
});
//resize after loading

function fitInput(){
    var divW = $("#MyDiv").width();
    var buttonW = $("#MyButton").width();
    $("#MyInput").width(divW - buttonW - 10);
}
//resize function

Fiddle: http://jsfiddle.net/Burnacid/aGHqV/4/

like image 25
s.lenders Avatar answered Sep 22 '22 14:09

s.lenders