Improve Site Performance Increase Site Traffic Monitor Site Uptime Webmaster Resources NetMechanic Home Looking For Help? Partner Programs Privacy Policy Contact Us Press Room
NetMechanic Home LOGIN | HELP | ABOUT US | PRODUCTS | SITE MAP
NetMechanic Menu
Over 52 Million Web Pages Tested!     
 
Positioning tips.
search engine optimization story search.
Search for:


Your Email:

I would like to receive my newsletter in:
HTML format
Text format


Increase traffic.
Volume 8 (2005)
   September
   June
   April
   March
   January

Volume 7 (2004)
   November
   September
   July
   June
   May
   April
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 6 (2003)
   December
   November (Part 2)
   November
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June (Part 2)
   June
   May (Part 2)
   May
   April (Part 2)
   April
   March (Part 2)
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 5 (2002)
   December (Part 2)
   December
   November (Part 2)
   November
   October (Part 2)
   October
   September (Part 2)
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June (Part2)
   June
   May (Part 2)
   May
   April (Part 2)
   April
   March (Part 2)
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 4 (2001)
   December (Part 2)
   December
   November (Part 2)
   November
   October (Part 2)
   October
   September (Part 2)
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June (Part 2)
   June
   May (Part 2)
   May
   April (Part 2)
   April
   March (Part 2)
   March
   February (Part 2)
   February
   January (Part 2)
   January

Volume 3 (2000)
   December (Part 2)
   December
   November (Part 2)
   November
   October (Part 2)
   October
   September (Part 2)
   September
   August (Part 2)
   August
   July (Part 2)
   July
   June
   May
   April
   March
   February
   January

Volume 2 (1999)
   December
   November
   October
   September
   July
   June
   May
   April
   March
   February
   January

Volume 1 (1998)
   December
   November
   October
   September

 

CSS Tip:
Adding Whitespace To Text

by Larisa Thomason,
Senior Web Analyst,
NetMechanic, Inc.

  
May 2003
Vol. 6, No. 9
 • Promotion Tip
 • HTML Tip
 • CSS Tip
  

There are just a few ways in HTML to control line breaks and spacing. The most common are break tags and the PRE tag. The PRE tag is easiest, but you can't control the text font or color. The CSS white-space property lets you style the text to match the rest of your page - but only in browsers that support it.

The best solution is to use CSS to add style to the PRE tag. It's not a perfect solution, but it's the most reliable one.

PRE Tag In HTML

The PRE tag is a great way to display preformatted text that needs specific spaces between lines, words, or even individual letters. That is, it's great if you don't mind giving up some control over page display.

Depending on your page layout and which browsers your visitors use, the loss of control may be considerable. That's because browsers have set ways (defaults) to display text inside a PRE tag.

For instance:

  • Fixed pitch fonts: Text inside a PRE tag always displays in the browser's default monopitch font, which is usually Courier. The rest of your page text may be in Arial or any other font you choose, but anything enclosed inside a PRE tag looks like somebody took a typewriter to it!

  • Default spacing: The PRE tag is a block-level element, so browsers place the same amount of space around them as around paragraph tags. Your preformatted text may end up farther away from the surrounding text than you wanted.

  • Horizontal scrollbars: If your preformatted text contains one or more long lines, the browser window stretches to accommodate them instead of wrapping the text. That may lead to unsightly horizontal scrollbars.

You can force the text to wrap using the WIDTH attribute. But this is where browser problems first appear. Explorer and even Opera ignore the WIDTH attribute, but Netscape 6 and 7 support it.

Here is         some text
              that  is
inside a PRE tag.

That's not the most attractive display. Let's see how to add some style to it!

The White-space Property In CSS

You can apply the white-space property to any block-level element. In Netscape and Opera, it's possible to control spacing and line breaks inside paragraphs, header tags, and other elements using the white-space property.

Unfortunately, Explorer 5.x browsers (for Windows) ignore this extremely useful technique. Explorer 6 only supports it if you use a strict DOCTYPE definition. If you use a frameset or transitional DOCTYPE, the browsers ignore the property entirely.

Include the white-space property in your style definition like this:

white-space: pre;

Use it with other style definitions to control whitespace and retain the same fonts and colors as the rest of the document. Here's a sample definition:

.preElement
{
white-space:pre;
font-weight:bold;
color:navy;
font-family:arial;
}

When you apply this class to a page element, you get bold, navy text, in Arial. It retains line breaks and word spacing.

But remember that Explorer browsers may ignore the white-space property. It's best to check your page using Browser Photo before you rely on CSS to control spacing. Browser Photo gives you screen shots of your page in 16 different browser and operating system combinations. You can quickly find and fix the errors.

Mixing CSS and PRE

The easiest way to handle the browser display problem is to add a style definition to your PRE tag. This works in all browsers.

Here's the same style definition we used in the previous section, but applied to the PRE tag instead of to a class:

pre
{
font-weight:bold;
color:navy;
font-family:arial;
}

The only downside is that you lose the ability to control the width - but Netscape is the only browser that supports that anyway! Even if you add a width property to the style definition, all browsers ignore it. They also ignore the next logical way to handle width: place the PRE tag inside a DIV tag that has the desired width defined. The DIV stretches to fit the PRE instead of controlling its width.

Check out this example page in different browsers to see these techniques in action.

Still, this mixture of tags is valid HTML and CSS. It lets you include preformatted text on your page and style it to match the rest of the page. Without CSS, your only other option would be to control the spacing using BR tags and non-breaking spaces ( ). That's a lot of extra code to add to your page - and every extra tag makes maintenance more difficult and errors more likely!



Rate This Tip:
Not Useful Useful Very Useful   
 
NetMechanic Tools
HTML Toolbox
Browser Photo
Server Check
Search Engine Starter
Search Engine Tools
GIFBot
Newsletter
HTML Tutorial and Tips
Search Engine Tutorial
Accessibility Information
Browser Problem Tutorial

Company Info
Products
About Us
Contact
Advertise
Link To Us
Jobs
Privacy Policy
Partner Programs
Press Room
RSS Feed
Support
 



Powered by Overture!

 
     
 
   
 
     


Keynote Home
Copyright © 1996-2007,
Keynote NetMechanic
All rights reserved.