Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Reworked my first answer. Works a lot better now. See the <a href="http://jsfiddle.net/vQvkn/" rel="nofollow">fiddle</a>. It is based on the idea that you split the paragraph on the pivot word. The pivotword and the last section are placed back in the paragraph. The first half (before the pivot word) is then split into an array, which is traversed backwards (each time popping the last element) to fill the span until it reaches its width. This will repeat itself until there are no more words left in the array. I am not a native English speaker, so I hope this will all make some sense.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript" src="js/jquery-1.9.0/jquery.min.js"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; .container { width: 500px; border: 1px solid #ddd; } .pivotWord { background-color: red; color: white; font-weight: bold; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;p&gt; Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in PIVOT voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. &lt;/p&gt; &lt;p&gt; Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. PIVOT Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. &lt;/p&gt; &lt;p&gt; Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et PIVOT dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. &lt;/p&gt; &lt;p&gt; Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non PIVOT proident, sunt in culpa qui officia deserunt mollit anim id est laborum. &lt;/p&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; function pivotAround(pivotword){ $('p').each(function(){ //initialize a few things var sentence = $(this).html(); var splittedSentence = sentence.split(pivotword); var paragraphWidth = $(this).width(); $(this).html(""); //create elements from the sentence parts. var pivotSpan = $("&lt;span /&gt;", { 'class': 'pivotWord' }).html(pivotword); var postSpan = $("&lt;span /&gt;", { }).html(splittedSentence[1]); //append them to the DOM $(this).append(pivotSpan) .append(postSpan); //get size of pivotSpan var pivotSpanWidth = pivotSpan.width(); //calculate where the pivot word should be var pivotSpanLeftMargin = (paragraphWidth / 2) - (pivotSpanWidth / 2); //make global array of splittedSentence[0] preSpanArray = splittedSentence[0].split(' '); distributeWithinMargin(pivotSpanLeftMargin, this); //array not empty? while(preSpanArray.length &gt; 0){ distributeWithinMargin(paragraphWidth, this); } }); } function distributeWithinMargin(margin, element) { var span = $("&lt;span /&gt;", { 'style': 'margin-left: -40000px' }); $(element).prepend(span); while (preSpanArray.length &gt; 0 &amp;&amp; span.width() &lt;= margin) { var lastElement = preSpanArray.pop(); span.prepend(lastElement + " "); } /* * last word makes the span too wide, so push it back to the stack * only do this when array not empty, or you will end up in an * endless loop */ if (preSpanArray.length &gt; 0) { preSpanArray.push(lastElement); //remove that word from the span var firstSpaceIndex = $(span).html().indexOf(" "); $(span).html($(span).html().substring(firstSpaceIndex + 1)); } //calculate the text-indent from that value var textIndent = margin - span.width(); $(span).css('margin-left', textIndent); } pivotAround("PIVOT"); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload