Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label and text box on the same line using css

Tags:

When creating a new asp.net mvc3 app you get the logon and register form with a label above the text field. I want to change it so that both the label and field are on the same line and aligned

The following doesn't work

@using (Html.BeginForm()) {
<div>
    <fieldset>
        <legend>Account Information</legend>

        <div class="editor-label">
            @Html.LabelFor(m => m.UserName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.UserName)
            @Html.ValidationMessageFor(m => m.UserName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(m => m.Password)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.Password)
            @Html.ValidationMessageFor(m => m.Password)
        </div>

        <div class="editor-label">
            @Html.CheckBoxFor(m => m.RememberMe)
            @Html.LabelFor(m => m.RememberMe)
        </div>

        <p>
            <input type="submit" value="Log On" />
        </p>
    </fieldset>
</div>

CSS:

.display-label, 
.editor-label 
{
      display:inline-block;
      width: 200px;    
      text-align: right;   
      margin-right: 10px;

}

.display-field, 
.editor-field 
{
    display:inline-block;
     margin: 0.5em 0 0 0;
}
like image 891
user9969 Avatar asked Jun 22 '11 09:06

user9969


People also ask

How do I add textbox and label in same line?

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.

How do you get both the heading and icon on the same line in CSS?

Just use position:absolute for the icon inside the icon-text block. Save this answer.

How do you put space between label and textbox in HTML?

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


1 Answers

I typically float my label left and the input lines up next to it. Here's an example: http://jsfiddle.net/qXFLa/

Here's an example of how I'd rearrange your code:

<div class="editor">
  @Html.LabelFor(m => m.UserName)
  @Html.TextBoxFor(m => m.UserName)
  @Html.ValidationMessageFor(m => m.UserName)
</div>

Then, for your css:

.editor label {
  float: left;
  width: 30px;   // just putting an example here - but give them a width to align better
  text-align: right; // this right-aligns them with the input
}

.editor input[type=text] {
  width: 80px; // just putting an example value in here to make your inputs uniform in length
  margin: 0 0 0 10px; // just some breathing room from the labels
}
like image 51
kinakuta Avatar answered Sep 18 '22 15:09

kinakuta