CSS label text right below input element

后端 未结 4 1962
一个人的身影
一个人的身影 2021-02-05 10:08

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 HT

相关标签:
4条回答
  • 2021-02-05 10:34

    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;
    }
    
    0 讨论(0)
  • 2021-02-05 10:42

    You'd make the job a lot easier by wrapping each field (in this case, each input/label pair) in a div.

    0 讨论(0)
  • 2021-02-05 10:44

    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.

    0 讨论(0)
  • 2021-02-05 10:44

    You can use pure css to get this to achieve what you want, but it requires a lot of adhoc positioning stuff that you're better off not doing.

    The simplest way is to put the label beneath the input on the html:

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

    Then you can wrap each input/label pair with a div, and set the div like so:

    <form id="sg1">
        <div class="wrap">
            <input name="member1" id="member1" value="jack" />
            <label for="member1">member 1</label>
        </div>
        <div class="wrap">
            <input name="member2" id="member2" value="carter" />
            <label for="member2">member 2</label>
        </div>
        <div class="wrap">
            <input name="member3" id="member3" value="jackson" />
            <label for="member3">member 3</label>
        </div>
        <div class="wrap">
            <input name="member4" id="member4" value="tielk" />    
            <label for="member4">member 4</label>
        </div>
    </form>
    
    #sg1 div
    {
        clear: both;
        float: left;
    }
    

    Next you can put

    #sg1 label
    {
        float: right;
    }
    
    input
    {
        display:block;
    }
    
    0 讨论(0)
提交回复
热议问题