Skip to main content
August 12, 2009
Question

String length in pixels?

  • August 12, 2009
  • 3 replies
  • 2623 views

Hello~

Is there a way to determine the width of a string in pixels, rather than characters? Thanks!

KC

    This topic has been closed for replies.

    3 replies

    Inspiring
    August 13, 2009

    If you are asking for that, then you aren't allowing the HTML markup-language to do its appointed job.

    You should describe "the size of things" and "the length of things" and "the position of things" in terms of typographer's terms, such as em and point, or simply generic terms like larger.  Then, use styles that enable the browser to come up with an arrangement of elements.

    These days, you've got no idea how big the user's screen is.

    You don't know if the guy reading your page is nineteen years old or (uhh...) fifty.

    August 24, 2009

    I managed to find a combination of CSS and JavaScript that did what I needed it to, without hard-coding the widths. Thanks!

    BKBK
    Community Expert
    Community Expert
    August 25, 2009
    a combination of CSS and JavaScript

    Indeed. I too was thinking about something like that. Put the string in a div or span and give the tag an ID. Then write Javascript that would, on clicking, for example, find the width of document.getElementById('theTagId').

    Participating Frequently
    August 13, 2009

    This is not a ColdFusion related question but a client side question

    because it depends on the font you're using to display the string (for

    example using Courier and Times New Roman gives you 2 different

    widths) and the context in which you're displaying the font: Java

    Applet, Flash, SVG, plain simple in browser, etc

    Mack

    August 13, 2009

    I thought that might be the case. Do you know of a good way to calculate the width when it's just displaying in the browser, nothing fancy, and with a standard font (Arial, in this case)?

    Participating Frequently
    August 13, 2009

    I think there's a plugin for jQuery called dimensions that does what you need.

    Mack

    Inspiring
    August 12, 2009

    In what context?