【问题标题】:white-space css property is creating issues with flex [duplicate]空白 css 属性正在创建 flex 问题 [重复]
【发布时间】:2018-04-19 14:54:05
【问题描述】:

当有弹性容器时,我遇到了white-space:nowrap 的问题。

有一个 flex containerflex-direction:row;,其中有两个 div sidebar 使用 flex:0 0 70px;main-content 设置一定的宽度。

main-contentflex-direction:column 两个主要div 中,headercontent

现在在content 中,有三个blade 具有flex-grow:1

每个blade 都有long-content 设置white-space:nowrap。我希望所有三个刀片都在屏幕上可见,即在main-content 的空间中自动调整。但是由于我在container 上设置了overflow-x:hidden;,因此不会出现水平滚动,因此右侧屏幕会被剪切,该内容不可见。

我希望它在屏幕上调整,每个刀片的空间相等。

代码如下:

function expand() {
  var sidebar = document.querySelector(".sidebar");
  sidebar.classList.add("sidebar--expanded");

}
button {
  max-width: 60px;
  max-height: 30px;
}

.container {
  display: flex;
  overflow-x: hidden;
}

.sidebar {
  background: red;
  height: 100vh;
  display: flex;
  flex: 0 0 70px;
}

.sidebar--expanded {
  flex: 0 0 100px;
}

.main-content {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}

.header {
  background: blue;
  flex-grow: 1;
  display: flex;
  color: white;
  justify-content: flex-end;
  flex: 0 0 30px;
}

.content {
  display: flex;
  color: white;
  background: teal;
  flex-grow: 1;
  width: 100%;
}

.long-content {
  overflow-x: auto;
  white-space: nowrap;
}

.blade {
  border: 1px solid red;
  padding: 5px;
  flex-grow: 1;
}
<div class="container">
  <div class="sidebar">
    <button onclick="expand()">Sidebar</button>
  </div>
  <div class="main-content">
    <div class="header">
      header
    </div>
    <div class="content">
      <div class="blade">
        <div class="long-content">
          Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add
          title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle
          public.Add title to make the fiddle public.
        </div>
      </div>
      <div class="blade">
        <div class="long-content">
          Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add
          title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle
          public.Add title to make the fiddle public.
        </div>
      </div>
      <div class="blade">
        <div class="long-content">
          Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add
          title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle public.Add title to make the fiddle
          public.Add title to make the fiddle public.
        </div>
      </div>
    </div>
  </div>
</div>

解释整个情况有点困难,所以这里是JSFiddle

我通过应用不同的 css 规则尝试了所有的 CSS 组合,但无法实现?请帮忙。

谢谢!!

其他信息 实际上,第二个弹性项目main-content 并没有考虑其兄弟sidebar 的存在,因此它将整个屏幕宽度视为唯一的项目。

【问题讨论】:

  • 为什么要投票?我已经添加了所有信息
  • 如果您对问题投反对票,则必须有规则,您应该为此添加理由
  • 有人建议这样做,但每次都被否决了...但是您的代表应该知道在问题中发布代码。
  • 嗯...你去。
  • min-width: 0 添加到.main-content.blade。解释在副本中。 jsfiddle.net/4k1qsm1y/58

标签: html css flexbox


【解决方案1】:

如果您希望它在屏幕上调整每个刀片的相等空间,那么为什么要放置 white-space: nowrap。
从长内容中删除white-space: nowrap,它将在您的容器内自动调整,占用相等的空间。

【讨论】:

  • 实际上在真正的 scnario 中会有表格,所以我不想包装内容.. 我添加了 overflow-x:auto 它应该可以正常工作
  • 好的,那很好......但我看不出它是如何正常工作的。当你问这个问题时,你已经使用了 overlfow-x: auto。
  • 是的.. 但是如果您将主要内容的宽度设置为小于侧边栏的宽度,它的工作原理就像小于 90% - 所以它可能是 80%,70% 然后它工作
猜你喜欢
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多