Label and text box on the same line using css

前端 未结 4 821
离开以前
离开以前 2020-12-08 15:30

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 s

相关标签:
4条回答
  • 2020-12-08 16:08

    Although i have not tried the accepted answer by kinakuta, it requires you to re-arrange the Visual Studio generated view; I would approach as follows, if you don't want to re-arrange the autogenerated layout., i.e. to keep the format

    <div class="editor-label" />
    <div class="editor-field" />
    
    <div class="editor-label" />
    <div class="editor-field" />
    
    etc...
    

    The following CSS appears to work for me. Feel free to offer improvements. (it looks very similar to the answer by Pa0l0)

    <style type="text/css">
        .editor-label, .display-label
        {
            clear:both;
            float:left;
            width:150px;
            margin:1em 0 0 0;
            font-weight:bold;
        }
        .editor-field, .display-field
        {
            margin:1em 0 0 0;   
            min-height:1.5em;
        }
    </style>
    
    0 讨论(0)
  • 2020-12-08 16:13

    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
    }
    
    0 讨论(0)
  • 2020-12-08 16:15

    I'm using this css

    .editor-label
    {   display: block;
        float: left; 
        padding: 0; 
        width: 150px;
        margin: 1em 1em 0 0;
    }
    
    .editor-field
    {
        width:auto;
        margin: 0.5em 0 0 0;
        overflow: auto;
        min-height: 2em;
    }
    
    0 讨论(0)
  • 2020-12-08 16:19

    A few of these answers wasn't quite what I was looking for. This bit in my CSS seems to work for my needs.

    input,
    select,
    textarea{
        display:inline-block !important;
    }
    
    0 讨论(0)
提交回复
热议问题