Overflow in Tables and CSS

This is a demonstration of how overflow is handled differently in tables and CSS. Overflow occurs when a word or URL is longer than the available width of it's containing box (when this width is specified). I've used a very long URL.

Overflow in Tables

This is a 2 x 2 table. The cell containing the URL has a width of 300px (set via CSS). However, because the address is much longer the containing <td> will expand, pushing the right column table cells off-screen instead of breaking out of the <td> and overlapping.

It's also interesting to note that although this table is in a div to provide the dark gray background, this div isn't expanding with the table outside the viewport (at least not in Safari). The div has width: auto

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

http://diveintomark.org/archives/2003/02/26/how_to_block_spambots_ban_spybots_and_tell_unwanted_robots_to_go_to_hell

Quisque quis nunc et risus iaculis vulputate.

Four kinds of overflow in CSS

The left div is positioned absolutely with a width of 300px, and the right div has a large left margin. They are inside a containing div. No body width is set, so the first example will probably expand past the right edge of the viewport, and cause the page to have a horizontal scrollbar.

Visible

This div is set to have overflow: visible. This means that if a line of text is too long it will 'stick out' of it's containing box, possibly overlapping other content, or maybe not (see Observations below). This can be caused by a pre tag, or because there are no word breaks (spaces) in a URL or some other very long word.

http://diveintomark.org/archives/2003/02/26/how_to_block_spambots_ban_spybots_and_tell_unwanted_robots_to_go_to_hell

This is the default behaviour, and the bane of any site with generated content (eg user comments)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque quis nunc et risus iaculis vulputate. Sed quis velit ac magna dignissim nonummy. Morbi ultricies dictum dui. Ut et lorem sed sapien semper molestie. Sed quam. Etiam sodales ante eget tellus. Pellentesque ornare, ante et suscipit euismod, ipsum tortor interdum ante, vel ultrices enim turpis eu sapien. Aenean adipiscing suscipit quam. Vestibulum vestibulum, turpis eget ultrices aliquam, eros metus luctus magna, a luctus enim velit quis leo. Vivamus vestibulum. Sed consectetuer, sem et aliquet semper, odio justo volutpat urna, at cursus tellus ligula at odio. Integer commodo, diam a euismod varius, diam dui fringilla enim, at laoreet nunc libero in wisi. Sed risus dui, fermentum et, iaculis eget, egestas sit amet, nulla. Vivamus luctus nisl ut purus. Phasellus malesuada, dolor eget tempus suscipit, enim purus ullamcorper odio, a porttitor dui eros vitae arcu. Fusce ac nunc.

Hidden

This div is set to have overflow: hidden. This means that if a line of text is too long it will be hidden outside it's containing box. However the part 'sticking out' is now no longer visible.

http://diveintomark.org/archives/2003/02/26/how_to_block_spambots_ban_spybots_and_tell_unwanted_robots_to_go_to_hell

This means the text will be inaccessable without looking at the source HTML. Also, if there is any spacing set on the right side of the containing box, the 'sticking out' text will still be visible over the margin, border and padding.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque quis nunc et risus iaculis vulputate. Sed quis velit ac magna dignissim nonummy. Morbi ultricies dictum dui. Ut et lorem sed sapien semper molestie. Sed quam. Etiam sodales ante eget tellus. Pellentesque ornare, ante et suscipit euismod, ipsum tortor interdum ante, vel ultrices enim turpis eu sapien. Aenean adipiscing suscipit quam. Vestibulum vestibulum, turpis eget ultrices aliquam, eros metus luctus magna, a luctus enim velit quis leo. Vivamus vestibulum. Sed consectetuer, sem et aliquet semper, odio justo volutpat urna, at cursus tellus ligula at odio. Integer commodo, diam a euismod varius, diam dui fringilla enim, at laoreet nunc libero in wisi. Sed risus dui, fermentum et, iaculis eget, egestas sit amet, nulla. Vivamus luctus nisl ut purus. Phasellus malesuada, dolor eget tempus suscipit, enim purus ullamcorper odio, a porttitor dui eros vitae arcu. Fusce ac nunc.

Scroll

