【问题标题】:Force column to stay on right (float-right)强制列保持在右侧(向右浮动)
【发布时间】:2020-11-15 09:55:17
【问题描述】:

我在一行中有三列。每列都是 lg-6,这使得最后一列跨度低于其他两列。使用 float-right 将此列放置在右侧。但是,每当最后一列(第 2 列)上方的列比第一列获得更多内容时,最后一列(第 3 列)就会向左移动(如下图所示)。有什么办法可以防止列像这样移动吗?强制它保持正确。

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>

<div class="row d-md-block mt-4 mx-n3 px-4">
        <div class="card col-lg-6 col-sm-12 mx-n1 float-left">
            <div class="card-header screensCardHeader">
                <h4 class="m-0 font-weight-bold text-header">Column 1</h4>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="table_0_view" class="table" width="100%" cellspacing="0">
                        <thead class="screensThead">
                            <tr>
                                <th>#</th>
                                <th>Namn</th>
                            </tr>
                        </thead>
                        <tbody id="tbody_0_view" class="screenstable screenstableque">
                            
                            <tr id="2" sport="0" class="screenstablerow">
                                <td class="viewtd row-id-0">1</td>
                                <td>Person 1</td>
                            </tr>
                            
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        
        <div class="card col-lg-6 col-sm-12 mx-n1 float-right order-1">
            <div class="card-header screensCardHeader">
                <h4 class="m-0 font-weight-bold text-header">Column 2</h4>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="table_1_view" class="table" width="100%" cellspacing="0">
                        <thead class="screensThead">
                            <tr>
                                <th>#</th>
                                <th>Namn</th>
                            </tr>
                        </thead>
                        <tbody id="tbody_1_view" class="screenstable screenstableque">
                            
                            <tr id="3" sport="1" class="screenstablerow">
                                <td class="viewtd row-id-1">1</td>
                                <td>Person 2</td>
                            </tr>
                            <tr id="4" sport="1" class="screenstablerow">
                                <td class="viewtd row-id-1">1</td>
                                <td>Person 3</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        
        <div class="card col-lg-6 col-sm-12 mx-n1 float-right justify-content-end">
            <div class="card-header screensCardHeader">
                <h4 class="m-0 font-weight-bold text-header">Column 3</h4>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table id="table_2_view" class="table" width="100%" cellspacing="0">
                        <thead class="screensThead">
                            <tr>
                                <th>#</th>
                                <th>Namn</th>
                            </tr>
                        </thead>
                        <tbody id="tbody_2_view" class="screenstable screenstableque">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

【问题讨论】:

    标签: html css twitter-bootstrap bootstrap-4


    【解决方案1】:

    不要使用 float 属性,而是在父级中使用 d-flex

    并在第 3 列中使用 ml-auto


    更新 - 关于 OP cmets

    但这意味着第 1 列将跟随第 2 列的高度,对吗?那么,在第 2 列中添加内容时,有什么方法可以隐藏第 1 列中出现的空格?

    答案:在row div 中使用align-items-start

    此外,左右列之间的间距似乎消失了。我之前在 mx-n1 中使用了 1 的负边距。使用 d-flex 时有什么办法可以恢复?根据上次引导的文档使用,负边距:getbootstrap.com/docs/4.5/utilities/spacing

    答案:使用col-*-* 内部的card 实用程序类,而不是与它一起使用

    注意widthcellspacing 属性已弃用

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container">
      <div class="row d-flex mt-4 px-4 align-items-start">
        <div class="col-lg-6 col-sm-12 mb-4">
          <div class="card">
            <div class="card-header screensCardHeader">
              <h4 class="m-0 font-weight-bold text-header">Column 1</h4>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="table_0_view" class="table">
                  <thead class="screensThead">
                    <tr>
                      <th>#</th>
                      <th>Namn</th>
                    </tr>
                  </thead>
                  <tbody id="tbody_0_view" class="screenstable screenstableque">
    
                    <tr id="2" sport="0" class="screenstablerow">
                      <td class="viewtd row-id-0">1</td>
                      <td>Person 1</td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
        <div class="col-lg-6 col-sm-12 mb-4">
          <div class="card">
            <div class="card-header screensCardHeader">
              <h4 class="m-0 font-weight-bold text-header">Column 2</h4>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="table_1_view" class="table">
                  <thead class="screensThead">
                    <tr>
                      <th>#</th>
                      <th>Namn</th>
                    </tr>
                  </thead>
                  <tbody id="tbody_1_view" class="screenstable screenstableque">
    
                    <tr id="3" sport="1" class="screenstablerow">
                      <td class="viewtd row-id-1">1</td>
                      <td>Person 2</td>
                    </tr>
                    <tr id="4" sport="1" class="screenstablerow">
                      <td class="viewtd row-id-1">1</td>
                      <td>Person 3</td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
    
    
        <div class="col-lg-6 col-sm-12 ml-auto mb-4">
          <div class="card">
            <div class="card-header screensCardHeader">
              <h4 class="m-0 font-weight-bold text-header">Column 3</h4>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <table id="table_2_view" class="table">
                  <thead class="screensThead">
                    <tr>
                      <th>#</th>
                      <th>Namn</th>
                    </tr>
                  </thead>
                  <tbody id="tbody_2_view" class="screenstable screenstableque">
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 但这意味着第 1 列将跟随第 2 列的高度,对吧?那么,在第 2 列中添加内容时,有什么方法可以隐藏第 1 列中出现的空格?
    • 另外,左右列之间的间距似乎消失了。我之前在 mx-n1 中使用了 1 的负边距。使用 d-flex 时有什么办法可以恢复?根据上次引导的文档使用,负边距:getbootstrap.com/docs/4.5/utilities/spacing
    • 非常感谢!更新答案解决了所有问题。但最终它引入了另一个问题。请在我最初的帖子中查看更新的照片。由于当向第 1 列添加某些内容时,第 3 列跟随第 1 列,如果第 1 列被填充,则第 3 列将在溢出下方消失。有什么方法可以使第 3 列在第 2 列下方保持静止?还是在使用 d-flex 时会出现这种情况?
    • 你在找masonry layout
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多