<p>In my (PHP) web app, I have a part of my site that keeps a history of recent searches. The most recent queries get shown in a side box. If the query text is too long, I truncate it and show ellipses. Eg: "My very long query is..."</p>

<p>Currently, I truncate after a certain number of characters. Since the font is not monotype, a query of all I's is more narrow than a query of all W's. I'd like them to all be about the same width prior to the ellipses. Is there a way to get the approximate width of the resulting string so that the ellipses for any given string will occur in about the same number of pixels from the beginning? Does CSS have a way? Does PHP? Would this be better handled by JavaScript?</p>

[{'answer_id': 33899, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>Does CSS have a way?</p>\n</blockquote>\n\n<p>No</p>\n\n<blockquote>\n  <p>Does PHP?</p>\n</blockquote>\n\n<p>No</p>\n\n<p>-</p>\n\n<p>To do that you\'d have to get the font metrics for each character, and apply them to all your letters in your string. While you could do this by using a drawing/rendering library like ImageMagick on the server, it wouldn\'t really work because different browser on different OS\'s render fonts differently.</p>\n\n<p>Even if it did work, you wouldn\'t want to do it, because it would also take forever to render. Your server would be able to push 1 page per second (if that) instead of several thousand.</p>\n\n<p>If you can live without the trailing ..., then you can nicely fake it using <code>div</code> tags and css <code>overflow: hidden</code>, like this:</p>\n\n<pre><code>.line_of_text {\n    height:1.3em;\n    line-height:1.3em;\n    overflow:hidden;\n}\n\n&lt;div class="line_of_text"&gt; Some long text which will arbitrarily be cut off at whatever word fits best&lt;/div&gt;\n</code></pre>\n'}, {'answer_id': 33919, 'author': 'Robert Groves', 'author_id': 3534, 'author_profile': 'https://Stackoverflow.com/users/3534', 'pm_score': 4, 'selected': True, 'text': '<p>Here\'s another take on it and you don\'t have to live without the ellipsis!</p>\n\n<pre><code>&lt;html&gt;\n&lt;head&gt;\n\n&lt;style&gt;\ndiv.sidebox {\n    width: 25%;\n}\n\ndiv.sidebox div.qrytxt {\n    height: 1em;\n    line-height: 1em;\n    overflow: hidden;\n}\n\ndiv.sidebox div.qrytxt span.ellipsis {\n    float: right;\n}\n&lt;/style&gt;\n\n\n&lt;/head&gt;\n\n&lt;body&gt;\n\n&lt;div class="sidebox"&gt;\n    &lt;div class="qrytxt"&gt;\n        &lt;span class="ellipsis"&gt;&amp;hellip;&lt;/span&gt;\n        Some long text which will arbitrarily be cut off at whatever word fits best but will have an ellipsis at the end.\n    &lt;/div&gt;\n    &lt;div class="qrytxt"&gt;\n        &lt;span class="ellipsis"&gt;&amp;hellip;&lt;/span&gt;\n        Some more long text which will arbitrarily be cut off at whatever word fits best but will have an ellipsis at the end.\n    &lt;/div&gt;\n    &lt;div class="qrytxt"&gt;\n        &lt;span class="ellipsis"&gt;&amp;hellip;&lt;/span&gt;\n        Short text. Fail!\n    &lt;/div&gt;\n&lt;/body&gt;\n\n&lt;/html&gt;\n</code></pre>\n\n<p>There is one flaw with this, if the text is short enough to be fully displayed, the ellipses will still be displayed as well.</p>\n\n<p>[EDIT: 6/26/2009]</p>\n\n<p>At the suggestion of Power-Coder I have revised this a little. There are really only two changes, the addition of the <code>doctype</code> (see notes below) and the addition of the <code>display: inline-block</code> attribute on the <code>.qrytxt</code> DIV. Here is what it looks like now...</p>\n\n<pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;style&gt;\n        div.sidebox \n        {\n            width: 25%;\n        }\n\n        div.sidebox div.qrytxt\n        {\n            height: 1em;\n            line-height: 1em;\n            overflow: hidden;\n            display: inline-block;\n        }\n\n        div.sidebox div.qrytxt span.ellipsis\n        {\n            float: right;\n        }\n&lt;/style&gt;\n&lt;/head&gt;\n\n&lt;body&gt;\n    &lt;div class="sidebox"&gt;\n        &lt;div class="qrytxt"&gt;\n            &lt;span class="ellipsis"&gt;&amp;hellip;&lt;/span&gt;\n            Some long text which will arbitrarily be cut off at whatever word fits best but will have an ellipsis at the end.\n        &lt;/div&gt;\n\n        &lt;div class="qrytxt"&gt;\n            &lt;span class="ellipsis"&gt;&amp;hellip;&lt;/span&gt;\n            Some more long text which will arbitrarily be cut off at whatever word fits best but will have an ellipsis at the end.\n        &lt;/div&gt;\n\n        &lt;div class="qrytxt"&gt;\n            &lt;span class="ellipsis"&gt;&amp;hellip;&lt;/span&gt;\n            Short text. FTW\n        &lt;/div&gt;\n    &lt;/div&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>Notes:</p>\n\n<ul>\n<li><p>Viewed in IE 8.0, Opera 9, FF 3</p></li>\n<li><p>A <code>doctype</code> is required for IE to get the <code>display: inline-block</code> to work correctly.</p></li>\n<li><p>If the <code>.qrytxt</code> DIV\'s overflow occurs on a long word, there is going to be a wide gap between the ellipsis and the last visible word.  You can see this by viewing the example and resizing your browser width in small increments. (this probably existed in the original example as well, I just may have not noticed it then)</p></li>\n</ul>\n\n<p>So again, an imperfect CSS-only solution.  Javascript may be the only thing that can get the effect perfect.</p>\n\n<p>[EDIT: 6/27/2009]</p>\n\n<p>Here is another alternative which uses browser specific extensions.</p>\n\n<pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;\n\n&lt;html&gt;\n&lt;head&gt;\n    &lt;style&gt;\n        div.sidebox \n        {\n            width: 26%;\n        }\n\n        div.sidebox div.qrytxt\n        {\n            height: 1em;\n            line-height: 1em;\n            overflow: hidden;\n            text-overflow:ellipsis;\n            -o-text-overflow:ellipsis;\n            -ms-text-overflow:ellipsis;\n            -moz-binding:url(ellipsis-xbl.xml#ellipsis);\n            white-space:nowrap;\n        }\n    &lt;/style&gt;\n&lt;/head&gt;\n\n&lt;body&gt;\n    &lt;div class="sidebox"&gt;\n        &lt;div class="qrytxt"&gt;\n            Some long text which will arbitrarily be cut off at whatever word fits best but will have an ellipsis at the end.\n        &lt;/div&gt;\n\n        &lt;div class="qrytxt"&gt;\n            Some more long text which will arbitrarily be cut off at whatever word fits best but will have an ellipsis at the end.\n        &lt;/div&gt;\n\n        &lt;div class="qrytxt"&gt;\n            Short text. FTW\n        &lt;/div&gt;\n    &lt;/div&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>Note that in order for the above example to work, you must create the xml file referenced by the -moz-binding rule, <strong><em>ellipsis-xbl.xml</em></strong>. It\'s should contain the following xml:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;\n  &lt;bindings xmlns="http://www.mozilla.org/xbl" xmlns:xbl="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"&gt;\n    &lt;binding id="ellipsis"&gt;\n      &lt;content&gt;\n        &lt;xul:window&gt;\n          &lt;xul:description crop="end" xbl:inherits="value=xbl:text"&gt;&lt;children/&gt;&lt;/xul:description&gt;\n        &lt;/xul:window&gt;\n      &lt;/content&gt;\n    &lt;/binding&gt;\n  &lt;/bindings&gt;\n</code></pre>\n'}, {'answer_id': 33942, 'author': 'Jiaaro', 'author_id': 2908, 'author_profile': 'https://Stackoverflow.com/users/2908', 'pm_score': 2, 'selected': False, 'text': "<p>@Robert</p>\n\n<p>what if you put the ellipses in a div with a low <code>z-index</code> so that when it moves to the left (for shorter lines) they get covered up by a background image or something?</p>\n\n<p>it's pretty hacky I know, but hey worth a try right?</p>\n\n<p><strong>edit</strong> Another idea: determine the position of the div containing the ellipses with javascript and if it's not pushed all the way right, hide it?</p>\n"}, {'answer_id': 43472, 'author': 'Evil Andy', 'author_id': 4431, 'author_profile': 'https://Stackoverflow.com/users/4431', 'pm_score': 2, 'selected': False, 'text': '<p>You could also quite easily use a bit of javascript:</p>\n\n<pre><code>document.getElementByID("qrytxt").offsetWidth;\n</code></pre>\n\n<p>will give you the width of an element in pixels and even works in IE6. If you append a span containing ellipses to the end of each query a simple logical test in JavaScript with a bit of CSS manipulation could be used to hide/show them as needed.</p>\n'}, {'answer_id': 1047647, 'author': 'joebert', 'author_id': 128357, 'author_profile': 'https://Stackoverflow.com/users/128357', 'pm_score': 2, 'selected': False, 'text': '<p>PHP should be left out of consideration completely due to the fact that even though there is a function designed for measuring fonts, <a href="http://www.php.net/imageftbbox" rel="nofollow noreferrer">http://www.php.net/imageftbbox</a> , there is no way for PHP to know whether the visitor has a minimum font size setup that is larger than your anticipated font size.</p>\n'}]
['https://Stackoverflow.com/questions/33893', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/356/']