Is there a way to automatically control orphaned words in an HTML document?

谁说胖子不能爱 提交于 2019-11-29 03:37:20

You can avoid orphaned words by replacing the space between the last two words in a sentence with a non-breaking space ( ).

There are plugins out there that does this, for example jqWidon't or this jquery snippet.

There are also plugins for popular frameworks (such as typogrify for django and widon't for wordpress) that essentially does the same thing.

I know you wanted a javascript solution, but in case someone found this page a solution but for emails (where Javascript isn't an option), I decided to post my solution.

Use CSS white-space: nowrap. So what I do is surround the last two or three words (or wherever I want the "break" to be) in a span, add an inline CSS (remember, I deal with email, make a class as needed):

<td>
    I don't <span style="white-space: nowrap;">want orphaned words.</span>
</td>

In a fluid/responsive layout, if you do it right, the last few words will break to a second line until there is room for those words to appear on one line.

Read more about about the white-space property on this link: http://www.w3schools.com/cssref/pr_text_white-space.asp

EDIT: 12/19/2015 - Since this isn't supported in Outlook, I've been adding a non-breaking space &nbsp; between the last two words in a sentence. It's less code, and supported everywhere.

EDIT: 2/20/2018 - I've discovered that the Outlook App (iOS and Android) doesn't support the &nbsp; entity, so I've had to combine both solutions: e.g.:

<td>
    I don't <span style="white-space:nowrap;">want&nbsp;orphaned&nbsp;words.</span>
</td>
Jonathan Rich

In short, no. This is something that has driven print designers crazy for years, but HTML does not provide this level of control.

If you absolutely positively want this, and understand the speed implications, you can try the suggestion here:

detecting line-breaks with jQuery?

That is the best solution I can imagine, but that does not make it a good solution.

CD Jorgensen

If you want to handle it yourself, without jQuery, you can write a javascript snippet to replace the text, if you're willing to make a couple assumptions:

  1. A sentence always ends with a period.
  2. You always want to replace the whitespace before the last word with &nbsp;

Assuming you have this html (which is styled to break right before "end" in my browser...monkey with the width if needed):

<div id="articleText" style="width:360px;color:black; background-color:Yellow;">
    This is some text with one word on its own line at the end.
    <p />
    This is some text with one word on its own line at the end.
</div>

You can create this javascript and put it at the end of your page:

<script type="text/javascript">
    reformatArticleText();
    function reformatArticleText()
    {
        var div = document.getElementById("articleText");
        div.innerHTML = div.innerHTML.replace(/\S(\s*)\./g, "&nbsp;$1.");
    }
</script>

The regex simply finds all instances (using the g flag) of a whitespace character (\S) followed by any number of non-whitespace characters (\s) followed by a period. It creates a back-reference to the non-white-space that you can use in the replace text.

You can use a similar regex to include other end punctuation marks.

If third-party JavaScript is an option, one can use typogr.js, a JavaScript "typogrify" implementation. This particular filter is called, unsurprisingly, Widont.

<script src="https://cdnjs.cloudflare.com/ajax/libs/typogr/0.6.7/typogr.min.js"></script>
<script>
document.body.innerHTML = typogr.widont(document.body.innerHTML);
</script>
</body>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!