One of the most annoying errors someone can make on a webpage is incorrectly setting the overflow property of fixed-width containers. I notice this a lot when I read CodeBetter.com. In this post the screenshots overflow the post column and extend all the way behind the navigation column. This also happens with code blocks. I’m surprised that, not only do the writers fail to notice this when they preview their posts, as they should do, but no one has done anything to fix it.
I end up having to use IE Dev Toolbar or Firebug to select the offending images or codeblocks and set the overflow property to "auto". But even then, IE has problems: it shows a vertical scrollbar when it should just bump up the height of the container. The only fix I’ve found is to use JavaScript.
But before I get into that, here’s a quick primer on the overflow property. (W3Schools and W3C.org have more information, of course.)
Overflow determines what happens to content that can’t fit its container. For example, a long line of preformatted code in a narrow blog column. There are four options:
Visible The extra content is shown, even outside the borders of the container. This is generally not what’s wanted.
Hidden The extra content is hidden, with no way to see it. This is used for certain “tricks”, like single-image rollovers where the “hover” part of the image moved into view when needed.
Scroll The container is given scrollbars, even when the content doesn’t overflow. This usually looks wrong when the scrollbars aren’t needed.
Auto Scrollbars are added as necessary. I prefer this for most cases, since is will only show the scrollbars when they’re needed.
IE has a problem with "auto" though. Content that only overflows horizontally (like long lines of code) should only be given a horizontal scrollbar. But since the horizontal scrollbar covers up the bottom line of text, IE also adds a vertical scrollbar, instead of making the container bigger like Firefox does. Not only is this annoying with average-sized blocks of code, since you need to scroll to see that final line, but it makes it almost impossible to read one- or two-line blocks, since the vertical scrollbar is so small.
The only way I’ve found to fix this is using JavaScript:
This will loop through the elements that might need fixing (here, defined by the “codecontainer” class) and checks if clientHeight (the visible region) is different from scrollHeight (the height of the content). If height is not specified by CSS anywhere, the difference will be the height of the scrollbar. But to make sure, we first set style.height to scrollHeight. This makes the container’s height the same as the content height, which will change clientHeight but not scrollHeight. Now the difference is the height of the scrollbar, which we can add to style.height to remove the vertical scrollbar.
It’s all a little confusing with three varieties of “height” to work with. It just makes the container height equal to the content height, then increases it by the scrollbar height.
Unfortunately, I’m using a third-party syntax highlighter called … syntaxhighlighter, which I haven’t been able to update with this feature yet. So for now, this post is full of hypocracy, but I’ll get it fixed.