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.
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.
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% .
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 .
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>
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With