HTML pre no line wrap border

10,512

Solution 1

Try adding display: inline to your pre tag CSS.

Solution 2

Use:

<pre style="display: inline-block; border:2px solid Black;">

Solution 3

For displaying code you want to use

pre { white-space:pre-wrap }

And for normal text you may want to use

pre { white-space:pre-line }

pre-line makes from multiple white space characters one space character, pre-wrap doesn't.

Now if you add a border it will be around the entire text, and the text will be wrapped.

pre {
    white-space:pre-line;
    border:2px solid black
}

Or didn't you ask for this?

Share:
10,512
Entity
Author by

Entity

Updated on June 29, 2022

Comments

  • Entity
    Entity almost 2 years

    I have a pre tag with some code in it that doesn't line wrap. I want to put a border around it (border:1px solid Black;), but the border is limited to the width of the browser, regardless of the length of the text. How can I force the border to always extend to contain all the text in the pre tag?

    Here's the code I'm using:

    <pre style="border:2px solid Black;">@Model.Code()</pre>
    
  • Entity
    Entity almost 13 years
    That puts a border around every line of text
  • Entity
    Entity almost 13 years
    I'll go ahead and mark this as answer; I just needed to use display: inline-block;
  • Entity
    Entity almost 13 years
    Ah. I forgot to mention there were multiple lines in the pre.