473,770 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting line wrap and adding "..." to overflowed words

I have a div box with a border, and some padding left and right. It has
some text with different font sizes, and a few icons. I want to cut off
any overflow at the end of the line with "...", instead of the text
wrapping. What is the best way I can detect whether the line wrapped,
and how many characters to cut off at the end?

- Jason

Oct 13 '06 #1
5 6410
Jason wrote:
I have a div box with a border, and some padding left and right. It has
some text with different font sizes, and a few icons. I want to cut off
any overflow at the end of the line with "...", instead of the text
wrapping. What is the best way I can detect whether the line wrapped,
and how many characters to cut off at the end?
You can't. (Assuming that you are takling about HTML. JavaScript is
used in more areas than just web browsers.)

You might be able to hack the effect with overflow:hidden and a
right-aligned overlaid textbox with a white background and contents of
"...", but that won't stick to glyph boundaries. (And, this is a topic
for an HTML or CSS newsgroup.)

Oct 13 '06 #2
On 13 Oct 2006 10:39:54 -0700, "Phrogz" <ga***@refinery .comwrote:
>Jason wrote:
>I have a div box with a border, and some padding left and right. It has
some text with different font sizes, and a few icons. I want to cut off
any overflow at the end of the line with "...", instead of the text
wrapping. What is the best way I can detect whether the line wrapped,
and how many characters to cut off at the end?

You can't. (Assuming that you are takling about HTML. JavaScript is
used in more areas than just web browsers.)
Hmm, well you can... but not in a nice way on anything but IE (where
you don't need to as it supports the CSS to do it automatically)

(method is strip words until the scrollHeight of the overflow'd region
is low enough...)

Jim.
Oct 13 '06 #3

"Jim Ley" <ji*@jibbering. comwrote in message
news:45******** *******@news.in dividual.net...
On 13 Oct 2006 10:39:54 -0700, "Phrogz" <ga***@refinery .comwrote:
Hmm, well you can... but not in a nice way on anything but IE (where
you don't need to as it supports the CSS to do it automatically)
Someday, perhaps. The text-overflow property is in CSS3 working draft.
(method is strip words until the scrollHeight of the overflow'd region
is low enough...)
Not sure that would be doable with text of different sizes plus inline
images.

A more complex approach would be to position the div relative, put the text
line into a p with position: absolute, width: auto, visibility: hidden.
Follow that with a second, empty p with height: 0.

A window.onload function would store the original line in case of window
resize, then call another function to trim the line (the trim function would
be called on div.resize also).

The empty p.clientWidth is maxwidth. In the trim function, if p.clientWidth
maxwidth, append a span with textNode of '…' (ellipse character),
then delete words/images from previous siblings until p.clientWidth <=
maxwidth. Then make p visible.

nf
Oct 13 '06 #4
Jason wrote:
I have a div box with a border, and some padding left and right. It has
some text with different font sizes, and a few icons. I want to cut off
any overflow at the end of the line with "...", instead of the text
wrapping. What is the best way I can detect whether the line wrapped,
and how many characters to cut off at the end?

- Jason
Hmmm. Interesting.

You can prevent the line from wrapping using the white-space: nowrap;
directive in CSS. Then hide the overflow using overflow: hidden.

You can make a CSS class that has an ellipsis background image aligned
to the right bottom, along with enough padding-right to make it look
nice. You'll have to contain each line in its own element in order for
this to be effective.

You can check each line (each of which must be in its own element
according to your design parameters) by appending a dummy <spanto the
line element and grabbing the <span>'s offsetLeft. If the offsetLeft of
the <spanis greater than the clientWidth of the line element, it's
overflowed.

If it's overflowed, apply the CSS class and you get a (non-selectable)
ellipsis.

Jeremy
Oct 13 '06 #5
I wrote:
line into a p with position: absolute, width: auto, visibility: hidden.
Oops, forgot to mention white-space: nowrap.
Oct 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
6084
by: name | last post by:
Here is a first attempt at a line/word wrapping utility. Seems to work okay, but lacks some checking stuff, etc. --------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h>
25
6263
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do something;
5
8781
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not work. I am sure that this probably fairly simple, but I have not been able to figure it out. Z.K.
6
1165
by: Markus Ernst | last post by:
Hi Searching for a possibility to display some text with preserved white space and line breaks, but with long lines being wrapped, I found this CSS declaration, which I found helpful: pre { white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */
1
1466
by: mmurrell | last post by:
I am developing some dhtml code that displays a balloon style help when a user mouses over words. The balloon help is positioned correctly until the tag (in this case) word-wraps. Is there any way in JS to detect and get the correct coordinates? I have tried disabling word-wrap on the span tag in css, but the offsetTop + offsetHeight still points to the top of the word (the same place vertically it would have been positioned before the...
13
2702
by: Haines Brown | last post by:
I have a style for a list: dl { line-height: 1; margin-bottom: 1em; } But the W3C CSS validator says: Line: 124 Context : dl Invalid number : line-height Parse Error -
1
2055
by: Prabu | last post by:
Hi, I have a web site hosted on a W2k3 machine with IIS 6.0 installed. When ever i run a specific task from this website I get a 'The XSL processor stack has overflowed - probable cause is infinite template recursion' error message. I created a windows application with the same code and kicked of the transformation it worked fine. This makes me believe that there is some setting in the application pool's memory which can be tweeked to...
2
2250
by: Jason Carlton | last post by:
Sorry if this goes through twice, I had an error the first time that I submitted. I have a bit of text that has a varying length, and I'm wanting to float text to the right. The problem is that the text needs to wrap to 2 lines instead of 1, and I'm wanting to float the text to the right of the second line. Something like this:
7
4203
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without destroying html tags which wordwrap has a tendency of doing! )) What I want to do is add that line break so that it DOES force a line wrap - but I am not sure where to insert it in the function
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.