Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS label text right below input element

Tags:

css

I have input text's and label tags. I can't figure out the CSS to get the label text to align right below the input text. Here's a snippet of the HTML:


<form id="sg1">
    <label for="member1">member 1</label>
    <input name="member1" id="member1" value="jack" />
    <label for="member2">member 2</label>
    <input name="member2" id="member2" value="carter" />
    <label for="member3">member 3</label>
    <input name="member3" id="member3" value="jackson" />
    <label for="member4">member 4</label>
    <input name="member4" id="member4" value="tielk" />    
</form>​

Trying to get:

[input box 1]    [input box 2]
      label 1          label 2

etc, with all elements.

like image 426
ina Avatar asked Aug 11 '10 23:08

ina


People also ask

How do you put input under a label in CSS?

<input type="text" id="lname" name="lastname" placeholder="Your last name..">

How do I add text below input field?

The fastest solution is to use add display: inline-block to your input tag. I suggest you to inspect your page and test your changes on the html and copy it inside your css file once you're satisfied. Show activity on this post. You just need to put display: block for error class so it will come below the input box.

How do you display input and label side by side?

We specify the margin-bottom of our <div> element. Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.

How do you put an input element on the same line as its label?

Using table cell attribute in display property: Make a label inside a div and give the display property. To make the input element and span as equally placed use table-cell attribute in those tags. This attribute makes the element behaves a td element.


2 Answers

A quickly whipped up example that works:

input {
  display: inline-block;
  width: 6em;
  position: relative;
  top: -3em;
}

label {
  display: inline-block;
  width: 6em;
  margin-right: .5em;
  padding-top: 1.5em;
}
<form id="sg1">
  <label>member 1 <input name="member1" id="member1" value="jack" /></label>
  <label>member 2 <input name="member2" id="member2" value="carter" /></label>
  <label>member 3 <input name="member3" id="member3" value="jackson" /></label>
  <label>member 4 <input name="member4" id="member4" value="tielk" /></label>
</form>​

Could be improved, but I find it cleaner than extraneous divs, and it degrades much nicer than the label-after-input-approach when CSS support is absent. Personally, I prefer to nest the inputs in the labels anyway.

like image 178
You Avatar answered Oct 21 '22 11:10

You


Use a table (one input/label pair per cell) or left-floating divs (one input/label pair per div). Example:

<div class="pair">
    <input type="text" name="foo" value="bar" /><br />
    <label for="foo">shabba</label>
</div>
<div class="pair">
    …
</div>

CSS:

div.pair {
    float:left;
}
like image 23
scy Avatar answered Oct 21 '22 11:10

scy