英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论