如有必要,如何允许文本自动换行?

发布于 2024-08-20 23:03:32 字数 289 浏览 10 评论 0 原文

我正在寻找最好的解决方案,以便在必要时允许文本在单词中间换行。我所说的“最好”是指与大多数浏览器兼容,并且会在单词内部断词之前支持断词。

如果标记看起来比我的更好(请参阅我的答案),也会有所帮助。

编辑:

请注意,这是专门针对用户生成的内容的。

编辑 2:

相关网站上大约 25% 的 Firefox 用户仍在使用 v3.0 或更低版本,因此支持他们至关重要。这是基于上个月的数据(约 121,000 次访问)。

I am looking for the best solution to allow text to wrap in the middle of a word if necessary. By best, I mean most browser-compatible, and will favor word breaks before it breaks inside a word.

It would also help if the markup looked nicer than mine (see my answer).

Edit:

Note this is specifically for user-generated content.

Edit 2:

About 25% of Firefox users on the site in question are still using v3.0 or below, so it is critical to support them. This is based on the last month worth of data (about 121,000 visits).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

时光沙漏 2024-08-27 23:03:32

如果长单词对于容器来说太长,CSS 属性 word-wrap:break-word 将强制长单词换行到下一行。 IE(可以追溯到很久以前)、Firefox 和 Safari 均支持此功能。

编辑:看起来您也可以在旧版本的 Firefox 中使用 white-space: -moz-pre-wrapwhite-space: pre-wrap 来实现此目的>。请参阅此处了解更多信息。不过我还没有在旧版本的 Firefox 上测试过这一点。

The css property word-wrap: break-word will force long words to wrap to the next line if they are too long for their container. This is supported by IE (going way back), Firefox and Safari.

Edit: Looks like you may be able to achieve this in older versions of Firefox as well using white-space: -moz-pre-wrap and white-space: pre-wrap. See here for more info. I have not tested this with older versions of Firefox though.

も星光 2024-08-27 23:03:32

由于没有明确的答案(取决于您的需求,例如,您想要连字符吗?您必须支持哪些浏览器?),我通过 Adobe BrowserLab 了解选项是什么:

如果不需要连字符,则使用 可以获得最佳兼容性。如果您需要连字符,那么使用 ­ 是您的最佳选择,但请注意,这在 Firefox 2.0 Mac/Windows 或 Safari 3.0 中不起作用(在字符处换行)。

并且,请注意,如果您选择通过使用溢出、滚动或允许在字符处换行来根本不处理长单词,则 IE6 和 IE7 都会通过扩展容器宽度来响应(至少使用 DIV我用过),所以要小心。

结果:

