html - 在 flex 行中使用 `nowrap` 时,避免将最后一个元素推出屏幕

标签 html css flexbox nowrap

我需要在一行中呈现一段很长的文本。和行尾的图像(屏幕的紧端)。

给定一个很长的文本,我必须简单地截断它。 文本现在应该放在第二行,因为它太长了。

请看我的代码。 如何避免将漂亮的表情符号推出屏幕?

请注意右侧的元素可能是动态的。所以给出一个固定的宽度是不够的。

div {
display:flex;
justify-content: space-between;
}

span {
white-space:nowrap;
overflow:hidden;
}
<div>
<span>hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello </span>
<span>🛑</span>
</div>

最佳答案

您基本上只需要记住默认情况下 flex 元素可以收缩(flex-shrink: 1)。

一旦你禁用了flex-shrink,你就应该准备好了。

div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;    /* optional */
}

span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;  /* optional */
}

span:last-child {
  flex-shrink: 0;
}
<div>
  <span>hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello </span>
  <span>🛑</span>
</div>

关于html - 在 flex 行中使用 `nowrap` 时,避免将最后一个元素推出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49056401/

相关文章:

css - 在可选组件中重用 LESS mixin 类

css - Firefox 和 IE 的 Flex 问题

javascript - 使用 AJAX 回显表单提交到 div

javascript - 使用 AJAX 附加来自另一个页面的文本或值

javascript - 动态生成的 html 按钮在页面加载时不起作用

html - 媒体调整页面大小时 IE 重叠 flexbox

html - 带有文本溢出的 CSS Flexbox 边框半径 : Ellipsis

html - 页面和 CSS 中的空白间隙不包括 element.style

jquery - Twitter Bootstrap v2.3.2 响应式菜单在 IE 或 Safari 或移动设备中不起作用

css - 模糊所有,但 ng-repeat 中的一个 div