codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » HTML » Article

HTML <PRE> tag

Article by:  Jeff Anderson  ( 1362 ) (4/26/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: No need to reformat text files with line breaks and tabs if you use the pre tag
Viewed: 87762 times Rating (103 votes): 
 3.6 out of 5
 Rate this Article  Read Comments  Post Comments

HTML <PRE> tag



Say you have a text file - perhaps you're a guitarist and you've got a guitar tab in text format, or perhaps you have a text-based email document you want to put quickly online. Do you have to go through all the bother of adding <BR> and spacing it all out correctly?

The simple solution is to use the <PRE> tag which maintains a text format - line breaks and tabs without you having to change anything. Simply wrap the tag around the text as shown:

<pre>

text file stuff here
complete with line breaks and things.

</pre>


A simple trick which took me many years as a web programmer to discover!





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'HTML <PRE> tag'
Posted by :  Archive Import (Brenden) at 00:30 on Friday, February 14, 2003
The only problem with the pre tag is the inability to format the font or any other attributes. I am sure I had found a way around this a long time ago but now nothing I try seems to work. If anyone has worked out a way to use the pre tag with selected fonts please post a reply.
Posted by :  Archive Import (ben) at 10:16 on Wednesday, February 19, 2003
To use different fonts (and other formatting, you have to use in-line styles.

eg

<PRE STYLE="font-size: small; font-family:'comic sans ms'">

p.s. always use the comic sans font when doing this because it looks so professional - but fun!
Posted by :  Archive Import (Leo) at 04:39 on Friday, February 21, 2003
Simply put, it does not work. If you use comic sans you lose the monospace font... It could still be used for keeping white spaces and linebreak, but it cannot be used for other purposes (guitar tabs and ASCII art are the first that comes into my mind)

However I would rather use a defined style with alla attributes instead of using <PRE>.
Posted by :  Archive Import (John Palmer) at 19:38 on Sunday, March 02, 2003
I control font size by putting:

<PRE>
<H5><TT>
---white space preserved text,
font controlled size---
</TT></H5>
</PRE>

I use this code to put typed family trees on my genealogical website. It seems to work with both Explorer and Netscape.
Posted by :  Archive Import (Joey) at 07:47 on Friday, March 21, 2003
Is it possible to make it wrap the text?
Posted by :  Archive Import (Cozzo) at 17:41 on Friday, March 21, 2003
Is it possible that 2 ppl had the exact same thought on the exact same day?

Posted by : Joey at 07:47 on Friday, March 21, 2003
"Is it possible to make it wrap the text?"

I'm looking for the answer to that exact same question... any one?
Posted by :  Archive Import (dave) at 07:42 on Sunday, March 23, 2003
apparently you can try
<p style="white-space: -pre-wrap">text here</p>

or

<p style="white-space: -moz-pre-wrap">text here</p>
but I think they will only work in netscape. Otherwise, no joy I'm afraid.
Posted by :  Archive Import (Hugo) at 11:36 on Wednesday, April 02, 2003
In Netscape <pre wrap="on" class="preClass"> works well but can't find a method for IE to wrap text..
Posted by :  Archive Import (Gavin) at 11:15 on Tuesday, April 08, 2003
Is there any way to turn wrapping off all together in the body of the page?
Posted by :  Archive Import (Perogi) at 14:52 on Monday, April 21, 2003
Another problem with pre is that it will always leave a white space at the front of the first line (per HTML specification).
Otherwise, it is an excellent tag to use to keep formatting intact.
Posted by :  Archive Import (Richard) at 18:57 on Wednesday, April 23, 2003
You can use stylesheets and just refernce a class in the PRE tag

ex:

<pre class='body'>text with
new lines
in it
</pre>
Posted by :  Archive Import (Seth) at 20:59 on Saturday, April 26, 2003
is there anything wrong with using the <pre> tag? I have searched many places and not many people use this tag, why? and also, is the <pre> tag handled by all browsers? if it is possible can you please email me with an answer, thank you.
Posted by :  Archive Import (das Megabyte) at 10:56 on Thursday, May 08, 2003
People don't use the <pre> tag because it offers no display or layout guarantees. Unless it's used as the last element to the right in a left aligned layout, there's a chance that a really long line won't break properly.

Furthermore, it relies on the browser to recognize your file's break type. This doesn't sound like a big deal, but different browsers take different breaks, and different machines use different break mechanisms. The end result is unpredictability.

That said, <pre> is also a wonderful layout tool for small HTML files. I'm not just talking text art...i've used it to perform uniform "tabs" and even to layout images. Plus it's been around since at least HTML 3.2, so you're likely to have a <pre> based layout look identical in every browser you can find.
Posted by :  Archive Import (Kent) at 15:40 on Thursday, May 15, 2003
I'm trying to make it quote (in a Help Page) the word 'script' within angle brackets... So far no luck, it assumes I am creating a script, naturally...
Posted by :  Archive Import (James) at 22:50 on Friday, May 16, 2003
To make it quote a left angle bracket, instead of typing the angle bracket, type the ampersand symbol (located above the 7 on the keyboard) and then lt;
Similarly, to quote a right angle bracket, use the ampersand symbol and then gt;
So it would look like (and)lt; or (and)gt; where (and) is the ampersand symbol
I'm not going to actually type the code because I'm not sure if it will replace it on the page or not. But this is how to print an angle bracket to the text of a web page.
Posted by :  Archive Import (Guru) at 10:35 on Thursday, June 12, 2003
how to print the html format data in the textarea?

Like <b>codetoad</b>
<i>codetoad></i>
It must print like bolded codetoad and italic codetoad
Posted by :  Archive Import (Samuel) at 15:21 on Thursday, July 03, 2003
This was a very useful article, it helped me alot, now I dont have to go thru all of that hard stuff, I can use the pre tag for alot of stuff!
Posted by :  Archive Import (Me) at 21:03 on Wednesday, September 03, 2003
Can you make fancy html buttons for a school website????
Posted by :  sajet at 02:37 on Thursday, July 15, 2004

Whenver i use the <pre> tag, it works fine but seems to put a newline after </pre>
Is this a normal behaviour? any work around technic for this?

Posted by :  gkmone at 08:00 on Saturday, July 30, 2005
I am using <PRE> tag in my application and the problems which i face are as follows:

1) First line starts from the center of the page.

2) No wrapping of the text inside this tag.