Browser                     Method                   Wraps at char    Adds Hyphens    Overflows horizontally    Container expands horizontally
----------------------------------------------------------------------------------------------------------------------------------------------
Firefox 3.0 - Windows XP    None                     No               No              Yes                       No
Firefox 3.0 - Windows XP    <wbr>                    Yes              No              No                        No
Firefox 3.0 - Windows XP    ­ or ­          Yes              Yes             No                        No
Firefox 3.0 - Windows XP    word-wrap: break-word    No               No              Yes                       No
IE7 - Windows XP            None                     No               No              No                        Yes
IE7 - Windows XP            <wbr>                    Yes              No              No                        No
IE7 - Windows XP            ­ or ­          Yes              Yes             No                        No
IE7 - Windows XP            word-wrap: break-word    Yes              No              No                        No
Firefox 3.0 - OS X          None                     No               No              Yes                       No
Firefox 3.0 - OS X          <wbr>                    Yes              No              No                        No
Firefox 3.0 - OS X          ­ or ­          Yes              Yes             No                        No
Firefox 3.0 - OS X          word-wrap: break-word    No               No              Yes                       No
Safari 3.0 - OS X           None                     No               No              Yes                       No
Safari 3.0 - OS X           <wbr>                    Yes              No              No                        No
Safari 3.0 - OS X           ­ or ­          No               No              No                        No
Safari 3.0 - OS X           word-wrap: break-word    Yes              No              No                        No
Chrome 3.0 - Windows XP     None                     No               No              Yes                       No
Chrome 3.0 - Windows XP     <wbr>                    Yes              No              No                        No
Chrome 3.0 - Windows XP     ­ or ­          Yes              Yes             No                        No
Chrome 3.0 - Windows XP     word-wrap: break-word    Yes              No              No                        No
Firefox 2.0 - OS X          None                     No               No              Yes                       No
Firefox 2.0 - OS X          <wbr>                    Yes              No              No                        No
Firefox 2.0 - OS X          ­ or ­          No               No              Yes                       No
Firefox 2.0 - OS X          word-wrap: break-word    No               No              Yes                       No
Firefox 2.0 - Windows XP    None                     No               No              Yes                       No
Firefox 2.0 - Windows XP    <wbr>                    Yes              No              No                        No
Firefox 2.0 - Windows XP    ­ or ­          No               No              Yes                       No
Firefox 2.0 - Windows XP    word-wrap: break-word    No               No              Yes                       No
Firefox 3.5 - Windows XP    None                     No               No              Yes                       No
Firefox 3.5 - Windows XP    <wbr>                    Yes              No              No                        No
Firefox 3.5 - Windows XP    ­ or ­          Yes              Yes             No                        No
Firefox 3.5 - Windows XP    word-wrap: break-word    Yes              No              No                        No
Firefox 3.5 - OS X          None                     No               No              Yes                       No
Firefox 3.5 - OS X          <wbr>                    Yes              No              No                        No
Firefox 3.5 - OS X          ­ or ­          Yes              Yes             No                        No
Firefox 3.5 - OS X          word-wrap: break-word    Yes              No              No                        No
IE6 - Windows XP            None                     No               No              No                        Yes
IE6 - Windows XP            <wbr>                    Yes              No              No                        No
IE6 - Windows XP            ­ or ­          Yes              Yes             No                        No
IE6 - Windows XP            word-wrap: break-word    Yes              No              No                        No
IE8 - Windows XP            None                     No               No              Yes                       No
IE8 - Windows XP            <wbr>                    Yes              No              No                        No
IE8 - Windows XP            ­ or ­          Yes              Yes             No                        No
IE8 - Windows XP            word-wrap: break-word    Yes              No              No                        No
Safari 4.0 - OS X           None                     No               No              Yes                       No
Safari 4.0 - OS X           <wbr>                    Yes              No              No                        No
Safari 4.0 - OS X           ­ or ­          Yes              Yes             No                        No
Safari 4.0 - OS X           word-wrap: break-word    Yes              No              No                        No

示例 HTML:

<html>
    <head>
        <style>
            div {
                width: 4em;
                border: 1px solid black;
                margin-bottom: 6em;
                padding: .25em;
            }
        </style>    
    </head>
    <body>
        This is text easily contained by the DIV:
        <div>proper width</div>

        A long word with no character breaking:
        <div>
            AReallyLongWordThatNeedsToBeBroken AndAnotherWord
        </div>

        <i><wbr></i> tag:
        <div>
            A<wbr>R<wbr>e<wbr>a<wbr>l<wbr>l<wbr>y<wbr>L<wbr>o<wbr>n<wbr>g<wbr>W<wbr>o<wbr>r<wbr>d<wbr>T<wbr>h<wbr>a<wbr>t<wbr>N<wbr>e<wbr>e<wbr>d<wbr>s<wbr>T<wbr>o<wbr>B<wbr>e<wbr>B<wbr>r<wbr>o<wbr>k<wbr>e<wbr>n A<wbr>n<wbr>d<wbr>A<wbr>n<wbr>o<wbr>t<wbr>h<wbr>e<wbr>r<wbr>W<wbr>o<wbr>r<wbr>d
        </div>

        <i>&shy;</i> character:
        <div>
            A­R­e­a­l­l­y­L­o­n­g­W­o­r­d­T­h­a­t­N­e­e­d­s­T­o­B­e­B­r­o­k­e­n A­n­d­A­n­o­t­h­e­r­W­o­r­d
        </div>

        <i>overflow: scroll</i> CSS attribute:
        <div style="overflow: scroll">
            AReallyLongWordThatNeedsToBeBroken AndAnotherWord
        </div>

        <i>word-wrap: break-word</i> CSS attribute:
        <div style="word-wrap: break-word">
            AReallyLongWordThatNeedsToBeBroken AndAnotherWord
        </div>
    </body>
</html>

Since there is no definitive answer (depends on your needs, e.g., do you want hyphens, what browsers must you support?), I did some research via Adobe BrowserLab to find out what the options are:

If you do not need hyphens, you will get best compatibility using <wbr>. If you require hyphens, then using ­ is your best bet, but note that this will not work (wrap at char) in Firefox 2.0 Mac/Windows, or Safari 3.0.

And, note that if you choose to not handle long words at all by using overflow, scroll or allowing wrap at a character, both IE6 and IE7 will respond by expanding the container width (at least with the DIV I used), so beware.

Results:

Browser                     Method                   Wraps at char    Adds Hyphens    Overflows horizontally    Container expands horizontally
----------------------------------------------------------------------------------------------------------------------------------------------
Firefox 3.0 - Windows XP    None                     No               No              Yes                       No
Firefox 3.0 - Windows XP    <wbr>                    Yes              No              No                        No
Firefox 3.0 - Windows XP    ­ or ­          Yes              Yes             No                        No
Firefox 3.0 - Windows XP    word-wrap: break-word    No               No              Yes                       No
IE7 - Windows XP            None                     No               No              No                        Yes
IE7 - Windows XP            <wbr>                    Yes              No              No                        No
IE7 - Windows XP            ­ or ­          Yes              Yes             No                        No
IE7 - Windows XP            word-wrap: break-word    Yes              No              No                        No
Firefox 3.0 - OS X          None                     No               No              Yes                       No
Firefox 3.0 - OS X          <wbr>                    Yes              No              No                        No
Firefox 3.0 - OS X          ­ or ­          Yes              Yes             No                        No
Firefox 3.0 - OS X          word-wrap: break-word    No               No              Yes                       No
Safari 3.0 - OS X           None                     No               No              Yes                       No
Safari 3.0 - OS X           <wbr>                    Yes              No              No                        No
Safari 3.0 - OS X           ­ or ­          No               No              No                        No
Safari 3.0 - OS X           word-wrap: break-word    Yes              No              No                        No
Chrome 3.0 - Windows XP     None                     No               No              Yes                       No
Chrome 3.0 - Windows XP     <wbr>                    Yes              No              No                        No
Chrome 3.0 - Windows XP     ­ or ­          Yes              Yes             No                        No
Chrome 3.0 - Windows XP     word-wrap: break-word    Yes              No              No                        No
Firefox 2.0 - OS X          None                     No               No              Yes                       No
Firefox 2.0 - OS X          <wbr>                    Yes              No              No                        No
Firefox 2.0 - OS X          ­ or ­          No               No              Yes                       No
Firefox 2.0 - OS X          word-wrap: break-word    No               No              Yes                       No
Firefox 2.0 - Windows XP    None                     No               No              Yes                       No
Firefox 2.0 - Windows XP    <wbr>                    Yes              No              No                        No
Firefox 2.0 - Windows XP    ­ or ­          No               No              Yes                       No
Firefox 2.0 - Windows XP    word-wrap: break-word    No               No              Yes                       No
Firefox 3.5 - Windows XP    None                     No               No              Yes                       No
Firefox 3.5 - Windows XP    <wbr>                    Yes              No              No                        No
Firefox 3.5 - Windows XP    ­ or ­          Yes              Yes             No                        No
Firefox 3.5 - Windows XP    word-wrap: break-word    Yes              No              No                        No
Firefox 3.5 - OS X          None                     No               No              Yes                       No
Firefox 3.5 - OS X          <wbr>                    Yes              No              No                        No
Firefox 3.5 - OS X          ­ or ­          Yes              Yes             No                        No
Firefox 3.5 - OS X          word-wrap: break-word    Yes              No              No                        No
IE6 - Windows XP            None                     No               No              No                        Yes
IE6 - Windows XP            <wbr>                    Yes              No              No                        No
IE6 - Windows XP            ­ or ­          Yes              Yes             No                        No
IE6 - Windows XP            word-wrap: break-word    Yes              No              No                        No
IE8 - Windows XP            None                     No               No              Yes                       No
IE8 - Windows XP            <wbr>                    Yes              No              No                        No
IE8 - Windows XP            ­ or ­          Yes              Yes             No                        No
IE8 - Windows XP            word-wrap: break-word    Yes              No              No                        No
Safari 4.0 - OS X           None                     No               No              Yes                       No
Safari 4.0 - OS X           <wbr>                    Yes              No              No                        No
Safari 4.0 - OS X           ­ or ­          Yes              Yes             No                        No
Safari 4.0 - OS X           word-wrap: break-word    Yes              No              No                        No

Sample HTML:

