我有一个字符串,我在一个跨度内显示。
<div>
<span>The name of this person is john He is a cricket player </span>
</div>在这里,它以文本的宽度为单位。现在,我不想给这个元素任何特定的宽度。所以我试着
.parent{
display: inline-block // so that the div should take only that width as of the text
}
.childspan {
//word-wrap: break-all;
}那么,如何在特定单词is之后使用CSS将跨度分成两行呢?
有办法这样做吗?而不给出固定的宽度?
发布于 2022-02-23 12:38:03
使用js最简单的方法是
const child = document.querySelector('.parent .child');
child.innerHTML = child.innerHTML.replace("is", "is<br/>");它可以在任何元素中用于单词后面的断线,如果您需要在每一个is之后中断,那么将它与RegExp一起使用。
child.innerHTML = child.innerHTML.replace(/is/g, "is<br/>");发布于 2022-02-23 12:36:33
您可以使用两个span元素并在两者之间添加一个<br>:
<div>
<span>The name of this person is john He is</span>
<br>
<span>a cricket player </span>
</div>
或者使用::after伪类:
<div>
<span id="line1">The name of this person is john He is</span>
<span>a cricket player </span>
</div>
<style>
#line1::after{
content: "\a";
white-space: pre;
}
</style>
发布于 2022-02-23 13:48:02
你可以使用<br>标签,在这里你想打破一条线。
https://stackoverflow.com/questions/71236965
复制相似问题