Please Anyone provide me a useful SOLUTION.
Posted by :  Gnorty at 05:07 on Monday, October 03, 2005
Should you wish to ever display html code directly to the screen in fixed face font, the &tl;xmg> does the trick nicely. Not sure how it goes with wrapping text though.

One word of caution -

you MUST remember to close the tag when you are finished, or all the HTML for the rest of the page will be displayed as source code, which is probably NOT what you wanted, and can be very unpleasant in some circumstances.


a safer way, albeit more long-winded is to use \< and \> in place of < and >

i.e. &lt;script&gt; will show as <script>

<Added>

whoops!!

the tag is of course <xmp>

the safer way is to use i.e. &lt; and &gt; instead of < and >
Posted by :  4u2cnnv at 03:07 on Wednesday, February 01, 2006
if any one still cares i managed to do this for a textarea that send text a an html format in an email

<textarea id="textarea1" col="80" wrap="hard|soft" ...></textarea>

Response.Write "<pre>" & trim(textarea1.text) & "</pre>"


================

so sumthing like this :

[b]"hello world garbage this is a silly the fox jumped over the lazy dog kinda stuff

best regards

4u2cnnv"[/b]

in this way

[b]"hello world garbage this is a silly the fox jumped over the lazy
dog kinda stuff

best regards

4u2cnnv"[/b]

and not

[b]"hello world garbage this is a silly the fox jumped over the lazy dog kinda stuff best regards 4u2cnnv"[/b]


- bye bye -
Posted by :  cybersprinters at 04:04 on Tuesday, July 15, 2008
I was very much pleased to understand the <pre> tag from this post...

and i was able to format the text using CSS tags.

<pre class="MyStyle">
your text
</pre>

and it worked in FF3 and IE7.
Thanks a lot fellas....


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
Image Submit Button
by Jeff Anderson
You can keep your forms from slipping into mundanity by substituting the default gray submit button to a colourful graphic of your choosing.
Parent _self _blank and _top Frame Target Specifications
by Jeff Anderson
How to specify frames and how to break out of them using standard link tags.
Pre Tag
by Jeff Anderson
No need to reformat text files with line breaks and tabs if you use the pre tag
Image Rollovers
by Jeff Anderson
A bit of code to swap to different images on the page. You often see this on sites to display a rollover on the navigation buttons.
ASCII characters and their codes
by Jeff Anderson
A full list of all the ASCII characters.
In-Line Frames (IFrames)
by Jeff Anderson
Inline frames or IFrames are the ones which appear in the midst of a standard page, but actually reference a totally separate url.
Validate Date
by Kannadasan Ramiah
Validating Date entered in a text box, pure HTML page
HTML Hspace and Vspace Image Borders
by Jeff Anderson
A lesser known feature in HTML is the ability to add a blank border around images both horizontally and vertically.
Creating Navigation with Frames
by Jeff Anderson
The most popular use of frames is to allow a static navigation to stay on one part of the screen (in one frame) while the content changes in another.
The OSI Reference Model - A Clear and Concise Illustration !
by James Opiko
The OSI Reference Model - A Resource For IT Certification Candidates & Networking Professionals








Recent Forum Threads
• Significant Factors
• Perl array access
• Re: huffman encoding and decoding in C++...
• Perl One Liner: Replace {(
• Re: html including php, accessing the functions
• Something like an INI editor or a DelimitedText-Editor
• Error Deleting File or Folder
• Re: How can use ASP.NET RegularExpressionValidator for alphanumeric 10 digit input
• Re: drop-down menu selected value


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009