title: How to apply a line wrap/continuation style and code formatting with css
body: <p>When presenting preformatted text on the web (e.g. code samples), line wrapping can be a problem. You want to wrap for readability without scrolling, but also need it to be unambiguous to the user that it is all one line with no line break.</p>
  
  <p>For example, you may have a really long command line to display, like this:</p>
  
  <pre><code>c:\Program Files\My Application\Module\bin\..&gt; Some_really_long_command line "with parameters" "that just go on and on" " that should all be typed on one line" "but need to be wrapped for display and I'd like the text style to indicate that it has wrapped"
  </code></pre>
  
  <p>(Stackoverflow forces a line like this not to wrap.)</p>
  
  <p>Is there a way of styling with CSS to give the same treatment as you see in books? i.e. to wrap the line, but include an image or glyph that indicates a line continuation. </p>
  
  <p>Obviously I am looking for a style that can be applied to all text, and let the browser's XHTML/CSS rendering engine figure out which lines have wrapped and therefore need the special treatment.</p>
  
  <h1>The Solution so far..</h1>
  
  <h2>Adding line continuation glyphs</h2>
  
  <p>Thanks to Jack Ryan and Maarten Sander, have a reasonably workable solution to add continuation glyphs to either the left or right of wrapped lines. It requires an image with repeating glyphs in the vertical, which is offset so that it is invisible if only one unwrapped line. The main requirement of this technique is that every line needs to be within a block (e.g. p, span or div). This means it cannot easily be used manually with existing text that is just sitting in a pre block.</p>
  
  <p>The fragment below summarises the essential technique. I posted a live example <a href="http://blog.tardate.com/2008/12/code-formatting-and-line-continuations.html" rel="nofollow noreferrer">here</a>.</p>
  
  <pre><code>.wrap-cont-l {
    margin-left: 24px;
    margin-bottom: 14px;
    width: 400px;
  }
  
  .wrap-cont-l p {
    font-family: Courier New, Monospace;
    font-size: 12px;
    line-height: 14px;
    background: url(wrap-cont-l.png) no-repeat 0 14px; /* move the background down so it starts on line 2 */
    text-indent: -21px;
    padding-left: 14px;
    margin: 0 0 2px 7px;
  }
  
  .wrap-cont-r {
    margin-left: 24px;
    margin-bottom: 14px;
    width: 400px;
  }
  
  .wrap-cont-r p {
    font-family: Courier New, Monospace;
    font-size: 12px;
    line-height: 14px;
    background: url(wrap-cont-r.png) no-repeat right 14px; /* move the background down so it starts on line 2 */
    text-indent: -28px;
    margin: 0 0 2px 28px;
    padding-right: 14px;
  }
  </code></pre>
  
  <p>To be used like this:</p>
  
  <pre><code>&lt;div class="wrap-cont-l"&gt;
    &lt;p&gt;take a long line&lt;/p&gt;
    &lt;p&gt;take a long line&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="wrap-cont-r"&gt;
    &lt;p&gt;take a long line&lt;/p&gt;
    &lt;p&gt;reel him in&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <h2>But wait, there's more!</h2>
  
  <p>I recently discovered <a href="http://code.google.com/p/syntaxhighlighter" rel="nofollow noreferrer">syntaxhighlighter</a> by Alex Gorbatchev. It is a fantastic tool for dynamically and automatically formatting text blocks. It is principally intended for syntax highlighting code, but could be used for any text. In v1.5.1 however, it does not wrap lines (in fact it forces them not to wrap).</p>
  
  <p>I did a little hacking around though, and was able to add a simple line wrap option syntaxhighlighter and also incorporate the continuation glyph idea. </p>
  
  <p>I've added this to the <a href="http://blog.tardate.com/2008/12/code-formatting-and-line-continuations.html" rel="nofollow noreferrer">live examples</a> and included a few notes on the hacks required (they are trivial). So with this as the text in the page:</p>
  
  <pre><code>&lt;textarea name="code" class="java:wraplines" cols="60" rows="10"&gt;
  public class HelloWorld {
  
    public static void main (String[] args)
  
    {
  
      System.out.println("Hello World! But that's not all I have to say. This line is going to go on for a very long time and I'd like to see it wrapped in the display. Note that the line styling clearly indicates a continuation.");
  
    }
  
  }
  &lt;/textarea&gt;
  </code></pre>
  
  <p>This is a snapshot of the formatted result:</p>
  
  <p><a href="http://tardate.com/syntaxhighlighter/line-continuation-example.jpg" rel="nofollow noreferrer">screenshot http://tardate.com/syntaxhighlighter/line-continuation-example.jpg</a></p>