CSS 自动换行/下划线换行以及空格和连字符
我有一堆很长的文件名,导致 HTML 格式溢出。所有这些文件名都使用下划线而不是空格,如果它们是空格,则很容易断开/换行。像这样。
Here_is_an_example_of_a_really_long_filename_that_uses_underscores_instead_of_spaces.pdf
是否有某种方法可以告诉CSS将文本中的下划线视为空格或连字符,从而也可以在下划线上换行/换行?像这样。
Here_is_an_example_of_a_really_long_
使用下划线的文件名_
相反_of_spaces.pdf
出于我的目的,我无法使用脚本来执行此操作。我也不想使用 word-wrap:break-word 技巧,因为如果不指定宽度,这通常不起作用。而且,它在单词中间任意中断。
谢谢。
I have a bunch of really long file names that cause my HTML formatting to overflow. All of these filenames use underscores instead of spaces and would break/wrap easily if they were spaces. Like this.
Here_is_an_example_of_a_really_long_filename_that_uses_underscores_instead_of_spaces.pdf
Is there some way to tell CSS to treat underscores in text as if they were whitespace or hyphens, and thus wrap/break on underscores too? Like this.
Here_is_an_example_of_a_really_long_
file_name_that_uses_underscores_
instead_of_spaces.pdf
For my purposes, I cannot use a script to do this. I also don't want to use the word-wrap:break-word trick because that usually doesn't work without also specifying a width. Also, it breaks arbitrarily in the middle of words.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用
标签(“换行机会元素”),它允许浏览器在您放置的位置处换行。所以你的 HTML 应该是:
你可以在输出 HTML 之前在服务器端添加这个标签。
另一种选择是实体
,它是零宽度空间。有时这在某些浏览器上效果更好。You can use the
<wbr>
tag ("Line Break Opportunity element"), which lets the browser break the line wherever you place it.So your HTML should be:
You can add this tag on the server-side before you output the HTML.
An alternative is the entity
which is a zero width space. Sometimes this works better on certain browsers.使用 CSS
检查 JSFiddle,
Use CSS
Check the JSFiddle,
目前您似乎无法使用 CSS 来实现此目的。
但是您可以使用 JavaScript 自动添加
标签,例如:It seems you can't use CSS for this purpose currently.
But you can use JavaScript to add
<wbr>
tags automatically, for example:如果没有 HTML5,您可以使用 Javascript 在要中断的字符之前或之后插入
:
然后使用容器的 CSS 类来包装单词 :
和最后设置一个零像素宽的空白作为跨度之前的内容:
Without HTML5, you can use Javascript to insert
<span></span>
before or after the characters you want to break on:then use the CSS class of the container to wrap the words :
and finally set a zero pixel wide white space as the content before the span :
不使用 JavaScript 或
,您可以插入(注意空格),带有以下 CSS:
标记:
Without using JavaScript nor
<wbr>
you could insert<span> </span>
(notice the space) with following CSS:The markup:
这是 TypeScript 中的另一个选项:
Here is another option, in TypeScript: