Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input box 100% width, with label to the left

Tags:

css

I'm trying to have a fluid input box with 100% width, while having the label floated to the left. Here's what I currently have:

.left {
  float: left;
}

input {
  width: 100%;
}

<form>
    <label class="left">Name:</label>
    <input class="left" id="name" name="name"/>
    <div class="clear"></div>
</form>

This works, however it drops down below the label. If I use a parent div to assign the floats, then it doesn't span 100%. Any ideas?

Thank you!

like image 864
dzm Avatar asked Jul 15 '11 21:07

dzm


People also ask

How do you put input and label on the same line?

Using float and overflow attributes: Make a label and style it with float attribute. Now set the label float(position) left or right according to your requirement. This will align your label accordingly. Overflow property for input is used here to clip the overflow part and show the rest.

How do you put labels inside an input box?

Remember to add an id to the input and matching for attribute to the label. Without styling, the label will appear above the input. Adding an absolute position to the label will make it appear to be inside the input field. Adding position: relative to the container will keep the label inside and not outside.


1 Answers

See: http://jsfiddle.net/r2769/ (resize the window)

CSS:

.left {
    float: left;
}

.left2 {
    overflow: hidden;
    display: block;
    padding: 0 4px 0 10px
}
.left2 input {
    width: 100%
}

HTML:

<form>
    <label class="left" for="name">Name:</label>
    <span class="left2"><input id="name" name="name" /></span>
</form>

An explanation of the method is here.

like image 72
thirtydot Avatar answered Sep 23 '22 20:09

thirtydot