Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<pre> tag in HTML with fixed width

Tags:

html

css

tags

I'm using the <pre> tag to display preformatted text (including line breaks, spaces and tabs etc.) But large lines without line-breaks are shown in one line and a scroll bar is added.

I want to limit the width of the <pre> tag (such that large lines are broken up to come to new lines and no scroll is required. Is this possible or is there some other tag that I can use?

Code is something like:

$.post("contr.php", q1, function(data) {     $("#el_text").html("< pre>"+data+"< /pre>"); }); 
like image 910
Edward Wong Hau Pepelu Tivrusk Avatar asked Oct 27 '09 23:10

Edward Wong Hau Pepelu Tivrusk


People also ask

How do you style a pre tag in HTML?

The <pre> tag in HTML is used to define the block of preformatted text which preserves the text spaces, line breaks, tabs, and other formatting characters which are ignored by web browsers. Text in the <pre> element is displayed in a fixed-width font, but it can be changed using CSS.

How do I change font size in pre tag?

While the font-size cannot be changed for text directly inside pre tags, you can always wrap that text in another element (a span, for example) and change the font size of that element.

How do you break a line in pre tag?

The usual approach is to convert single newlines in the input to “<br />”. (Double-newlines would normally introduce a new “<p>” element.)

How do you wrap text in pre tag?

HTML <pre> tag defines preformatted text. It is used to display code blocks since it preserves spaces and line breaks. If the line is large, then the <pre> tag won't wrap it by default. To wrap it, we need to use CSS.


2 Answers

An exhaustive way of supporting it in almost all browsers:

pre {     white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */     white-space: -pre-wrap; /* Opera */     white-space: -o-pre-wrap; /* Opera */     white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation) http://www.w3.org/TR/css3-text/#white-space */     word-wrap: break-word; /* IE 5.5+ */ } 

I had the same problem not long ago and had found the solution here: http://codingforums.com/showthread.php?t=43293

like image 180
Karim Avatar answered Sep 30 '22 20:09

Karim


pre{     white-space:pre-wrap; } 

..does what you want in Firefox and Chrome - wraps the lines but preserves whitespace. But unfortunately IE doesn't seem to support it (although I haven't looked in IE8 yet).

Edit: IE8 supports it.

like image 27
Dan Avatar answered Sep 30 '22 21:09

Dan