Make HTML stretch to fill space in container
There is an element <div id="MyDiv">, the size of which varies depending on the width of the page. It contains elements <input>and <a>.
How can I make the alignment <a>on the right side <div>and stretch <input>so that it matches the free space between the left side <div>and the right side <a>?
In addition, <a>should not go to the next line, and for the container are also defined min-widthand max-width. 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 .
CSS3, IE9 . IE10 - Internet Explorer, . Flexible Box.
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>jQuery . :
$( 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
You can use absolute positioning:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="MyDiv" style="position:relative;background:#AAAAAA; min-width:300px; max-width:600px;padding:0px 2px">
<input type="text" id="MyInput" style="padding:0px;margin:0px;border:0px;width:100%;"/>
<a id="MyButton" href="#" style="position:absolute;right:0px;top:0;padding:0px 2px; background:#aaa;">Button</a>
</div>
<body>
</html>