spacer
Yehuda Shiran September 6, 2000
Converting a Long Word into a Multi-liner
Tips: August 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

It is sometimes very annoying to have to scroll horizontally when the a word is longer than the window's width. Starting from Internet Explorer 5.5, you can control the layout of a text in a container. The style's property you use is wordWrap:

object.style.wordWrap = sWordWrapMode;

where sWordWrapMode is one of two options:

  • none

  • break-word

The following paragraph (works only in IE 5.5 and up) demonstrates the wordWrap property. The user can click one of the two links following the paragraph, making it a multi-line (wordwarpped) or a single word (scrolling is needed) paragraph. Play around with the paragraph word wrapping behavior:

LongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWord

One Word Multi-Line

Here is how we defined the above paragraph and links:

<P ID="longWord" STYLE="word-wrap:break-word; width:100%; left:0">
LongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWor
dLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWo
rdLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongW
ordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLong
WordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLongWordLon
gWordLongWordLongWordLongWordLongWordLongWordLongWord
</P>
<A HREF="javascript:void(longWord.style.wordWrap='normal')">One Word</A>
<A HREF="javascript:void(longWord.style.wordWrap='break-word')">Multi-Line</A>

The wordWrap property applies only to elements that have layout. An element has layout when it is absolutely positioned, is a block element, or is an inline element with a specified height or width. Notice the width specification above to make it an element with layout. The links themselves are straight-forward. You just change the wordWrap property to the desired value. Notice the usage of void() above. It is necessary to avoid overwriting of the page with the return value from the javascript: call, which is the property assignment value (normal or break-word).


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business