This div is set to have overflow: scroll. This forces the div to always display widget areas for scroll bars, although they will be inactive unless the content extends beyond the div's width(/height, although we haven't specified one). This should mean an active horizontal bar and a greyed out vertical one.

http://diveintomark.org/archives/2003/02/26/how_to_block_spambots_ban_spybots_and_tell_unwanted_robots_to_go_to_hell

While ugly, the text is at least accessable now.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque quis nunc et risus iaculis vulputate. Sed quis velit ac magna dignissim nonummy. Morbi ultricies dictum dui. Ut et lorem sed sapien semper molestie. Sed quam. Etiam sodales ante eget tellus. Pellentesque ornare, ante et suscipit euismod, ipsum tortor interdum ante, vel ultrices enim turpis eu sapien. Aenean adipiscing suscipit quam. Vestibulum vestibulum, turpis eget ultrices aliquam, eros metus luctus magna, a luctus enim velit quis leo. Vivamus vestibulum. Sed consectetuer, sem et aliquet semper, odio justo volutpat urna, at cursus tellus ligula at odio. Integer commodo, diam a euismod varius, diam dui fringilla enim, at laoreet nunc libero in wisi. Sed risus dui, fermentum et, iaculis eget, egestas sit amet, nulla. Vivamus luctus nisl ut purus. Phasellus malesuada, dolor eget tempus suscipit, enim purus ullamcorper odio, a porttitor dui eros vitae arcu. Fusce ac nunc.

Auto

This div is set to have overflow: auto. This is similar to overflow: scroll, but in this case scroll bars are only visible if content is actually larger than the containing div. This also has the advantage of usually only displaying the needed scrollbar, in this case the horizontal one.

http://diveintomark.org/archives/2003/02/26/how_to_block_spambots_ban_spybots_and_tell_unwanted_robots_to_go_to_hell

If the containing box is taller than the screen, the user won't see the scroll box, and this will appear the same as overflow: hidden. For content that may often overflow this is the best that CSS can do, ugly though it is.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque quis nunc et risus iaculis vulputate. Sed quis velit ac magna dignissim nonummy. Morbi ultricies dictum dui. Ut et lorem sed sapien semper molestie. Sed quam. Etiam sodales ante eget tellus. Pellentesque ornare, ante et suscipit euismod, ipsum tortor interdum ante, vel ultrices enim turpis eu sapien. Aenean adipiscing suscipit quam. Vestibulum vestibulum, turpis eget ultrices aliquam, eros metus luctus magna, a luctus enim velit quis leo. Vivamus vestibulum. Sed consectetuer, sem et aliquet semper, odio justo volutpat urna, at cursus tellus ligula at odio. Integer commodo, diam a euismod varius, diam dui fringilla enim, at laoreet nunc libero in wisi. Sed risus dui, fermentum et, iaculis eget, egestas sit amet, nulla. Vivamus luctus nisl ut purus. Phasellus malesuada, dolor eget tempus suscipit, enim purus ullamcorper odio, a porttitor dui eros vitae arcu. Fusce ac nunc.

Observations

Safari 1.0
  • tables - the cell expands, pushing cells to the right offscreen
  • visible - the URL 'breaks out' of it's containing box, overlapping content on the right
  • Hidden - the part of the URL that 'breaks out' of the containing box above is hidden. however note that it is still visible to the edge of the containing box. it's displayed over padding/border/margin areas.
  • Scroll - scrollbars are always visible, although because there are no height restrictions on this box, the vertical scrollbar is greyed out
  • Auto - scrollbars are only displayed when needed, so there is no vertical scrollbar
Mozilla Firebird 0.6
The only unusual rendering is the active vertical scroll bar on both Scroll and Auto. This is strange because there's nothing preventing this box expanding to display all content vertically.
Inernet Explorer 5.2
IE uses "/" characters as a break. Interesting! Also, for Visible: the URL overlaps, but causes the containing box to increase in size - just like the overflow on tables! This is a possible interpretation of the CSS2 spec, but is not the common one.

Conclusion

I think it's a great pity that the spec is so vague about something so important. It would have been much better to have made the default Visible always 'break out' of the containing box, and to have added an extra attribute ("Expand"?) that always made the containing box larger to fit if necessary (shrink-wrapping). Please let me know if you see any unusual results in other browsers via a comment