HTML
 tag causes linebreaks

后端 未结 9 583
轻奢々
轻奢々 2020-12-29 08:37

I\'m using CSS (via JQuery , but not relevant to this question) to highlight certain elements within an HTML file: I\'m using \"pre\" tags to separate out logical elements i

相关标签:
9条回答
  • 2020-12-29 09:33

    Why are you using jQuery for something that can be achieved via CSS?

    <html>
    <head>
        <style type="text/css">
        pre {
            display: block;
            padding: 0;
            margin: 0;
        }
        pre.error {
            background-color: red;
            color: white;
        }
        </style>
    </head>
    <body>
        <pre class="error">
    This is an error line.
        stack.trace.blah.blah
        more.blah.blah
        yadda.yadda.blah</pre>
        <pre class="ok">
    this is not an error line.it contains html
    &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;hello&lt;/body&gt;&lt;/html&gt;</pre>
        <pre class="error">
    This is an error line.
        stack.trace.blah.blah
        more.blah.blah
        yadda.yadda.blah</pre>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-29 09:34
    pre { margin: 0; }
    

    should give you the rendering in the second picture. Your snippet probably doesn't work because you don't remove the default margin from the pre.ok.

    0 讨论(0)
  • 2020-12-29 09:35

    You can fix with css as follow

    pre {
        width: 600px;                          /* specify width  */
        white-space: pre-wrap;                 /* CSS3 browsers  */
        white-space: -moz-pre-wrap !important; /* 1999+ Mozilla  */
        white-space: -pre-wrap;                /* Opera 4 thru 6 */
        white-space: -o-pre-wrap;              /* Opera 7 and up */
        word-wrap: break-word;                 /* IE 5.5+ and up */
    
        }
    
    0 讨论(0)
提交回复
热议问题