views:

21

answers:

2

I'm showing code in an ordered list, so that it displays with line numbers. Each li has a pre tag filled with a line of code in it.

My problem is that when I set the width of the ol, and I allow the overflow to scroll, the background color of the overflow is different than the background color of the area originally showing.

The background of the ol has to be a different color than the background of the lis to make the line numbers stand out.

Is there something wrong with my CSS, why is this happening? How can I have scrolling, and different background colors for line numbers and code?

HTML:

<ol class="code">
  <li>
    <pre>Hello World! This is a long line that you have to scroll to see.</pre>
  </li>
  <li>
    <pre>This is the second line that you have to scroll to see.</pre>
  </li>
</ol>

CSS:

  ol {
    width:200px;
    background-color:#CFCFCF; }

  pre {
    color:#FFF;
    background-color:#000; }

  .code {
    overflow:scroll; }

Example page

The problem:

alt text

+1  A: 

This is the correct behaviour. This is because when text overflows out of an element (in this case, the pre) the box does not grow with it.

Delan Azabani
+1  A: 

To get around this width problem, you can set your pre tag to display: inline. Then, I simply added some extra padding on the li tag to retain the same look.

Full example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title></title>
    <style type="text/css">
        ol
        {
            width: 200px;
            background-color: #CFCFCF;
        }
        pre
        {
            color: #FFF;
            background-color: #000;
            display: inline;
        }
        .code li
        {
            margin: 12px 0px 12px 0px;
        }
        .code
        {
            overflow: scroll;
        }
    </style>
</head>
<body>
    <ol class="code">
        <li>
            <pre>World! This is a long line that you have to scroll to see.</pre>
        </li>
        <li>
            <pre>This is the second line that you have to scroll to see.</pre>
        </li>
    </ol>
</body>
</html>
wsanville