简体   繁体   English

如何在父元素外显示下拉菜单并溢出:CSS 中的自动?

[英]How to show a dropdown menu outside a parent element with overflow: auto in CSS?

I have tried a lot of things and searched online but I cannot figure out the solution to this problem.我已经尝试了很多东西并在网上搜索,但我无法弄清楚这个问题的解决方案。

I have a div container which has a max-height, min-height and also overflow: auto.我有一个 div 容器,它有一个最大高度、最小高度和溢出:auto。 When the inner content is larger than the max-height, a scrollbar appears as expected.当内部内容大于 max-height 时,会按预期出现滚动条。 But, inside the content there is a dropdown, which when clicked, the menu expands, and instead of being displayed outside the parent container, it is like changing the inner content height.但是,在内容内部有一个下拉菜单,当点击它时,菜单会展开,而不是显示在父容器之外,这就像改变内部内容的高度一样。

The only solution I found online and made sense to me, is to wrap the container to div with relative positioning and make the dropdown absolute, but there is a big drawback now, the dropdown stays fixed on scroll, as it is absolute positioned relative to the wrapper and not the content.我在网上找到并且对我有意义的唯一解决方案是将容器包装到具有相对定位的 div 并使下拉列表成为绝对值,但是现在有一个很大的缺点,下拉列表保持固定在滚动上,因为它相对于包装器而不是内容。 Is there any common way to fix this or any other solution?有什么常见的方法来解决这个问题或任何其他解决方案吗?

I didn't post any code because I do not want the answer to rely on my code.我没有发布任何代码,因为我不希望答案依赖于我的代码。 I just want a minimal example if possible with these properties:如果可能的话,我只想要一个带有这些属性的最小示例:

  1. Container has a max-height容器有一个最大高度
  2. If content is larger than the container's max-height then the container should display a scrollbar.如果内容大于容器的最大高度,则容器应显示滚动条。
  3. The content has a dropdown which should scroll with every other element of the content.内容有一个下拉菜单,它应该与内容的所有其他元素一起滚动。
  4. The menu options of the dropdown element are escaping the container / are displayed outside the boundaries of the container.下拉元素的菜单选项是 escaping 容器 / 显示在容器边界之外。

To illustrate on my comments on the question, here's an MCVE:为了说明我对这个问题的评论,这里有一个 MCVE:

 .scroll-container { border: 3px dashed #eee; height: 400px; padding: 10px; overflow: auto; width: 400px; }.content { background-color: #f0f0f0; height: 600px; position: relative; }.dropdown { background-color: orange; position: absolute; height: 300px; width: 300px; left: 300px; }
 <div class="scroll-container"> <div class="content"> <div class="dropdown"></div> </div> </div>

As you can see, with absolute positioning based on the relative position of div.content the orange div.dropdown creates a horizontal overflow, which is what you don't want.如您所见,使用基于 div.content 的相对div.content的绝对定位,橙色div.dropdown会创建水平溢出,这是您不想要的。 To fix this scenario, you need to remove position: relative from div.content and use transform: translateX(300px);要解决此问题,您需要从div.content中删除position: relative并使用transform: translateX(300px); instead of left: 300px;而不是left: 300px; :

 .scroll-container { border: 3px dashed #eee; height: 400px; padding: 10px; overflow: auto; width: 400px; }.content { background-color: #f0f0f0; height: 600px; }.dropdown { background-color: orange; position: absolute; height: 300px; width: 300px; transform: translateX(300px); }
 <div class="scroll-container"> <div class="content"> <div class="dropdown"></div> </div> </div>

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

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