CSS 自动换行/下划线换行以及空格和连字符

发布于 2024-11-15 03:32:02 字数 417 浏览 4 评论 0原文

我有一堆很长的文件名,导致 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赤濁 2024-11-22 03:32:02

您可以使用 标签(“换行机会元素”),它允许浏览器在您放置的位置处换行。

所以你的 HTML 应该是:

Here_<wbr>is_<wbr>an_<wbr>example_<wbr>of_<wbr>a_<wbr>really_<wbr>...

你可以在输出 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:

Here_<wbr>is_<wbr>an_<wbr>example_<wbr>of_<wbr>a_<wbr>really_<wbr>...

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.

西瓜 2024-11-22 03:32:02

使用 CSS

word-break: break-all

检查 JSFiddle

Use CSS

word-break: break-all

Check the JSFiddle,

李白 2024-11-22 03:32:02

目前您似乎无法使用 CSS 来实现此目的。

但是您可以使用 JavaScript 自动添加 标签,例如:

var arr = document.getElementsByClassName('filename');
for(var i = 0; i < arr.length; i++) {
  arr[i].innerHTML = arr[i].innerHTML.replace(/_/g, '_<wbr/>');
}
body { width: 250px; }
<a class="filename">Here_is_an_example_of_a_really_long_filename_that_uses_underscores_instead_of_spaces.pdf</a>

It seems you can't use CSS for this purpose currently.

But you can use JavaScript to add <wbr> tags automatically, for example:

var arr = document.getElementsByClassName('filename');
for(var i = 0; i < arr.length; i++) {
  arr[i].innerHTML = arr[i].innerHTML.replace(/_/g, '_<wbr/>');
}
body { width: 250px; }
<a class="filename">Here_is_an_example_of_a_really_long_filename_that_uses_underscores_instead_of_spaces.pdf</a>

長街聽風 2024-11-22 03:32:02

如果没有 HTML5,您可以使用 Javascript 在要中断的字符之前或之后插入

//Jquery
$("#id_of_the_container").html(function(index, text) {
    return text.replace(/_/g, "_<span />");
});

//Pure Javascript
var htmlText = document.getElementById("id_of_the_container").innerHTML;
document.getElementById("id_of_the_container").innerHTML = htmlText.replace(/_/g, "_<span />");

然后使用容器的 CSS 类来包装单词 :

.yourClass {
    word-break : break-word;
}

和最后设置一个零像素宽的空白作为跨度之前的内容:

.yourClass > span:before {
    content: "\200b";
}

$("#set").html(function(index, text) {
  return text.replace(/_/g, "_<span />");
});
.boxes-container > div {
  margin: 5px;
  max-width: 200px;
  border : solid black 2px;
  display: inline-block;
  padding : 5px;
}

.bigger {
  margin-right : 200px !important;
}

.wrap {
  word-break: break-word;
}

.wrap > span:before {
  content : "\200b";
}

.answer-example {
  color : RGB(0, 50, 0);
  border-color: RGB(0, 50, 0) !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="boxes-container">
  <div class="bigger">
    Without_Anything,
    Long_Strings_With_Underscores_Make_text_Hard_To_Wrap
  </div>
  <div class="wrap">
    Without_Span_tags,
    Long_Strings_With_Underscores_Make_text_Hard_To_Wrap
  </div>
  <div id="set" class="wrap answer-example">
    With_Span_tags,
    Long_Strings_With_Underscores_Make_text_Hard_To_Wrap
  </div>
</div>

Without HTML5, you can use Javascript to insert <span></span> before or after the characters you want to break on:

//Jquery
$("#id_of_the_container").html(function(index, text) {
    return text.replace(/_/g, "_<span />");
});

//Pure Javascript
var htmlText = document.getElementById("id_of_the_container").innerHTML;
document.getElementById("id_of_the_container").innerHTML = htmlText.replace(/_/g, "_<span />");

then use the CSS class of the container to wrap the words :

.yourClass {
    word-break : break-word;
}

and finally set a zero pixel wide white space as the content before the span :

.yourClass > span:before {
    content: "\200b";
}

$("#set").html(function(index, text) {
  return text.replace(/_/g, "_<span />");
});
.boxes-container > div {
  margin: 5px;
  max-width: 200px;
  border : solid black 2px;
  display: inline-block;
  padding : 5px;
}

.bigger {
  margin-right : 200px !important;
}

.wrap {
  word-break: break-word;
}

.wrap > span:before {
  content : "\200b";
}

.answer-example {
  color : RGB(0, 50, 0);
  border-color: RGB(0, 50, 0) !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="boxes-container">
  <div class="bigger">
    Without_Anything,
    Long_Strings_With_Underscores_Make_text_Hard_To_Wrap
  </div>
  <div class="wrap">
    Without_Span_tags,
    Long_Strings_With_Underscores_Make_text_Hard_To_Wrap
  </div>
  <div id="set" class="wrap answer-example">
    With_Span_tags,
    Long_Strings_With_Underscores_Make_text_Hard_To_Wrap
  </div>
</div>

洒一地阳光 2024-11-22 03:32:02

不使用 JavaScript 或 ,您可以插入 (注意空格),带有以下 CSS:

span{
    width: 0;
    overflow: hidden;
    display: inline-block;
}

标记:

Here_<span> </span>is_<span> </span>an_<span> </span>example...

Without using JavaScript nor <wbr> you could insert <span> </span> (notice the space) with following CSS:

span{
    width: 0;
    overflow: hidden;
    display: inline-block;
}

The markup:

Here_<span> </span>is_<span> </span>an_<span> </span>example...
野味少女 2024-11-22 03:32:02

这是 TypeScript 中的另一个选项:

const addWordBreakAfterUnderscores = (content: string) => {
    // u200B is a zero-width space, which is used to prevent
    // the wordbreak from being visible.
    return content.replaceAll('_', '_\u200B');
};

Here is another option, in TypeScript:

const addWordBreakAfterUnderscores = (content: string) => {
    // u200B is a zero-width space, which is used to prevent
    // the wordbreak from being visible.
    return content.replaceAll('_', '_\u200B');
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文