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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...