tags:

views:

88

answers:

3

I have some code samples which I want to publish in an HTML document. I'm wrapping them with <code> tags but I'd like them to be styled such that line breaks are preserved. I can do this by also enclosing them with <pre> tags but I'd prefer to use CSS.

I've tried the following in IE7 (which according to this reference should work) but with no joy (line breaks are stripped):

code {
    white-space: pre;
}

Is this possible?

+2  A: 

Are you sure you're not doing something wrong? This code works for me on IE7:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
  <style type="text/css">
  code { white-space: pre; }
  </style>
</head>
<body>
  <code>
      function() {
          alert('yay');
      }
  </code>
</body>
</html>
Paolo Bergantino
Are your line breaks cr/lf (Windows) or lf (Unix)?
Thomas L Holaday
+1 The missing doctype was the problem (but I've accepted SpliFF's answer which was more explicit). Thanks!
Matthew Murdoch
@Thomas - they're Windows
Matthew Murdoch
+1  A: 

You could try using the CSS suggested here: http://ajaxian.com/archives/wrapping-the-pre-tag

Ian Oxley
+1  A: 

Check your doctype is valid and onr the first line. Maybe it's slipping into quirks mode?

SpliFF