Fixed width element with variable width input

5.2k Views Asked by At

Your probably going to say this has been asked before but this is a variation with a bug. So we are all aware of the technique used to answer this question: Fixed width div on left, fill remaining width div on right

However this does not work if the variable width element is an input tag.

http://jsfiddle.net/8pk4K/2050/

even overriding the inputs default css doesnt fix this:

display: block;
overflow:hidden; 
background-color:green;
height: 100px;
width: auto;

Iv been playing with this for ages, it only happens on input tags, if you replace it with a span (default display inline but set it to display block) it still works.

Any idea why this only doesnt work for input tags and nothing else?

EDIT: For clarification, I know that the fix for this is to put the input into a div and apply width 100% to the input. My question is why this is necessary, not how to fix it.

3

There are 3 best solutions below

10
Luís P. A. On BEST ANSWER

You can use calc to produce the width what you desire because inputs are replaced elements that have intrinsic dimensions just like images

CSS

.header-right{
    display: block;
    overflow:hidden; 
    background-color:green;
    height: 100px;
    border: none;
    width: calc(100% - 240px); //Add this
    }

Note: You must give a dimension (width) to the select or otherwise give you the default browser width

DEMO HERE

1
kishan On

Try adding width in % for both .header and .header-right. Like

.header{
width:20%;
}
.header-right{
width:80%;
}
1
GreyRoofPigeon On

I know the problem, styling form elements will always be a pain in the ass.

I've came up with this work around, by wrapping the input in the right div.

<div class="header"></div>
<div class="header-right">
    <input type="text" />
</div>
.header{
    float:left;
    background: #efefef;
    background-repeat: no-repeat;
    width: 240px;
    height: 100px;
    }

.header-right{
    overflow:hidden; 
    background-color:#000;
    height: 100px;
    position: relative;
    }
.header-right input {
    background: green;
    position: absolute;
    width: 100%;
    height: 100%;
}

JSFiddle