hd.
hd.

Reputation: 18306

White spaces not ignored in pre tag

I have read that the 'pre' tag will collapse all white spaces and tabs into one space, but it doesn't do so for me.

why is it so? is it depending on web browser or should I do something more?

Upvotes: 3

Views: 8517

Answers (2)

hayesgm
hayesgm

Reputation: 9096

From HTML 4.01 Specification

The PRE element tells visual user agents that the enclosed text is "preformatted". When handling preformatted text, visual user agents:

  • May leave white space intact.
  • May render text with a fixed-pitch font.
  • May disable automatic word wrap.
  • Must not disable bidirectional processing

PRE tags will leave white space as you have typed it. The purpose is just that. If you don't use a pre tag, standard HTML will collapse white space as you have written. Use PRE if you are interested in preserving white space, not collapsing it.

Here is an example in JSFiddle.

This will preserve white space:

 <pre>
    Spaces
     and more
      galore
 </pre>

This will collapse white space:

 <div>
   All
    together
     now
 </div>

Upvotes: 6

Caffeinatedwolf
Caffeinatedwolf

Reputation: 1307

The PRE tag will just keep you contents in the same format as you wrote it. it is used to keep your "preformatted" text the same way as you wrote it.

Upvotes: 0

Related Questions