【问题标题】:Why does my Bootstrap input not align right?为什么我的 Bootstrap 输入不正确?
【发布时间】:2015-04-16 13:51:01
【问题描述】:

为什么我的 Bootstrap 输入不能右对齐(=> 请参阅第 3 个输入,占位符='不右对齐')?
td 表格元素具有定义对齐方式的样式...
当我在设置输入样式 text-align=right 时从 td 元素中删除样式元素时,它仍然是左对齐的。

<div class="row">
    <div class="col-md-6">
        <div class="well" style="width:840px">
            <table class='table borderless' style="width:800px">
            <tr>
                <td colspan="2">
                    <input class="form-control input-sm" style="width:300px;margin-bottom:0px" type="text" name="input1" placeholder="Input 1">
                    <input class="form-control input-sm" style="width:300px;margin-bottom:0px" type="text" name="input2" placeholder="Input 2">
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: right">
                    <input class="form-control input-sm" style="width:300px;margin-bottom:0px" type="text" name="input3" placeholder="Does not align right">
                </td>
            </tr>
            <tr>
            </table>
        </div>
    </div>
</div>

【问题讨论】:

    标签: html css twitter-bootstrap


    【解决方案1】:

    为什么我的 Bootstrap 输入不正确?

    因为.form-control 将拥有来自 bootstrap.min.css 的display: block。作为证明,如果您再次将其设为inline-block,它将起作用:

    .form-control { display: inline-block !important; }
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    
    <div class="row">
        <div class="col-md-6">
            <div class="well" style="width:840px">
                <table class='table borderless' style="width:800px">
                <tr>
                    <td colspan="2">
                        <input class="form-control input-sm" style="width:300px;margin-bottom:0px" type="text" name="input1" placeholder="Input 1">
                        <input class="form-control input-sm" style="width:300px;margin-bottom:0px" type="text" name="input2" placeholder="Input 2">
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: right">
                        <input class="form-control input-sm" style="width:300px;margin-bottom:0px" type="text" name="input3" placeholder="Does not align right">
                    </td>
                </tr>
                <tr>
                </table>
            </div>
        </div>
    </div>

    您可能真正想要的是您的控件立即表现得像那样。为此,请确保使用 Bootstrap 的 form-inline,例如像这样:

    <div class="row form-inline">
    

    请注意,这也会影响第一行输入。如果您将form-inline 移动到另一个位置,您可以选择防止这种情况发生。

    【讨论】:

    • 为什么
      标签在我将它放在最后一个输入下方时更喜欢您的建议后也没有对齐?
    • 我不确定你的意思,你可能在问一个新问题?您发布的问题中没有hr。无论如何,这可能是出于类似的原因:hr 往往是一个 block 元素,因此占据了整个宽度。如果你设置一个明确的宽度,实际的可视化线可能会变成100px,但hr元素包围的块是全宽的,因此没有“文本对齐”的概念。
    • 如前所述,hr 在视觉上将是 100 像素宽,但会继续占据其包含元素的整个宽度,因为剩余的宽度会自动计算为 margin。使用 Chrome 或 FF 中的开发人员工具检查hr,当宽度小于包含元素时,您将看到设置特定宽度时添加的边距。
    【解决方案2】:

    这是因为您的输入通过 .form-control 类将 display 属性设置为 block。块级元素将占据其包含元素的整个宽度。即使设置了宽度,剩余空间也会被margin占用。

    要使输入对齐方式,您希望将 display 值更改为 inline-block 或将类 .pull-right 添加到输入中。

    .form-control {
        display: inline-block;
    }
    
    <input type="text" class="form-control input-sm pull-right">
    

    【讨论】:

      【解决方案3】:

      Twitter Bootstrap 专门将width: 100% 属性设置为.form-control 以使生活更轻松并使用Grid system 处理此类事情

      您可以使用以下解决方案

      HTML 标记

      <div class="row">
        <div class="col-md-9 is-centered">
          <div class="well well-form">
           <!-- Two inline input fields per row -->
           <div class="row">
            <div class="col-sm-6 form-group">
             <input class="form-control input-sm" type="text" name="input1" placeholder="Input 1">
            </div>
            <div class="col-sm-6 form-group">
             <input class="form-control input-sm" type="text" name="input2" placeholder="Input 2">
            </div>
           </div>
           <div class="row has-border">
            <div class="col-sm-12">
             <!-- Single input fields per row (pushed to the right) -->
             <div class="row">
             <div class="col-sm-6 pull-right">
              <input class="form-control input-sm" type="text" name="input3" placeholder="align to right">
             </div>
            </div>
            </div>
          </div>
         </div>
        </div>
      </div>
      

      助手(可选)

      SCSS

      [class*="col-"] {
       &.is-centered {
          float: none;
          margin-right: auto;
          margin-left: auto;
        } 
      }
      
      .well-form {
        > .row {
          padding-top: 8px;
          border-top: 1px solid #ddd;
        }
        .form-group {
          margin-bottom: 8px;
        }
      }
      

      CSS

      [class*="col-"].is-centered {
        float: none;
        margin-right: auto;
        margin-left: auto;
      }
      
      .well-form > .row {
        padding-top: 8px;
        border-top: 1px solid #ddd;
      }
      .well-form .form-group {
        margin-bottom: 8px;
      }
      

      DEMO

      需要注意的几点:

      • &lt;table&gt; 在这种情况下不是一个好方法
      • style=" 内联到 HTML 标记一点都不好(即使它是一个演示)

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-29
        • 1970-01-01
        • 2022-01-19
        • 2020-09-19
        • 2015-05-17
        • 2014-05-21
        • 1970-01-01
        相关资源
        最近更新 更多