HTML實現表單元素對齊的例子

來源:互聯網
上載者:User

網頁裡,常見如下的表單代碼:

<div>
  <label for='email'>郵箱地址:</label>
  <input name='email' type='text'/>
</div>
<div>
  <label for='password'>密碼:</label>
  <input name='password' type='text'/>
</div>
通常,我們想達到下面這種排版效果:

郵箱地址:


密碼:


Bootstrap 裡是這麼寫1:

<form class="form-horizontal">
  <div class="form-group">
    <label for="inputEmail3" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3" placeholder="Password">
    </div>
  </div>
</form>

代碼中用到了 grid,然後兩個 label 設定同樣的寬度,並且 label 內的文本靠右對齊。

但如果不考慮移動端的情況的話,我覺得可以這樣寫:

<style>.u-textRight { text-align: right !important; }</style>

<div class='u-textRight'>
  <label for='email'>郵箱地址:</label>
  <input name='email' type='text'/>
</div>
<div class='u-textRight'>
  <label for='password'>密碼:</label>
  <input name='password' type='text'/>
</div>


label 與 input 都是 inline 元素,我在它們的包含塊上設定靠右對齊,這樣不需要給 label 指定寬度,代碼也更簡潔。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.