如果元素不适合 flex 布局,如何隐藏它?

Ego*_*rov 4 html javascript css flexbox

我正在尝试制作一个包含 3 个项目的水平 flexbox div。每个都有一个最小宽度,我想隐藏它们,因为屏幕太小而无法容纳它们。如果我知道所有 3 个 div 的宽度,我只会使用媒体查询在正确的断点处隐藏 div。问题是 div1 是具有动态宽度的图像(在示例中以随机宽度重新创建)

如果元素不适合 flexbox 容器,有没有办法隐藏它?

这有点难以解释,但希望多次运行此代码片段将显示我所追求的内容:

let width = (Math.random()*600)
document.getElementById('one').style.minWidth = width + 'px'
Run Code Online (Sandbox Code Playgroud)
.root {
  display: flex;
  justify-content: space-between;
}

.one {
  background-color: red;
}

.two {
  min-width: 200px;
  background-color: green;
}

.three {
  min-width: 300px;
  background-color: yellow;
  flex: 1
}
Run Code Online (Sandbox Code Playgroud)
<div class="root">
  <div id="one" class="one">
    unknown width
  </div>
  <div class="two">
    hide me if im off the screen
  </div>
  <div class="three">
    hide me if im off the screen
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

小智 6

由于您有一个固定高度的图像,您可以设置root为最大高度并使用flex-wrap: wrap.

let width = (Math.random()*600)
document.getElementById('one').style.minWidth = width + 'px'
Run Code Online (Sandbox Code Playgroud)
.root {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  overflow:hidden;
  max-height:50px;
  overflow-y:hidden;
}

.one {
  background-color: red;
  height: 50px;
}

.two {
  min-width: 200px;
  background-color: green;
}

.three {
  min-width: 300px;
  background-color: yellow;
  flex: 1
}
Run Code Online (Sandbox Code Playgroud)
<div class="root">
  <div id="one" class="one">
    unknown width
  </div>
  <div class="two">
    hide me if im off the screen
  </div>
  <div class="three">
    hide me if im off the screen
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/4y39h8jn/1/