get text width

A

Andrew Poulos

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
 
M

Martin Honnen

Andrew said:
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/d...hor/dhtml/reference/objects/textrectangle.asp>
 
P

PDannyD

On Friday 25 August 2006 13:44, Andrew Poulos [[email protected]] wrote in
message said:
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>
 
A

Andrew Poulos

Martin said:
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/d...hor/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,134
Latest member
Lou6777736
Top