Return to Kroll Design home page

Using the <br> tag within the <pre> tag

 
« Return

I created this tutorial to test what happens when you include a <br> tag within a <pre> tag, and why you might want to do this on occasion.


This has neither <pre> nor <br>:

line 1. line 2. line 3.

This has <pre> with no <br>:

line 1.
line 2.
line 3.

This has <br> with no <pre>:

line 1.
line 2.
line 3.

This has both <pre> and <br>:

line 1.
line 2.
line 3.

This has both <pre> and <br>, but to avoid the double-spacing of the previous example, I put the HTML on one continuous line:

line 1.
line 2.
line 3.

You may be wondering why in this example I don't just remove the <br> tag. There's times when I want the formatting which <pre> provides, such as leaving multiple blanks as is and not collapsing them to one blank, and when I want the code on one line because the code is a string in a JavaScript statement. JavaScript breaks if a literal spans multiple lines, because unlike most languages, a JavaScript statement can be terminated with either a semicolon OR A LINE BREAK. A lot of JavaScript statements end with both a semicolon and a line break, but that's actually not necessary -- you only need one or the other (but, yes, always including the semicolon is good programming practice). Hence the need to have HTML code in one continuous line if it's a JavaScript string. More recently, I've been using PHP to convert HTML that spans multiple lines into the one continuous line that JavaScript needs, converting the line breaks to <br> tags -- this way, my PHP / HTML source code is easier to maintain, but by time it gets fed to the client side it's in the format that the client-facing JavaScript needs.




 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 10/5/2021 4:09 AM