html - Bootstrap .form-horizo​​ntal 中的单选按钮

标签 html css twitter-bootstrap

我正在用头撞墙,试图用 .form-horizo​​ntal 中的单选按钮从 Bootstrap 中获得理智的行为。

问题在于让单选按钮及其标签对齐,同时还要与表单的其余部分对齐。我管理过的最好的是:

    <legend>New user</legend>
    <form accept-charset="UTF-8" action="#" class="form-horizontal" id="new_user" method="post">

        <div class="control-group">
          <label class="control-label" for="user_affiliation">Affiliation</label>
            <div class="controls">
              <input id="user_affiliation" name="user[affiliation]" size="30" type="text" />
            </div>
          </div>

        <div class="control-group">
          <div clas="controls">
            <div class="radio">
              <label>
                <input id="user_role_managing_editor" name="user[role]" type="radio" value="Managing editor">
                Managing editor
              </label>
              <label>
                <input id="user_role_area_editor" name="user[role]" type="radio" value="Area editor">
                Area editor
              </label>
              <label>
                <input id="user_role_author_or_referee" name="user[role]" type="radio" value="Author or referee">
                Author/referee
              </label>
            </div>
          </div>
        </div>

        <div style="text-align: center;">
          <input class="btn btn-primary" name="commit" type="submit" value="Register" />
        </div>

    </form>

这让我明白了:enter image description here

如您所见,单选按钮与表单的其余部分格格不入。他们应该与文本字段垂直对齐,或者做一些比拥抱左边更明智的事情,就好像这是一个 .form-inline

如有任何帮助,我们将不胜感激。

最佳答案

没有黑客!,使用 bootstrap2:

<div class="control-group">
  <label class="control-label"> User role</label>
  <div class="controls">
    <label class="radio">
      <input type="radio" name="user[role]" id="user_role_managing_editor" value="Managing editor" checked>
      Managing editor
    </label>
    <label class="radio">
      <input type="radio" name="user[role]" id="user_role_area_editor" value="Area editor">
      Area editor
    </label>
    <label class="radio">
      <input type="radio" name="user[role]" id="user_role_author_or_referee" value="Author or referee">
      Author/referee
    </label>
  </div>
</div>

演示:http://jsfiddle.net/GX6q4/1/

更新:这是 Bootstrap3 版本:

<legend>New user</legend>
<form accept-charset="UTF-8" action="#" class="form-horizontal" id="new_user" method="post">
  <div class="form-group">
    <label class="col-xs-2 control-label" for="user_affiliation">Affiliation</label>
    <div class="col-xs-10">
      <input class="form-control" id="user_affiliation" name="user[affiliation]" size="30" type="text" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-xs-2 control-label">User role</label>
    <div class="col-xs-10">
      <div class="radio">
        <label>
          <input type="radio" name="user[role]" id="user_role_managing_editor" value="Managing editor" checked>Managing editor
        </label>
      </div>
      <div class="radio">
        <label>
          <input type="radio" name="user[role]" id="user_role_area_editor" value="Area editor">Area editor
        </label>
      </div>
      <div class="radio">
        <label>
          <input type="radio" name="user[role]" id="user_role_author_or_referee" value="Author or referee">Author/referee
        </label>
      </div>
    </div>
  </div>
</form>

演示:http://jsfiddle.net/24sx72mn/1/

Bootstrap 4 版本:

<legend>New user</legend>
<form accept-charset="UTF-8" action="#" id="new_user" method="post">
  <div class="form-group row">
    <label class="col-sm-2 col-form-label" for="user_affiliation">Affiliation</label>
    <div class="col-sm-10">
      <input class="form-control" id="user_affiliation" name="user[affiliation]" type="text" />
    </div>
  </div>
  <div class="form-group row">
    <label class="col-sm-2 col-form-label">User role</label>
    <div class="col-sm-10">
      <div class="form-check">

        <input type="radio" class="form-check-input" name="user[role]" id="user_role_managing_editor" value="Managing editor" checked>
        <label class="form-check-label" for="user_role_managing_editor">
          Managing editor
        </label>
      </div>
      <div class="form-check">

        <input type="radio" class="form-check-input" name="user[role]" id="user_role_area_editor" value="Area editor">
        <label class="form-check-label" for="user_role_area_editor">
          Area editor
        </label>
      </div>
      <div class="form-check">

        <input type="radio" class="form-check-input" name="user[role]" id="user_role_author_or_referee" value="Author or referee">
        <label class="form-check-label" for="user_role_author_or_referee">
          Author/referee
        </label>
      </div>
    </div>
  </div>
</form>

演示:http://jsfiddle.net/p8wyd7s8/1/

关于html - Bootstrap .form-horizo​​ntal 中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18952153/

相关文章:

javascript - SCRIPT1002 : Syntax Error, 第 1 行字符 6

javascript - “比例”选项似乎打破了 Chart.js 图表

html - 如何将 svg 虚线从一点动画化到另一点?

html - 如何在不设置行高的情况下设置div中的行间距

html - 如何摆脱垂直滚动条

html - 将一些文本周围的填充添加到两个 div 列(内容、侧边栏)中。侧边栏列移动到内容列下方

html - CSS ..我不能用像 chrome 这样的 CSS 重排网站

javascript - 如何为 Bootstrap 模态窗口指定主要和次要操作?

javascript - Angular JS 在路由加载内容后应用 JS 插件

html - 如何使用 Bootstrap 3 对齐表单中的单独行?