6/16/2007 ↓

CSS for code: Wrap long lines

Author: Mark Ghosh Category: Code, HOW-TO, WordPress

Thanks for visiting! We would like to serve you better. Please subscribe to our RSS feed for daily updates. This blog posts regular Wordpress news, updates of themes, plugins, ideas, hacks, quick fixes and everything about blogging, especially about Wordpress. You can also receive updates from this blog via email if you want that method of notification.

If you post code on your Wordpress blog, you could use a code beautifier plugin such as syntax highlighter or you could include your code in <pre> tags. They preserve line breaks, multiple blanks, tabs between words and other formatting commonly used in code. However, in some cases, if body of your blog is narrow(er) or your line of code is really long, it will not wrap and will overflow over your sidebar. You can just hide the overflow but that still will look tacky and not very usable. On performing a little search, I discovered that Tyler Longren had already come up with a clever hack to solve the problem. If you add the following code to your stylesheet, it will wrap the long lines of code.



pre {

    white-space: pre-wrap; /* css-3 */

    white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */

    white-space: -pre-wrap; /* Opera 4-6 */

    white-space: -o-pre-wrap; /* Opera 7 */

    word-wrap: break-word; /* Internet Explorer 5.5+ */

}

via Amit (via Lorelle)

1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.8 out of 5)
Loading ... Loading ...

Friends

Translate

Translate to German Translate to Spanish Translate to French Translate to Italian Translate to Portuguese Translate to Japanese Translate to Korean Translate to Russian Translate to Chinese

Latest Videos

13 Comments | Leave a comment | Comments RSS

  1. Wow. This is very helpful. I didn’t know Mozilla has a property for word wrapping. Thanks!

    [Reply] Dino Latoga (1 comments.) — 06/17/2007 @ 12:33 am
  2. valid CSS ?

    [Reply] Myo Kyaw Htun (8 comments.) — 06/17/2007 @ 2:54 am
  3. [...] went to Weblogtoolscollection (this is ping to their most resent article) and we wrote news article. Then we got BAN-ed and our [...]

    First BAN for Mu « Word&Press Mu — 06/17/2007 @ 4:23 am
  4. nice hack!

    [Reply] LcF (19 comments.) — 06/17/2007 @ 4:48 am
  5. interesting. i’ve always just applied a fixed width to pre, and then used overflow: auto; so that it scrolls and makes the code available, without overpowering the layout.

    [Reply] adam (39 comments.) — 06/17/2007 @ 9:10 am
  6. There’s a must more efficient way to present code: make sure lines don’t wrap, and give your code container an overflow property so that horizontal scrollbar gets in if needed. A line wrapping may be confusing when copying code.

    [Reply] Ozh (74 comments.) — 06/17/2007 @ 9:20 am
  7. [...] CSS for code: Wrap long lines [...]

    Around the web | alexking.org — 06/17/2007 @ 3:33 pm
  8. pre { overflow:auto; }

    [Reply] anieto2k (2 comments.) — 06/17/2007 @ 5:15 pm
  9. The overflow works too, although some people don’t want the scrollbar. I do agree that I’d rather have the scrollbar when viewing code though because line-wrapping can get a little messy with long lines of code.

    [Reply] Ronald Huereca (39 comments.) — 06/17/2007 @ 6:09 pm
  10. Very handy! Thanks for pointing that one out, I’ve had problems with cross-browser overflows before and wrote off ever finding a soultion to it

    [Reply] Keith (1 comments.) — 06/17/2007 @ 8:15 pm
  11. [...] Weblog Tools Collection » Blog Archive » CSS for code: Wrap long lines (tags: css code wrap) [...]

    links for 2007-06-18 | Patrick Kempf — 06/17/2007 @ 8:43 pm
  12. Nice tip.

    Overall, I think I prefer the scrollbars, but that’s a personal preference. It’s nice to know the option is out there. :)

    As for the commenter who said “make sure the lines don’t scroll”: How do you propose to do that? Considering people can resize the font, you can’t guarantee that unless you make the text and image.

    [Reply] Stephen Rider (10 comments.) — 06/19/2007 @ 12:21 pm
  13. Very useful, thanks a lot!!!

    [Reply] Shaun — 05/17/2008 @ 7:04 pm

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(required)

(required, will not be published)


S2