TailwindCSS – 如何使元素的下拉菜单不将其他元素推出侧边栏的边界?

huangapple go评论70阅读模式
英文:

TailwindCSS - How to make element's dropdown not push the other elements out of the boundary in the sidebar?

问题

我基本上有一个侧边栏,可以包含多个部分(蓝色),每个部分都可以打开/关闭,它可以包含多个项目(红色)。当打开时,我希望展开的元素不要将其他蓝色部分推到边框外,而是将它们紧靠边框,并在展开的部分上使用overflow-y-scroll。以下是它看起来的屏幕截图(1)和应该看起来的样子(2):

TailwindCSS – 如何使元素的下拉菜单不将其他元素推出侧边栏的边界?

TailwindCSS – 如何使元素的下拉菜单不将其他元素推出侧边栏的边界?

以下是使用的代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
    <div id="container" class="h-96 w-52 overflow-hidden bg-black">
      <div class="h-[100px] w-full border border-black bg-blue-500"></div>
      <div id="item-container" class="h-fit space-y-1 overflow-y-scroll">
        <div class="h-10 w-full bg-red-500"></div>
        <div class="h-10 w-full bg-red-500"></div>
        <div class="h-10 w-full bg-red-500"></div>
        <div class="h-10 w-full bg-red-500"></div>
        <div class="h-10 w-full bg-red-500"></div>
        <div class="h-10 w-full bg-red-500"></div>
      </div>
      <div class="h-20 w-full border border-black bg-blue-500"></div>
      <div class="h-20 w-full border border-black bg-blue-500"></div>
    </div>
<style>

    .h-96{
    height: 24rem;
    }
    .w-52{
    width: 13rem;
    }
    .overflow-hidden{
    overflow: hidden;
    }
    .bg-black{
    background-color: rgb(0 0 0);
    }
    .w-full{
    width:100%;
    }
    .border{
    border-width: 1px;
    }
    .border-black{
    border-color: rgb(0 0 0);
    }
    .bg-blue-500{
    background-color: rgb(59 130 246);
    }
    .h-fit{
    height: fit-content;
    }
    .space-y-1{
    margin-top: 0.25rem;
    }
    .overflow-y-scroll{overflow-y: scroll;}
    .h-10{height: 2.5rem;}
    .bg-red-500{background-color: rgb(239 68 68);}
    .h-20{height: 5rem;}
    .h-[100px]{
    height:100px;
    }

</style>

<!-- end snippet -->

非常感谢您提供的任何帮助!

英文:

I basically have a sidebar that could contain multiple sections(blue), where each section can be opened/closed and it can contain multiple items(red). When opened, I would like the expanded element not to push the other blue sections outside of the border, but to push them just against it and have the overflow-y-scroll on the expanded section . Here are the screenshots of what it looks like(1) and what it should look like(2):
TailwindCSS – 如何使元素的下拉菜单不将其他元素推出侧边栏的边界?

TailwindCSS – 如何使元素的下拉菜单不将其他元素推出侧边栏的边界?

Here is the code used:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div id=&quot;container&quot; class=&quot;h-96 w-52 overflow-hidden bg-black&quot;&gt;
&lt;div class=&quot;h-[100px] w-full border border-black bg-blue-500&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;item-container&quot; class=&quot;h-fit space-y-1 overflow-y-scroll&quot;&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;h-20 w-full border border-black bg-blue-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-20 w-full border border-black bg-blue-500&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<style>

.h-96{
height: 24rem;
}
.w-52{
width: 13rem;
}
.overflow-hidden{
overflow: hidden;
}
.bg-black{
background-color: rgb(0 0 0);
}
.w-full{
width:100%;
}
.border{
border-width: 1px;
}
.border-black{
border-color: rgb(0 0 0);
}
.bg-blue-500{
background-color: rgb(59 130 246);
}
.h-fit{
height: fit-content;
}
.space-y-1{
margin-top: 0.25rem;
}
.overflow-y-scroll{overflow-y: scroll;}
.h-10{height: 2.5rem;}
.bg-red-500{background-color: rgb(239 68 68);}
.h-20{height: 5rem;}
.h-\[100px\]{
height:100px;
}

</style>

<!-- end snippet -->

Thank you in advance for any help provided!

答案1

得分: 1

你可以使容器变灵活,并使蓝色项目不收缩。

  • 容器的相关添加类:flex flex-col
  • 对于蓝色项目:shrink-0
英文:

You can make the container flex and make blue items unshrinkable.

  • Relevant added classes for container: flex flex-col.
  • For blue items: shrink-0.
&lt;div id=&quot;container&quot; class=&quot;h-96 w-52 overflow-hidden bg-black flex flex-col&quot;&gt;
&lt;div class=&quot;h-[100px] w-full border border-black bg-blue-500 shrink-0&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;item-container&quot; class=&quot;space-y-1 overflow-y-scroll&quot;&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-10 w-full bg-red-500&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;h-20 w-full border border-black bg-blue-500 shrink-0&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;h-20 w-full border border-black bg-blue-500 shrink-0&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月8日 20:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定