如何避免弹性物品在折叠时具有相同的高度

时间:2019-10-25 10:34:51

标签: javascript html css bootstrap-4

有一些来自数组的元素,然后我需要将它们放置在具有diplay flex的列表中,我的问题是我添加了一个按钮,该按钮应该折叠所选的元素,而其他按钮也要修改其高度...

尝试了不同的显示方式并在li元素上内联放置,但没有任何效果。

有什么方法可以在列表中内联并且高度独立?

.dragscroll {
  display: flex;
  overflow-x: auto;
}

.rectangle-holder {
  border: 1px solid black;
}

ul {
  list-style-type: none;
}
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
  <div class="container-fluid p-0">
    <div class="row">
      <div class="col-12">
        <ul class="dragscroll">
          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment1" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment1">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>

          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment2" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment2">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>

          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment3" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment3">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>
          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment4" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment4">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>

          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment5" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment5">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>

          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment6" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment6">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>

          <li class="rectangle-holder">
            <div class="row">
              <div class="col-12">
                <p>Hello</p>
              </div>
              <div class="col-12">
                <button class="btn btn-primary" data-target="#comment7" data-toggle="collapse" type="button">Collapse</button>
              </div>
            </div>
            <div class="row">
              <div class="col-12">
                <div class="collapse" id="comment7">
                  <p>Collapsable!</p>
                </div>
              </div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</body>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

</html>

1 个答案:

答案 0 :(得分:2)

您需要添加CSS属性 align-items:flex-start; 。 它定义了如何在当前行上沿交叉轴布局弹性项目的默认行为。并且 flex-start 设置了项目的交叉起始边沿,将其放置在交叉起始线上。

.dragscroll {
    display: flex;
    overflow-x: auto;
    align-items: flex-start;
}