有没有一种仅使用CSS的方法来在特定的非空白字符上分割一个单词?

huangapple go评论61阅读模式
英文:

Is there a CSS only way of breaking a word on a specific non whitespace character?

问题

I want to split a word (e.g. THIS_PHRASE_HERE) on underscores if it overflows the div it's inside, which is a fixed width. I know I can do word-break: break-all to split the word on any character, but that looks a little messy, especially if there's an orphan of 1 or 2 characters at the end. I'm sure I could do this with JS, but was wondering if there's a way of doing it in CSS only, something like word-break: "_".

我想要在一个固定宽度的div内,如果单词溢出,就以下划线分割单词(例如THIS_PHRASE_HERE)。我知道可以使用word-break: break-all来在任何字符上分割单词,但那看起来有点凌乱,尤其是如果末尾只有1或2个字符时。我确定可以使用JavaScript来实现这个,但想知道是否有一种纯CSS的方法,类似word-break: "_"

英文:

I want to split a word (e.g. THIS_PHRASE_HERE) on underscores if it overflows the div it's inside, which is a fixed width. I know I can do word-break: break-all to split the word on any character, but that looks a little messy, especially if there's an orphan of 1 or 2 characters at the end. I'm sure I could do this with JS, but was wondering if there's a way of doing it in CSS only, something like word-break: "_".

答案1

得分: 1

很显然,只用CSS无法实现,word-break 属性没有该选项。
您可以使用HTML标签<wbr>来添加一个单词断点,浏览器可以选择是否在此处断行。

英文:

Apparently there are no way of doing only with CSS, word-break property doesn't have that option.
You can use the html tag <wbr> to add a work break oportunity, where the browser may optionally break a line.

huangapple
  • 本文由 发表于 2023年5月25日 21:28:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332842.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定