Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I adjust the width of a <pre> area to fit the text?

Tags:

css

I have the following:

<p>This is a test</p> <pre>public class Car {     protected Car() { }     protected Car(int speed) { }     protected void Car() { } }</pre> <p>Another line</p> 

and

pre {     font-family: monaco,consolas,"courier new",monospace;     font-size: 1em;     min-height: 3em;     overflow: auto;     padding: 1em;     xwidth: 80%;     background-color: red; } 

When the text displays the <pre> background goes the full width of the page. I have a demo here:

fiddle

What I would like is for the red background to stop just after the far most right character of my code. I don't want to see a big red area that extends from one side of the page to another.

Can someone tell me if it is possible to do this with CSS. I really am not sure as I cannot think what I can do.

Thanks

like image 434
Gordon Avatar asked Aug 20 '11 14:08

Gordon


People also ask

How do I make input width fit content?

Use the span. text to fit width of text, and let the input have same size with it by position: absolute to the container.

How do I wrap text in a 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.

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.


2 Answers

You can use display: inline-block;:

http://jsfiddle.net/hLVV9/1/

Although please check out the browser support, because it wouldn't surprise me if IE doesn't support it.

like image 171
PeeHaa Avatar answered Sep 22 '22 03:09

PeeHaa


The best solution so far :

pre {     white-space: pre-wrap; } 
like image 31
Dev Avatar answered Sep 25 '22 03:09

Dev