Skip to content Skip to sidebar Skip to footer

How To Apply A Line Wrap/continuation Style And Code Formatting With Css

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 unambig

Solution 1:

Here is one (unpleasant) way of doing this. It requires a number of bad practices. But SO is about solutions to real problems so here we go...

First each line needs to be wrapped in some sort of containing block. Span or p are probably the most appropriate.

Then the style of the containing block needs to have line height set. and a background image that contains a number of newLine glyphs at the start of every line except the first one. As this is code it would be resonable to expect it to not wrap more than 5 times. So repeating 5 times is probably enoygh.

This can then be set as the background image, and should display at the beginning of every line except the first one. I guess the resulting CSS might look like this:

p.codeLine
{
    font-size: 12px;
    line-height: 12px;
    font-family: Monospace;
    background: transparent url(lineGlyph) no-repeat 012px; /* move the background down so it starts on line 2 */padding-left: 6px; /* move the text over so we can see the newline glyph*/
}

Solution 2:

This cannot be done with CSS. Sorry. :(

Post a Comment for "How To Apply A Line Wrap/continuation Style And Code Formatting With Css"