Skip to main content

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



I was wondering if there's a way to automatically control orphaned words in an HTML file, possibly by using CSS and/or Javascript (or something else, if anyone has an alternative suggestion).





By 'orphaned words', I mean singular words that appear on a new line at the end of a paragraph. For example:





"This paragraph ends with an undesirable orphaned

word."





Instead, it would be preferable to have the paragraph break as follows:





"This paragraph no longer ends with an undesirable

orphaned word."





While I know that I could manually correct this by placing an HTML non-breaking space (&nbsp) between the final two words, I'm wondering if there's a way to automate the process, since manual adjustments like this can quickly become tedious for large blocks of text across multiple files.





Incidentally, the CSS2 properties "orphans:" (and "widows:") only apply to entire lines of text, and even then only for the printing of HTML pages (not to mention the fact that these properties are largely unsupported by most major browsers).





Many professional page layout applications, such as Adobe InDesign, can automate the removal of orphans by automatically adding non-breaking spaces where orphans occur; is there any sort of equivalent solution for HTML?





Thank you in advance for your help!


Comments

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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  

    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.

    Edited to fix regex.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex