How do I evenly add space between a label and the input field regardless of length of text?

后端 未结 5 2014
悲哀的现实
悲哀的现实 2021-02-01 04:13

Let\'s say I have 10 input fields and before the input fields on the left side I have span tag that holds the text to indicate what the user should enter into the field. I did

5条回答
  •  青春惊慌失措
    2021-02-01 04:22

    2019 answer:

    Some time has passed and I changed my approach now when building forms. I've done thousands of them till today and got really tired of typing id for every label/input pair, so this was flushed down the toilet. When you dive input right into the label, things work the same way, no ids necessary. I also took advantage of flexbox being, well, very flexible.

    HTML:

    
    
    
    
    
    

    CSS:

    label {
      display: flex;
      flex-direction: row;
      justify-content: flex-end;
      text-align: right;
      width: 400px;
      line-height: 26px;
      margin-bottom: 10px;
    }
    
    input {
      height: 20px;
      flex: 0 0 200px;
      margin-left: 10px;
    }
    

    Fiddle DEMO


    Original answer:

    Use label instead of span. It's meant to be paired with inputs and preserves some additional functionality (clicking label focuses the input).

    This might be exactly what you want:

    HTML:

    
    
    
    
    
    
    
    
    

    CSS:

    label {
        width:180px;
        clear:left;
        text-align:right;
        padding-right:10px;
    }
    
    input, label {
        float:left;
    }
    

    jsfiddle DEMO here.

提交回复
热议问题