How to align all fields as label width grows

后端 未结 5 2479
暖寄归人
暖寄归人 2021-02-20 18:05

I have a form where the labels are on the left and the fields on the right. This layout works great when the labels have small amounts of text. I can easily set a min-widt

相关标签:
5条回答
  • 2021-02-20 18:15

    Either use tables, or some kind of CSS grid-layout framework like blueprint. http://www.blueprintcss.org/

    0 讨论(0)
  • 2021-02-20 18:19

    I think this is what you're looking for:

    label
    {
        width: 150px;
        display: inline-block;
    }
    input[type=text]
    {
        display: inline-block;
    }
    

    http://jsfiddle.net/rQ99r/5/

    0 讨论(0)
  • 2021-02-20 18:19

    What about this solution?

    <fieldset id="fs1">
        <div class="column">
            <label>Name</label>
            <label>Username text text text </label>
        </div>
        <div class="column">
            <input type="text" />
            <br />
            <input type="text" />
        </div>
    </fieldset>
    

    css:

    label
    {
      display: block;
    }
    .column{
        float:left;
    }
    

    fiddle: http://jsfiddle.net/steweb/xzHe6/

    0 讨论(0)
  • 2021-02-20 18:26

    You're trying to create tables... without tables? Seems to me this is a situation where using tables is a perfectly fine solution. And I don't see what the 'tag-hell' should be. A tables needs a few more tags, sure, but so what? You could wrap everything in divs and simulate table cells with floats, but then you'll have a css-hell ;) .

    0 讨论(0)
  • 2021-02-20 18:33

    Width align left and right columns and vertical align top

    <fieldset>
        <label>Name</label><input type="text" /><br />
        <label>Username (aka aka aka aka aka aka aka aka aka aka aka)</label><input type="text" /><br />
        <label>Password</label><input type="text" /><br />
        <label>Confirm Password</label><input type="text" /><br />
    </fieldset>
    
    <br />
    <label>Another label</label>
    

    CSS:

    fieldset label {
      display: inline-block;
      vertical-align: top;
      width: 150px;
      text-align: right;
      font: 11px Verdana;
      color: #003399;
      margin: 4px 4px 4px 4px;
    }
    
    fieldset input {
      display: inline-block;
      vertical-align: top;
      margin: 4px 4px 4px 4px;
    }
    

    fiddle: https://jsfiddle.net/46okafre/

    0 讨论(0)
提交回复
热议问题