Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make input[type=text] to fill remaining horizontal space?

Tags:

html

css

Does any have an idea how to do so? I created this fiddle http://jsfiddle.net/matusko/2pctr9ok/3/ and all I want to do is, that the input behave the same way as the upper divs.

CSS:

.left {
    float:left;
    width:180px;
    background-color:#ff0000;
}
.right {
    width: 100%;
    background-color:#00FF00;
    display: block;
}

HTML:

<div>
    <div class="left">
        left
    </div>
    <div class="right">
        right
    </div>
</div>
<br/>
<div>
    <div class="left">
        left
    </div>
    <input type="text" placeholder="right" class="right"/>
</div>

I dont understand why input doesnt behave like div, even when propriety inspector says that its display is block.

like image 295
Matúš Bartko Avatar asked Jun 17 '15 18:06

Matúš Bartko


People also ask

How do you make a div fill a remaining horizontal space?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent.

How do you put a space in a text field?

To add space inside a form's text field, use the CSS padding property.

How do you set the width and height of an input type text?

The height attribute specifies the height of the <input> element. Note: The height attribute is used only with <input type="image"> . Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded.


2 Answers

You can use calc in CSS to dynamically calculate the width for you.

Sample below:

.left {
  float: left;
  width: 180px;
  background-color: #ff0000;
}
.right {
  width: calc(100% - 180px);
  background-color: #00FF00;
  display: inline-block;
}
input[type="text"] {
  box-sizing: border-box;
  width: 100%;
}
<div>
  <div class="left">left</div>
  <div class="right">right</div>
</div>
<br/>
<div>
  <div class="left">left</div>
  <div class="right">
    <input type="text" placeholder="right" />
  </div>
</div>
like image 186
Dan Hayden Avatar answered Oct 12 '22 03:10

Dan Hayden


For < IE9 I would suggest the following http://jsfiddle.net/2pctr9ok/4/

Putting the left bottom in position:absolute, the whole bottom block in overflow:hidden and apply a padding-left:180px on the input.

like image 25
Villeneuve Michaël Avatar answered Oct 12 '22 01:10

Villeneuve Michaël