<html>
    <head>
        <style>
            div {
                width: 4em;
                border: 1px solid black;
                margin-bottom: 6em;
                padding: .25em;
            }
        </style>    
    </head>
    <body>
        This is text easily contained by the DIV:
        <div>proper width</div>

        A long word with no character breaking:
        <div>
            AReallyLongWordThatNeedsToBeBroken AndAnotherWord
        </div>

        <i><wbr></i> tag:
        <div>
            A<wbr>R<wbr>e<wbr>a<wbr>l<wbr>l<wbr>y<wbr>L<wbr>o<wbr>n<wbr>g<wbr>W<wbr>o<wbr>r<wbr>d<wbr>T<wbr>h<wbr>a<wbr>t<wbr>N<wbr>e<wbr>e<wbr>d<wbr>s<wbr>T<wbr>o<wbr>B<wbr>e<wbr>B<wbr>r<wbr>o<wbr>k<wbr>e<wbr>n A<wbr>n<wbr>d<wbr>A<wbr>n<wbr>o<wbr>t<wbr>h<wbr>e<wbr>r<wbr>W<wbr>o<wbr>r<wbr>d
        </div>

        <i>&shy;</i> character:
        <div>
            A­R­e­a­l­l­y­L­o­n­g­W­o­r­d­T­h­a­t­N­e­e­d­s­T­o­B­e­B­r­o­k­e­n A­n­d­A­n­o­t­h­e­r­W­o­r­d
        </div>

        <i>overflow: scroll</i> CSS attribute:
        <div style="overflow: scroll">
            AReallyLongWordThatNeedsToBeBroken AndAnotherWord
        </div>

        <i>word-wrap: break-word</i> CSS attribute:
        <div style="word-wrap: break-word">
            AReallyLongWordThatNeedsToBeBroken AndAnotherWord
        </div>
    </body>
</html>
甲如呢乙后呢 2024-08-27 23:03:32

我的答案来自这个问题的另一个实例,可以在 Wrapping long text in CSS

我使用组合

word-wrap: break-word;
overflow: hidden;

来处理这个问题。 word-wrap 设置将允许单词在支持该属性的浏览器中进行换行,无论单词的长度如何,而 overflow 设置将导致单词在末尾被截断无法识别自动换行的浏览器中的可用空间。这与您在不使用 JavaScript 的情况下可能获得的降级效果差不多。

My answer from another instance of this question, found at Wrapping long text in CSS :

I use the combination

word-wrap: break-word;
overflow: hidden;

to deal with this. The word-wrap setting will allow the word to be wrapped despite its length in browsers which support that property, while the overflow setting will cause it to be cut off at the end of the available space in browsers which don't recognize word-wrap. That's about as graceful of degradation as you're likely to get without going to javascript.

沩ん囻菔务 2024-08-27 23:03:32

[编辑:OP更新了他的问题,说这是专门针对用户生成的内容的;下面的内容不会(很容易)工作(你必须有自己的连字符字典)。不过,将其留在这里,以防万一有人发现此问题并需要 HTML 选项来执行此操作。]

您可以使用软连字符(也称为可选连字符;­& #173;)。它早在 HTML4 中就已定义。我从未使用过它,但我只是尝试过,它至少适用于:IE6、IE7、Chrome 4、FF 3.6 和 Safari 4。

有人可能认为用户代理(浏览器)应该自动处理这个问题(使用文档所用语言的连字符字典)。 实际上,我不知道有哪个网络浏览器可以自动执行此操作。显然,如果您告诉现代浏览器它们可以,现代浏览器就会自动执行此操作;请参阅 wsanville 对 CSS 选项的回答。

[Edit: OP updated his question to say this is specifically for user-generated content; the below won't (easily) work for that (you'd have to have your own hyphenation dictionary). Leaving it here, though, in case someone finds this question and needs an HTML option for doing this.]

You can use a soft hyphen (aka optional hyphen; ­ or ­). It was defined way back in HTML4. I've never used it, but I just tried and it works in: IE6, IE7, Chrome 4, FF 3.6, and Safari 4 for Windows at least.

One could argue that the user-agent (browser) should handle this automatically (using a hyphenation dictionary for the language the document is in). In practice, I don't know of a single web browser that does it automatically. Apparently, modern browsers do if you tell them they can; see wsanville's answer for a CSS option.

旧城烟雨 2024-08-27 23:03:32

我在这里找到了类似的答案:
http://css-tricks。 com/snippets/css/prevent-long-urls-from-breaking-out-of-container/

更强大的浏览器支持

仅 CSS

