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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.