简体   繁体   English

基于 (flex) 项目内容的 CSS 布局

[英]CSS layout based on (flex) items content

I've written some JavaScript to accomplish this, but it's performance is slowing down my component and its not clean so I'm struggling to find more efficient CSS solution which would optimize it.我已经写了一些 JavaScript 来实现这一点,但它的性能正在减慢我的组件并且它不干净,所以我正在努力寻找更有效的 CSS 解决方案来优化它。 I've searched the web with no effect.我搜索了 web 没有任何效果。

So basically we have this template (simplified):所以基本上我们有这个模板(简化):

<div class="container">
  <div class="box"></div>
  <div class="container">
      <div class="box"></div>
      <div class="box"></div>
  </div>
</div>

All 3 .box 's are taking 1/3 (ie calc(100% / 3) ) and rendered in a row.所有 3 个.box都占用 1/3(即calc(100% / 3) )并连续渲染。

If at least one of that box 's content overflowing it's dedicated space - all 3 suppose to render in column (ie flex-direction: column ).如果该box的至少一个内容溢出它的专用空间 - 所有 3 个都假设在列中呈现(即flex-direction: column )。 Is there any way to achieve this using flexboxes and no JavaScript?有没有什么方法可以使用flexboxes而没有 JavaScript 来实现这一点?

Note: this chunk is rendered inside a responsive component(dropdown), that is 100% of it's grid cell.注意:这个块在一个响应式组件(下拉)内呈现,即 100% 的网格单元。 And it may take 100% of the screen as well as 10%.它可能会占用 100% 的屏幕以及 10% 的屏幕。 So I cant use any media-queries here.所以我不能在这里使用任何媒体查询。

if you want wrap last item, you can use flex-wrap:wrap;如果要包装最后一项,可以使用flex-wrap:wrap; in flexbox container and if flex items don't have specific width you can use min-width.在 flexbox 容器中,如果弹性项目没有特定宽度,您可以使用最小宽度。

if you want change direction of flex box in one breakpoint you can use @media like this:如果你想在一个断点改变弹性盒子的方向,你可以像这样使用@media

@media screen and (max-width:680px){
    .flexContainer{
        flex-direction:column;
    }
}

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

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