-ms-word-break: break-all;

     /* Be VERY careful with this, breaks normal words wh_erever */
     word-break: break-all;

     /* Non standard for webkit */
     word-break: break-word;

-webkit-hyphens: auto;
   -moz-hyphens: auto;
        hyphens: auto;

如果您的文本是 PHP 生成的< /strong> 你有一个 PHP 函数

wordwrap

<?php
  $text = "The quick brown fox jumped over the lazy dog.";
  $newtext = wordwrap($text, 20, "<br />\n");

  echo $newtext;
?>

另外,如果您知道分隔符,您可以尝试 PHP 函数爆炸
使用 for 循环后显示(分解返回一个数组)。

I found a similar answer here:
http://css-tricks.com/snippets/css/prevent-long-urls-from-breaking-out-of-container/

A more robust browser support

CSS only

-ms-word-break: break-all;

     /* Be VERY careful with this, breaks normal words wh_erever */
     word-break: break-all;

     /* Non standard for webkit */
     word-break: break-word;

-webkit-hyphens: auto;
   -moz-hyphens: auto;
        hyphens: auto;

If your text is PHP generated you have a PHP function:

wordwrap

<?php
  $text = "The quick brown fox jumped over the lazy dog.";
  $newtext = wordwrap($text, 20, "<br />\n");

  echo $newtext;
?>

Also, you can try PHP function explode if you have and know the delimiter.
Display after with for loop (explode returns an array).

后eg是否自 2024-08-27 23:03:32

您可能需要检查 word-wrap: break-word CSS 属性。

以下示例适用于 Google Chrome 4.0、Firefox 3.5.7、Safari 4.0.4 和 IE 8:

<html>
<body>
    <div style="word-wrap: break-word; width: 50px;">
        Loremipsumdolorsitametconsectetueradipiscingelitselaoreetdoloremagnaaliq
    </div>
</body>
</html>

不幸的是,正如 BalusC 在对其中一个答案的评论中,上面的不适用于 3.5 之前的 Firefox 版本。由于使用旧版本 Firefox 的用户数量似乎低于约 6% ,如TJ Crowder 指出,对于不支持自动换行的浏览器,您可能需要回退到overflow:scroll。它可能看起来不太好,但是如果您不希望很多单词比分配的空间长,那么其中一个用户看到滚动条的概率会非常低,并且随着时间的推移,它会变得更低,用户升级浏览器。至少在这些情况下你会优雅地失败。

You may want to check the word-wrap: break-word CSS property.

The following example works in Google Chrome 4.0, Firefox 3.5.7, Safari 4.0.4, and IE 8:

<html>
<body>
    <div style="word-wrap: break-word; width: 50px;">
        Loremipsumdolorsitametconsectetueradipiscingelitselaoreetdoloremagnaaliq
    </div>
</body>
</html>

Unfortunately as noted by BalusC in a comment to one of the answers, the above will not work in versions of Firefox older than 3.5. Since the population of users using old Firefox versions seems to be below ~6%, as noted by T.J. Crowder, you may want to fallback to an overflow: scroll for browsers that do not support word-wrap. It may not look very nice, but if you are not expecting many words to be longer than the allocated space, the probability that one of the users will see the scroll bar would be very low, and it will get even lower as time passes and users upgrade their browsers. At least you would be failing gracefully in those situations.

只怪假的太真实 2024-08-27 23:03:32

过去,我使用过类似的东西(当时在 Safari 上不起作用,IIRC,但那是几年前的事了):

AVerLonngWord AndA notherWord

(我会动态生成这个,以便做布局的人不会看到这样的暴行。)

In the past, I have used something like this, (which did not work on Safari at the time, IIRC, but that was several years ago):

A<wbr>V<wbr>e<wbr>r<wbr>y<wbr>L<wbr>o<wbr>n<wbr>g<wbr>W<wbr>o<wbr>r<wbr>d A<wbr>n<wbr>d<wbr>A<wbr>n<wbr>o<wbr>t<wbr>h<wbr>e<wbr>r<wbr>W<wbr>o<wbr>r<wbr>d

(I would generate this dynamically so that the person doing layout would not have to look at such an atrocity.)

始终不够 2024-08-27 23:03:32

在您想要断行的位置输入键盘换行符。

例如。

Thisisverylongword

会变成

Thisisavery
长字

Enter keyboard line break at the point at which you want it to break.

eg.

Thisisaverylongword

would become

Thisisavery
longword

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文