css - 显示内联元素并自动换行

标签 css

我正在尝试将三个 div 内联排列在一起。最后一个 div 的内容中可能有一个很长的第一个单词,当它出现时,它会换行到下一行。我希望它保留在第一行并换行。

我有以下内容:

#container {
  width: 300px;
  display: inline-block;
}

#text1 {
  outline: 1px solid red;
  display: inline;
  margin-right: 5px;
}

#text2 {
  outline: 1px solid red;
  display: inline;
  margin-right: 5px;
}

#title-container {
  outline: 1px solid red;
  display: inline;
  overflow: hidden;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
  -ms-text-overflow: ellipsis;
  white-space: nowrap;
}

#title {
  white-space: pre-wrap;
  word-wrap: break-word;
  -ms-word-wrap: break-word;
  -ms-word-break: break-word;
}
<div id="container">
  <div id="text1">
    Text1
  </div>
  <div id="text2">
    12345
  </div>
  <div id="title-container">
    <span id="title">verryyyyyyyyyyyyyyyyyyyyyyyyyyyrrrrrrrrrrryyyyyyyyyyyyyy long title</span>
  </div>
</div>

这是一个演示问题的 fiddle 。

https://jsfiddle.net/wzm3h6u6/

这里正确的修复是什么?

最佳答案

只需将 word-break: break-all; css 添加到 #title-container

#container {
  width: 300px;
  display: inline-block;
}

#text1 {
  outline: 1px solid red;
  display: inline;
  margin-right: 5px;
}

#text2 {
  outline: 1px solid red;
  display: inline;
  margin-right: 5px;
}

#title-container {
  outline: 1px solid red;
  display: inline;
  overflow: hidden;
  text-overflow: ellipsis;
  -o-text-overflow: ellipsis;
  -ms-text-overflow: ellipsis;
  white-space: nowrap;
  word-break: break-all;
}

#title {
  white-space: pre-wrap;
  word-wrap: break-word;
  -ms-word-wrap: break-word;
  -ms-word-break: break-word;
}
<div id="container">
  <div id="text1">
    Text1
  </div>
  <div id="text2">
    12345
  </div>
  <div id="title-container">
    <span id="title">verryyyyyyyyyyyyyyyyyyyyyyyyyyyrrrrrrrrrrryyyyyyyyyyyyyy long title</span>
  </div>
</div>

关于css - 显示内联元素并自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458910/

相关文章:

css - 如何为容器中的所有元素提供相同的背景颜色?

javascript - 如何在点击按钮时放大图像

javascript - 将列样式应用于表体元素

javascript - 不影响嵌套 Accordion jquery 的自定义样式 - 如何修复

javascript - Bootstrap Carousel 无法在 asp.net Webform 中工作

javascript - 停止一个 div 的背景颜色覆盖另一个 div 的内容,旋转并因此重叠,div

Javafx tableview changelistener 经常变化

html - 使用 materializecss 时导航栏无法正确调整大小

css - 我需要剪辑或 overflow hidden 父元素的内容,但仅在一侧

jquery - 同位素响应在真正的大屏幕上表现异常