【问题标题】:Bootstrap 4 align elements right inside a col divBootstrap 4在col div内对齐元素
【发布时间】:2017-08-26 01:19:09
【问题描述】:

我的问题很简单,但我没有找到合适的方法(我的意思是不使用相对位置容器内的子元素的绝对定位)在 Bootstrap 4 中实现这一点。

我有一排有一个 col-8 和一个 col-4。我在 col-4 中的内容必须右对齐,以便其内容位于右边框。

<h1 class="col-md-8">Applications portfolio</h1>

  <div class="btn-group col-md-4" role="group">
    <p class="float-right">
      <a class="btn btn-secondary btn-md" href="#">
        <i class="fa fa-plus" aria-hidden="true"></i> Creation</a>
      <a class="btn btn-md btn-secondary" href="#">
        <i class="fa fa-flag" aria-hidden="true"></i> Report</a>
    </p>
  </div>

这是一个代码笔:

https://codepen.io/anon/pen/QpzVgJ

我希望我的两个按钮在 col-4 内右对齐。

如何在 Bootstrap 4 中正确对齐 col 中的正确元素?

【问题讨论】:

    标签: css twitter-bootstrap flexbox bootstrap-4 twitter-bootstrap-4


    【解决方案1】:

    使用 ml-auto 将按钮推到右侧...

    https://codepen.io/anon/pen/evbLQN

    <div class="btn-group col-md-4" role="group">
        <p class="ml-auto">
          <a class="btn btn-secondary btn-md" href="#">
            <i class="fa fa-plus" aria-hidden="true"></i> Creation</a>
          <a class="btn btn-md btn-secondary" href="#">
            <i class="fa fa-flag" aria-hidden="true"></i> Report</a>
        </p>
    </div>
    

    另一种选择是从col-md-4 中删除btn-group,然后float-right 将按预期工作。 pull-right 类在 Bootstrap 4 中被 float-right 取代。

    <section class="row">
      <h1 class="col-md-8">Applications portfolio</h1>
    
      <div class="col-md-4" role="group">
        <p class="float-right">
          <a class="btn btn-secondary btn-md" href="#">
            <i class="fa fa-plus" aria-hidden="true"></i> Creation</a>
          <a class="btn btn-md btn-secondary" href="#">
            <i class="fa fa-flag" aria-hidden="true"></i> Report</a>
        </p>
      </div>
    </section>
    

    PS - 为了防止您的 Codepen 中出现水平滚动条,请确保将 .row 放在 container-fluid 内。此外,一般 col-* 用于包含内容,不应应用于其他组件/元素。因此,例如,如果您想使用 btn-group..

    <div class="container-fluid">
        <section class="row">
            <div class="col-md-8">
                <h1>Applications portfolio</h1>
            </div>
            <div class="col-md-4">
                <div class="btn-group float-right mt-2" role="group">
                    <a class="btn btn-secondary btn-md" href="#">
                        <i class="fa fa-plus" aria-hidden="true"></i> Creation</a>
                    <a class="btn btn-md btn-secondary" href="#">
                        <i class="fa fa-flag" aria-hidden="true"></i> Report</a>
                </div>
            </div>
        </section>
    </div>
    

    http://www.codeply.com/go/8OYDK5D8Db


    相关:Right align element in div class with bootstrap 4

    【讨论】:

      【解决方案2】:

      该 md-4 中的 btn-groupclass 使该 DIV 成为 flex 容器,因此用于对齐的常规 text-right 类将不起作用。相反,在其样式属性中添加justify-content: flex-end;

      <div class="btn-group col-md-4 text-right" role="group" style="justify-content: flex-end;">
      

      https://codepen.io/anon/pen/RpEYEM

      (注意:我删除了那个 DIV 中的 p 标签)

      【讨论】:

        【解决方案3】:

        提供的标记存在一些问题,使布局看起来很奇怪。 @ZimSystem 提到了.container-fluid,但你也应该在.row 中始终有一个div.col,这样你就可以在边缘获得相同的填充。

        您不需要在按钮周围添加&lt;p&gt;。要获得对齐,只需将.ml-auto 添加到第一个按钮,如下所示:

        <div class="container-fluid">
        <section class="row mb-2 mt-2">
          <h1 class="col-md-8">Applications portfolio</h1>
        
          <div class="btn-group col-md-4" role="group">
              <a class="btn btn-secondary btn-md ml-auto" href="#">
                <i class="fa fa-plus" aria-hidden="true"></i> Creation</a>
              <a class="btn btn-md btn-secondary" href="#">
                <i class="fa fa-flag" aria-hidden="true"></i> Report</a>
          </div>
        </section>
        
        <section class="row" style="background-color: grey; height: 50px; overflow:hidden;">
          <div class="col">
            <p>Just a simple reference block to see the 100% width</p>
          </div>
        </section>
        </div>
        

        【讨论】:

          猜你喜欢
          • 2019-12-09
          • 1970-01-01
          • 2021-02-01
          • 2020-02-26
          • 2017-10-11
          • 2018-05-22
          • 2017-07-15
          • 1970-01-01
          • 2018-07-20
          相关资源
          最近更新 更多