1

I'm trying to let a label and an input field fill the whole width of a from. Here is my attempt. This is the HTML

<form>
    <p>
        <label>Username:</label>
        <input/>
    </p>
</form>

and CSS

form {
    widht: 400px height: 500px;
    background-color: lightgrey;
}
label {
    float: left;
}
input {
    width: 100%;
    box-sizing: border-box;
}

When I put a with: 100% in the input field it moves below the label and without the width it is too small. Any suggestions ?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • 1
    possible duplicate of [Style input element to fill remaining width of its container](http://stackoverflow.com/questions/773517/style-input-element-to-fill-remaining-width-of-its-container) – user247702 Sep 26 '13 at 08:16

3 Answers3

2

Use a wrapper element around your input and set overflow: hidden; (Make sure you use a block level element, if you are using span than declare display: block; in your CSS)

Demo

<label>Blah Blah</label>
<div><input type="text" /></div>

label {
    float: left;
}
div {
    overflow: hidden;
}

input[type=text] {
    width: 100%;
}
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1

you have to set width attribute properly.

Live Demo

label {
    float: left;
    width: 15%;
}
input {
    width: 83%;
    box-sizing: border-box;
}
NaYaN
  • 1,300
  • 7
  • 11
0

If you firebug it, you will know that Your border is actually putting some padding to the container p. So, if you just put border to none then you will be good to do. If you want to use border and dont want the input to go outside the form, then you need to either shorten the width of the input a little and apply border or you give some padding to your form.

Try This CSS

form {
 background-color: #D3D3D3;
 height: 500px;
 width: 400px;
}
form p{
 float: left;
 position: relative;
 width: 100%;
}
label {
 float: left;
 position: relative;
 width: 100%;
}
input {
 border:0;
 float: left;
 height: 20px;
 margin: 0 auto;
 padding: 0;
 position: relative;
 width: 100%;
}

Here is the DEMO