473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

get text width

Is there a way to know the overall width of a piece of text? Not the
width of, say, a single line P tag but any words it may contain.

I've been given a problem where text that is dynamically created is
supposed to be neatly wrapped in a styled DIV.

Andrew Poulos
Aug 25 '06 #1
4 5060


Andrew Poulos wrote:
Is there a way to know the overall width of a piece of text? Not the
width of, say, a single line P tag but any words it may contain.

I've been given a problem where text that is dynamically created is
supposed to be neatly wrapped in a styled DIV.
The browser and its HTML rendering engine will do the neat wrapping of
any contents the div contains. Or what exactly are you trying to achieve
that the browser does not do for your?

I don't really understand what you are looking for but besides the cross
browser offsetWidth/offsetHeight properties of element nodes IE/Win has
some special objects to find the bounding box of text, see the example
<http://msdn.microsoft.com/workshop/samples/author/dhtml/refs/rectdemo.htm>
and the documentation
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/textrectangle.asp>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 25 '06 #2
On Friday 25 August 2006 13:44, Andrew Poulos [ap*****@hotmail.com] wrote in
message <44**********************@per-qv1-newsreader-01.iinet.net.au>
Is there a way to know the overall width of a piece of text? Not the
width of, say, a single line P tag but any words it may contain.

I've been given a problem where text that is dynamically created is
supposed to be neatly wrapped in a styled DIV.
Do you have an example where the text isn't neatly wrapped? Unless you've
got very long words, a large text size and very little space there
shouldn't be a problem wrapping text.

The following CSS properties can be used for wrapping text.

text-align:left
should wrap text leaving the right 'ragged-right', this is usually the
default.

text-align:justify
should pad out the word spacing so that the left and right edges of the text
are all neatly aligned.

eg:
<p style="text-align:justify">Lorem Ipsum etc...</p>
Aug 25 '06 #3
Andrew Poulos wrote:
Is there a way to know the overall width of a piece of text?
Not a good way, and generally you shouldn't want to do this. But if
necessary you can put the text into something that will "shrink wrap"
it, such as a float or an absolutely-positioned element, and then find
that element's offsetWidth. (IIRC, offsetWidth was invented by
Microsoft and is non-standard, but is universally supported.)

Phil.
Aug 25 '06 #4
Martin Honnen wrote:
>

Andrew Poulos wrote:
>Is there a way to know the overall width of a piece of text? Not the
width of, say, a single line P tag but any words it may contain.

I've been given a problem where text that is dynamically created is
supposed to be neatly wrapped in a styled DIV.

The browser and its HTML rendering engine will do the neat wrapping of
any contents the div contains. Or what exactly are you trying to achieve
that the browser does not do for your?

I don't really understand what you are looking for but besides the cross
browser offsetWidth/offsetHeight properties of element nodes IE/Win has
some special objects to find the bounding box of text, see the example
<http://msdn.microsoft.com/workshop/samples/author/dhtml/refs/rectdemo.htm>
and the documentation
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/textrectangle.asp>

Hmm, when I get back in the office on Monday I'll recheck what I've
done. I have a coloured box that is supposed to shrink wrap around the
text. Initially, I think, I put a width value of 'auto' and didn't get
shrink-wrapping so I started looking for a way to code it.

Andrew Poulos
Aug 27 '06 #5

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

Similar topics

4
by: j.t.w | last post by:
Hi All. I'm having a problem with my Date of Birth textbox. When I open the ..htm file, the "DoB" textbox is flat with a border. All of my other textboxes are sunken and are yellow. When I...
8
by: BiNZGi | last post by:
Hi I have reduced the problem to this code: <form> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td><input type="text" style="width: 100%;" value="Lorem ipsum dolor...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
2
by: george.leithead | last post by:
Hi all, I have a very strange problem! In following Web page (which is generated from a CMS System), the navigation to the left 'dissapears' when you roll the mouse over the links? It does not...
12
by: tim | last post by:
I am using foldoutmenu 3 and am having problems with viewing my menus in firefox. On my sub3 menus i have more than one line of text in some places. firefox does not recognise that there is more...
3
w33nie
by: w33nie | last post by:
I want to disable the text boxes, captain_name and captain_email, but only if the radio button, captain_guarantee, has NOT been checked. how do i do this? <form name="formTeamApplication"...
4
by: pablorp80 | last post by:
Hello, Here is what I need: I need the focus and the cursor set to a textbox named txtGT, every time no matter if it is the first page load or whether it is a postback. Here is the problem: I...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
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...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.