Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:25:07+00:00 2026-06-13T00:25:07+00:00

I need javascript to detect each browser wrapped line of text and wrap it

  • 0

I need javascript to detect each browser wrapped line of text and wrap it into <span class="line">.

I came across articles talking about measuring the y axis of each word, but have not seen a solid solution.

Here’s what I have so far. See it on Jsfiddle.

HTML

<div class="inline-bg">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec cursus condimentum metus, et placerat augue rutrum vitae. Donec arcu lorem, eleifend at elementum eget, consectetur vel lacus. Nam euismod iaculis varius. Phasellus sed dui diam. Nullam facilisis, diam sit amet sagittis cursus, metus tortor gravida erat, ut fringilla risus ipsum eu nisl.</div>​

JS/jQuery

(function($){
    $.fn.inlinebackground = function() {
        $.each(this, function(i,t) {
            var split = $(t).html().split(' ');
            var output = '';
            $.each(split, function(i,o){
                output += '<span class="line">'+o+'</span>';
                if (i < (split.length - 1)) {
                    output += '<br>';
                }
            });
            $(t).html(output);
        });
    }
})(jQuery);

/* End Plugin Code */

// Run the plugin on .news-caption 
$(function(){
    $('.inline-bg').inlinebackground();
});

CSS

.inline-bg { width: 200px; line-height:3em; }
.inline-bg span.line { background-color: black; color: white; padding: 15px; }

​

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:25:09+00:00Added an answer on June 13, 2026 at 12:25 am

    I have to admit at first I thought this would be a daunting task since there is no way to task browser to tell you where auto wrap line breaks take place.

    I’ve created a solution that first wraps each word in a span, then goes through all the spans to determine their top position in container. It then builds an array of indexes of line start and end spans and wraps each line of word spans in a wrapping span.

    DEMO: http://jsfiddle.net/KVepp/2/

    Possible Limitations:

    • Space added at end of each span may perhaps cause a span to break to
      a new line where text may not.
    • Not sure if each word span needs to be removed once lines are wrapped. ( very simple mod)
    • Assumes no other html in container other than text
    • Needs a little additional work to be turned into a plugin if needed
      for multiple containers
    • regex for words is simple split at space. Likely need additional
      regex for recurring spaces

    HTML:

    <div id="content">Lorem Ipsum<div> 
    

    CSS:

    #content{ position:relative}
    

    JS:

    var $cont = $('#content')
    
    var text_arr = $cont.text().split(' ');
    
    for (i = 0; i < text_arr.length; i++) {
        text_arr[i] = '<span>' + text_arr[i] + ' </span>';
    }
    
    $cont.html(text_arr.join(''));
    
    $wordSpans = $cont.find('span');
    
    var lineArray = [],
        lineIndex = 0,
        lineStart = true,
        lineEnd = false
    
    $wordSpans.each(function(idx) {
        var pos = $(this).position();
        var top = pos.top;
    
        if (lineStart) {
            lineArray[lineIndex] = [idx];
            lineStart = false;
    
        } else {
            var $next = $(this).next();
    
            if ($next.length) {
                if ($next.position().top > top) {
                    lineArray[lineIndex].push(idx);
                    lineIndex++;
                    lineStart = true
                }
            } else {
                lineArray[lineIndex].push(idx);
            }
        }
    
    });
    
    for (i = 0; i < lineArray.length; i++) {
    var start = lineArray[i][0],
        end = lineArray[i][1] + 1;
    
    /* no end value pushed to array if only one word last line*/
    if (!end) {
        $wordSpans.eq(start).wrap('<span class="line_wrap">')
    } else {
        $wordSpans.slice(start, end).wrapAll('<span class="line_wrap">');
    }
    

    }​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to detect and eval the Javascript code contained in a string. The
In javascript I need to get the text within the custom tag. For example
I have an input box: input type=text class=rulerInputText id=rulerInputBox readonly I have Javascript code
How to detect Ctrl + V , Ctrl + C using JavaScript? I need
I need some javascript or php code to detect if user has linked directly
I found this javascript to detect blackberry devices: <script type=text/javascript> var ua = navigator.userAgent;
When a user clicks on my green button, I need Javascript to detect this
I need detect words that separate with space in a text. for example my
I need to load and display a PDF in an ASP.NET/Javascript application and detect
I need to do a few things in one javascript solution. Must first detect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.