html - 如何使用 CSS 来换行而不是用空格换行?

标签 html css

使用以下标记,是否可以(以及如何)实现预览中所示的换行?

标记

<div class="filled-box">
  <h2>Hi there</h2>
  <p>I am just a text with some words, that want to fill the entire space</p>
</div>

CSS

.filled-box {
  width: 80px;
  height: 80px;
  position: absolute;
  overflow: hidden;
}

.filled-box h2,
.filled-box p {
  display: inline;
  overflow-wrap: break-word;
  font-size: 20px;
  margin: 0;
  padding: 0;
}

预览

Hi there I am
just a text w
ith some word
s, that want 
to fill the e
ntire space

如果有解决方案,当然可以使用 CSS3 语法。

最佳答案

word-break: break-all;可能就是您正在寻找的。细节决定成败。

您的初始标记有一个 h2 标记和一个 p 标记,但您的预览显示它们都是内联的。 h2p 标签不是内联元素。

因此,使它们 display: inline 并不是理想的做法,而是使用内联元素,例如 span 等。

这里需要注意的另一件事是 word-break: break-all; 不能在您的内联元素上正常工作。只需使用它们的用途标签,并根据我们的偏好调整值。

.filled-box {
  width: 85px;
  position: absolute;
}

.filled-box p span{
  word-break: break-all;
  font-size: 16px;
  margin: 0;
  padding: 0;
}
.filled-box p {
  word-break: break-all;
  font-size: 16px;
  margin: 0;
  padding: 0;
}
<div class="filled-box">
  
  <p><span>Hi there</span> I am just a text with some words, that want to fill the entire space</p>
</div>

关于html - 如何使用 CSS 来换行而不是用空格换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648164/

相关文章:

jquery - 使用幻灯片动画淡入幻灯片

javascript - 在每个页面上打印一个 HTML 标题

html - 缩小窗口时从垂直到水平分隔线的 css 响应

html - 仅使用 CSS 调整其中一个 Div 的高度

html - 页面中间的中心导航菜单

html - CSS 媒体设备

javascript - 如何使用 node.js 和 socket.io 通过 WebSockets 流式传输 MP3 数据?

css - 我们如何更改占位符的颜色?

jquery - 如何使鼠标指针仅悬停在数据表中的特定列上

javascript - vim,在 html 中缩进 css 和 js 的正确方法