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

  • SEARCH
  • Home
  • 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 309371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:44:09+00:00 2026-05-12T07:44:09+00:00

Do you know a good cross-browser way of knowing how wide will be a

  • 0

Do you know a good cross-browser way of knowing how wide will be a text line so you can break it exactly to fit a fixed width?

Suppose you want to break a long text like so it doesn’t overflow a fixed width container, but you want the line to break the closest to the border possible, so guessing where to insert ­s isn’t a clean solution.

I want to investigate, I imagine this could be done having an invisible div then printing the line inside it and checking the div’s width, or something like that, with Javascript.

Has anyone done something like this?

*(the focus is not word wrapping, that’s just the application that comes to my mind now, but knowing a text’s width is what I want)

  • 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-05-12T07:44:09+00:00Added an answer on May 12, 2026 at 7:44 am

    Here is a complete “Heath Robinson” (does that reference travel well?) approach.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <script type="text/javascript">
            function txtContent_onchange()
            {
                var span = document.getElementById("spanMeasure")
                span.innerHTML = "";
                span.appendChild(document.createTextNode(this.value));
                document.getElementById("txtWidth").value = span.scrollWidth;
    
            }
        </script>
        <style type="text/css">
            #spanMeasure 
            {
                position:absolute;
                top:0px;
                left:0px;
                visibility:hidden;
                width:10px;
                white-space:nowrap;
                overflow:hidden
            }
        </style>
    </head>
    <body>
    
        <input id="txtContent" onchange="txtContent_onchange.call(this)" /><br />
        <input id="txtWidth" />
        <span id="spanMeasure"><span>
    
    </body>
    </html>
    

    The critical thing here is the configuration of the span element. This element will not impact the visual appearance of the page. Its scrollWidth property will return the length of the text it contains. Of course you would need to set any font style attributes to get a reasonable value.

    According to quirksmode site Opera may be a little flaky with this approach but I suspect its the closest you will get to a fully cross-browser solution.

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

Sidebar

Related Questions

Does anyone know of any good resources for learning about cross-browser development in ASP.NET?
I want to know a good way of converting the Model to ViewModel and
Does anyone know of a good javascript DOM ready library that I can use
Does anyone know of a good cross platform VNC client library (the viewer end)?
many people write css but they don't have good command for writing cross browser
I know that Cross validation is used for selecting good parameters. After finding them,
I'd really like to know whether there's a comprehensive list of cross-browser support for
I need help figuring out the best, cross-browser compatible way to SAVE user input
What is the best cross-browser way to get a flat mouse coordinate input data
Does anyone know a good (preferably open source and cross platform) tool to allow

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.