简体   繁体   English

Bootstrap 5 - 使用 flex-grow-1 class 使 div 可滚动

[英]Bootstrap 5 - Make div with flex-grow-1 class scrollable

I'm trying to make the indicated div below scrollable, but it always seems to extend the document size beyond screen space instead of creating a scrollbar.我正在尝试使指示的 div 下面可滚动,但它似乎总是将文档大小扩展到屏幕空间之外,而不是创建滚动条。 One solution is to set a fixed pixel height for the div, but I want to avoid this to ensure scalability between devices.一种解决方案是为 div 设置固定的像素高度,但我想避免这种情况以确保设备之间的可伸缩性。 Any ideas?有任何想法吗?

<div class="row no-gutters w-100 flex-grow-1">
  <div class="col-8">
    <div id="playarea" class="container-fluid border-top border-end h-100 px-0">
      <!-- Cards go here -->
    </div>
  </div>
  <div class="col-4">
    <div class="container-fluid border rounded-top w-100 h-100 d-flex flex-column">
      <div class="container my-3 d-flex mb-0">
        <h5 class="flex-grow-1">Side Content</h5>
      </div>
  
      <hr class="border mt-2 mb-3">
  
      <div class="container mb-3 d-flex">
        <!-- Non-scrollable search area -->
      </div>
  
      <div class="container flex-grow-1 overflow-auto">
        <!-- Scrollable content goes here -->
      </div>
  
      <div class="card mt-2 bg-light mb-3" id="info-panel">
        <div class="card-body">
          <!-- Non-scrollable info box -->
        </div>
      </div>
    </div>
  </div>
</div>

Many thanks in advance提前谢谢了

Your question is a bit confusing because your .row has .flex-grow-1 which one would only add if you want .row to grow in available height of its parent, not in width.您的问题有点令人困惑,因为您的.row.flex-grow-1 ,只有当您希望.row在其父级的可用高度而不是宽度上增长时,才会添加哪个。 This would mean .row should always have a height and my solution can be simplified.这意味着.row应该总是有一个高度,我的解决方案可以简化。 So let me know if that's the case, because my answer assumes .row has no height.所以让我知道是否是这种情况,因为我的回答假设.row没有高度。

My solution that requires no fixed height uses a container inside col-4 with absolute positioning:我不需要固定高度的解决方案使用col-4内的容器,具有绝对定位:

  #overflow-container {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    padding: inherit;
  }

This container will take up all available space inside col-4 and will grow or shrink with it.该容器将占用col-4内的所有可用空间,并随之增大或缩小。 Now the element with overflow: scroll will work as you want.现在带有overflow: scroll的元素将按您的意愿工作。

The only drawback here is that if col-8 (or row ) has no content - and thus no height - the #overflow-container will also have no height and any content inside the .overflow-auto element will not be visible.这里唯一的缺点是,如果col-8 (或row )没有内容 - 因此没有高度 - #overflow-container也将没有高度,并且.overflow-auto元素内的任何内容都将不可见。 So if col-8 can be empty, a min-height on col-4 s parent is needed.因此,如果col-8可以为空,则需要col-4的父min-height

The line padding: inherit is there to give the column its padding back;padding: inherit是为了让列返回填充; Bootstrap uses the selector row > * to give columns their padding. Bootstrap 使用选择器row > *为列提供填充。

I removed all unnecessary classes for this snippet to work for simplicity, but I added the sm breakpoint on the columns so they stack on mobile screen sizes.为了简单起见,我删除了此代码段所有不必要的类,但我在列上添加了sm断点,以便它们在移动屏幕尺寸上堆叠。 If you don't want this, you can simply remove the -sm part on the columns and the @media in the CSS. It's only there to show how it can be done.如果你不想要这个,你可以简单地删除列上的-sm部分和 CSS 中的@media 。它只是为了展示如何完成它。

 .col-sm-8 { background: lightyellow; }.col-sm-4 { background: lightgreen; } @media (min-width: 576px) { #overflow-container { position: absolute; top: 0; bottom: 0; left: 0; right: 0; padding: inherit; } }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script> <div class="container-fluid"> <div class="row"> <div class="col-sm-8"> <p>This column should have enough content...</p> <br><br><br><br><br> <p>...So `Overflowing content` will have a height.</p> </div> <div class="col-sm-4 position-relative"> <div class="d-flex flex-column" id="overflow-container"> <div class=""> <h5>Aside Content</h5> </div> <div class=""> <p>Some content here.</p> </div> <div class="overflow-auto"> <p>Overflowing content.</p> <br><br><br><br><br><br><br><br><br> <p>Unless col-8 is long enough for me to fit.</p> </div> <div class=""> <p>Some content here.</p> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM