【问题标题】:Bootstrap 5 float-end causing issue with flex elementsBootstrap 5 浮动端导致 flex 元素出现问题
【发布时间】:2022-07-16 03:31:01
【问题描述】:

当我添加类 float-end 时,它会将按钮向右推,但旁边的 div 标签位于它的前面。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="col-md-7 bg-white shadow-sm rounded-3 mx-3 ">
  <div>
    <a href="#" class="btn btn-sm btn-primary m-2 mr-0">
      Edit  <i class="bi bi-pencil"></i>
    </a>
  </div>
  <div class="d-flex p-2 justify-content-between border-bottom">
    <div class="fw-bold">Pan no</div>
    <div class="text-secondary">not added</div>
  </div>
  <div class="d-flex p-2 justify-content-between border-bottom">
    <div class="fw-bold">Birthdate</div>
    <div class="text-secondary">not added</div>
  </div>
</div>

【问题讨论】:

  • 在现代布局中通常要避免浮动。 Bootstrap 提供了实现相同结果所需的所有 flex 实用程序。
  • 请接受您的回答来解决这个帖子。

标签: css twitter-bootstrap bootstrap-5


【解决方案1】:

我通过将“clearfix”类添加到按钮的父 div 来修复它。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="col-md-7 bg-white shadow-sm rounded-3 mx-3 ">
  <div class="clearfix">
    <a href="#" class="btn btn-sm btn-primary m-2 mr-0">
      Edit  <i class="bi bi-pencil"></i>
    </a>
  </div>
  <div class="d-flex p-2 justify-content-between border-bottom">
    <div class="fw-bold">Pan no</div>
    <div class="text-secondary">not added</div>
  </div>
  <div class="d-flex p-2 justify-content-between border-bottom">
    <div class="fw-bold">Birthdate</div>
    <div class="text-secondary">not added</div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    这里,在概述部分 Float - Bootstrap v5.0

    请注意浮动实用程序对弹性项目没有影响。

    注意 float 会使其弹出到父元素的最左边或最右边的位置。所以您必须clearfix 添加到父元素。

    【讨论】:

      【解决方案3】:

      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
      
      <div class="col-md-7 bg-white shadow-sm rounded-3 mx-3 ">
         <div class="position-relative">
            <div class="position-absolute top-0 end-0">
                <a href="#" class="btn btn-sm btn-primary m-2 mr-0">
                   Edit 
                </a>
            </div>
        </div>
        <div class="d-flex p-2 justify-content-between border-bottom">
          <div class="fw-bold">Pan no</div>
          <div class="text-secondary">not added</div>
        </div>
        <div class="d-flex p-2 justify-content-between border-bottom">
          <div class="fw-bold">Birthdate</div>
          <div class="text-secondary">not added</div>
        </div>
      </div>
      <br>
      <br>
      <div class="col-md-7 bg-white shadow-sm rounded-3 mx-3 ">
        <div class="float-start col-10 p-2">
          <div class="d-flex p-2 justify-content-between border-bottom">
             <div class="fw-bold">Pan no</div>
             <div class="text-secondary">not added</div>
          </div>
          <div class="d-flex p-2 justify-content-between border-bottom">
            <div class="fw-bold">Birthdate</div>
            <div class="text-secondary">not added</div>
          </div>
        </div>
        <div class="float-end col-2 p-2">
            <a href="#" class="btn btn-sm btn-primary d-flex justify-content-center align-self-center">
               Edit 
            </a>
        </div>
        <div class="float-start col-12 p-2">
          <div class="d-flex p-2 justify-content-between border-bottom">
            <div class="fw-bold">Addres</div>
            <div class="text-secondary">not added</div>
          </div>
          <div class="d-flex p-2 justify-content-between border-bottom">
            <div class="fw-bold">Zip Code</div>
            <div class="text-secondary">not added</div>
          </div>
          <div class="d-flex p-2 justify-content-between border-bottom">
            <div class="fw-bold">City</div>
            <div class="text-secondary">not added</div>
          </div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2012-04-14
        • 2020-04-29
        • 1970-01-01
        • 1970-01-01
        • 2016-02-23
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多