Making preformated <pre> text wrap in CSS3, Mozilla, Opera and IE

From: http://myy.helia.fi/~karte/pre-wrap-css3-mozilla-opera-ie.html

 

Code snippets and poems often use spaces for indenting text. Pre-tag is used to display preformatted text on webpages. This makes indenting visible, but has the annoying side-effect of disabling wrapping. When wrapping is disabled, long lines go out of the right side of window, which is never usefull. This howto explains how you can make preformatted text wrap on all browsers when making websites. We also learn how to make pre on any website wrap while browsing the web with mozilla.

Copyright Tero Karvinen www.iki.fi/karvinen .

Contents of stylesheet pre-wrap.css :

/* Browser specific (not valid) styles to make preformatted text wrap */		

pre {
 white-space: pre-wrap;       /* css-3 */
 white-space: -moz-pre-wrap;  /* 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+ */
}

This web page includes the style sheet in head:

 <link rel="stylesheet" type="text/css" href="pre-wrap.css" />

All the code snippets on this page, such as the style sheet above, are inside <pre> tags. See how it wraps by resizing your browser window. (If you make browser window very small, less than 20 characters wide, text goes outside the window any way.)

Wrap pre on any page with Mozilla userContent.css

Edit $HOME/.mozilla/default/*.slt/chrome/userContent.css in your favourite editor. The file does not exist by default, you have to create it. Add

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

It is the same css as above with unrelated browsers removed and !important added. Important means that values in userContent are used instead those defined by stylesheets on websites. Close and open Mozilla. Try a page with pre but no css with its own. It should wrap, just like all pre-text in any website.

Links and bibliography

pmsyyz 2003-01-12 "I use this for IRC logs..." in Experts Exchange <pre> but not word wrap .

Akkana Peck: Customizing Mozilla .

Suignard (ed) / W3C: CSS3 Text Module - Candidate Recommendation 14 May 2003

Copyright, Notes, Todo

Todo: user stylesheet (!important) to make all pre in any page wrap in Mozilla. Test in Firefox.

Copyright 2004-09-12 Tero Karvinen www.iki.fi/karvinen . GNU Free Documentation License

Validate XHTML Basic 1.0

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值