具有块内容的Flexbox项目会溢出Flexbox容器

时间:2016-06-01 18:10:02

标签: html css overflow flexbox

问题

像按钮这样的阻止内容会导致我的弹性项目扩展并溢出Flexbox容器。

预期行为

在此示例中,按钮应保持并排,并使用省略号隐藏按钮文本溢出。弹性项目宽度应基于兄弟姐妹而不是内容,并且当容器更改宽度并添加或移除弹性项目时,保持在容器内保持响应。另一个警告是我不能使用溢出:隐藏在flex项目或按钮父div为我的特定场景。

这是fiddler

Block content causes flex items to overflow container

示例Html

<div class="container" style="width: 400px;">

  <div class="child one">
    Child One
    <br>Lorem ipsum
    <br>dolor sit amet
  </div>

  <div class="child two"><div><button class="text">Child Two with a loooooooooooooooooong naaaaaaaaaaaaaaaaaaaaaaaaaaaaaame</button><button>button two</button></div></div>

<div class="child three">Some Text</div></div>

CSS示例

div {
  border: 3px solid;
}

.container {
  padding: 10px;
  background-color: yellow;
  display: -webkit-flex;
  display: flex;
}

.child {
  flex: 1 1 auto;
  padding: 10px;
  margin: 10px;
  background-color: #eee;
}

.child.one {
  color: green;
}

.child.two {
  flex: 6 1 auto;
  color: purple;
}

.child.two button {
  display: inline-block;
}

.child.three {
  color: blue;
}

.text {
  text-overflow: ellipsis;
  overflow: hidden;
}

编辑:现在,我没有尝试显示多个按钮,而是将需求更改为仅一个按钮。如果有人能用多个按钮解决它,我仍然会感兴趣。

1 个答案:

答案 0 :(得分:2)

我已经为那些感兴趣的人提出了答案。但是我不得不修改我的初始要求。我没有尝试让两个按钮并排显示,而是将其缩小为一个按钮。鉴于此,这是答案......这似乎适用于各种浏览器。

fiddle example of resolution

example image

添加以下样式:

.child {
    width: 1%;
}

.child.button {
    width: 100%;
}

我会对有人解释宽度%的问题感兴趣吗?似乎它的行为更像是最小宽度可能有效。

编辑:感谢Michael_B链接到explanation of width relationship in flex items

相关问题