12

I've seen quite a few answers on this site which advocate using tables for a design because it can't be done using CSS and Divs... when in actual fact it can be done with a bit of ingenuity.

What are examples of things that genuinely can't be done?

The only one I've ever come across is vertically aligning a box within another box.

*edit: I suppose I'm mostly interested in unachievable layouts using CSS2

wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • I don't think your question is clear enough, are you asking generally what can't be done with CSS alone or what can be done with tables but not CSS? (in case of the latter please read my answer!) – roryf Apr 15 '09 at 08:45
  • Both, I suppose, though mostly the second. It's useful to be aware of what can't be achieved when someone gives you a design to implement and you're not sure if it will be tricky or impossible. – wheresrhys Apr 15 '09 at 11:03

11 Answers11

4

The answer to this question depends on a number of things:

  • How backwards compatible do you need to be? Including IE6 will decrease the capacity of pure CSS; and
  • How much of your site is fixed-width and/or fixed-height. There are certain things in CSS that become hard if not impossible in variable width and/or height situations.

Side-by-side content is a problem for CSS. You can use floats for this but if the sum of widths exceeds the width of the container, the tail end floats will fall down below. Tables are more capable in this regard as they will squeeze columns where possible to make things fit and cells will never be split onto new rows.

Vertical centering you mentioned. Its trivial with tables and hard or impossible (depending on compatibility and fixed or variable heights of the container and the item) in pure CSS.

You may also be referring to hover content. IE6 only supports the :hover pseudo element on anchors. Javascript is required for that browser for :hover-like behaviour.

Basically if what you need to do can be done fairly trivially with pure CSS then do it. If not, don't feel bad if you have to use tables despite all the anti-table fanatics (and they are fanatics) jumping up and down in horror.

If you want a relatively simple exmaple of this check out Can you do this HTML layout without using tables?. This is a conceptually simple layout problem that is trivial with tables and noone has yet posted a solution meeting the requirements with pure CSS.

Community
  • 1
  • 1
cletus
  • 616,129
  • 168
  • 910
  • 942
  • I would argue that it shoudl be thought of the other way around: If a relatively trivial design feature needs tables to make it work then change the design slightly. Most of your users won't even notice. But if you use tables a significant minority of users - screen reader users - will be confused. – wheresrhys Apr 15 '09 at 11:48
  • I disagree. The argument is made "tables are for content not layout". Unfortunately deficiencies in both CSS and its support in browsers don't make that quite true so favour CSS for layout but don't pull your hair out trying to avoid using a table if it all becomees too hard. – cletus Apr 15 '09 at 12:02
  • If your website wants to attract visually impaired users (roughly 9% of users) then there is definitely a case to rule out using tables for layout as it puts them off using your site. Using tables, quite appart from being unfair to VI users, could cost you money by alienating potential customers. – wheresrhys Apr 15 '09 at 13:09
  • not sure to be honest. Pretty much impossible to find reliable stats on it. Just found 9% mentioned on a handful of sites. – wheresrhys Apr 27 '09 at 10:34
4
  • Vertical alignment of blocks or text.

  • Having elastic containers that stretch to the width of their content.

  • Having several "rows" with the same structure where "cells" are synchronized in width throughout all rows.

  • Having several "columns" synchronize their height (up to the length of the longest text block).

These are quite basic designer needs that appear in even basic design concepts.

Cells/columns issues can possibly be solved with CSS if you take IE8 into account, but it will be many years until its wide spread (even IE7 in 2-3 years hasn't reached the desired market share).

As for "ingenuity", it is not that good thing in software development. Tricks that your colleagues and you yourself after a couple of months will not be able to understand usually build up that code base that everyone either is scared to touch or determined to refactor/rewrite completely.

Remember the KISS principle. The simpliest way you do this, the more reliably it will work.

User
  • 30,403
  • 22
  • 79
  • 107
  • Elastic containers can sort of be achieved by floating them and specifying width:auto, though if the content is text it won't wrap the text until the box is at its max width, so having elastic containers next to each other so that in total hey fill the width isn't possible – wheresrhys Apr 15 '09 at 11:18
  • Your third and fourth examples I think are possible, but only by adding extra divs and using the faux columns techniqe. The rows issue is a lot trickier than a table, but columns is not too bad – wheresrhys Apr 15 '09 at 11:19
  • Also, I agree with the ingenuity point and it's true that a) CSS2, particularly with cross-browser issues, requires far too much. b) there seems to be far less of a tradition of commenting CSS than other code files – wheresrhys Apr 15 '09 at 11:21
2

"... when in actual fact it can be done with a bit of ingenuity."

How about 'avoiding the need for ingenuity' as a thing that's hard to do in CSS.

;)

izb
  • 50,101
  • 39
  • 117
  • 168
  • I don't believe in upvoting things for being funny, but if there was a "made me chuckle" button I'd click it – wheresrhys Apr 15 '09 at 11:14
  • 1
    I think it's a perfectly valid point. Although I'm not a website developer, even trivial things need a lot of thought and experimentation with css. Tables tend to be a lot more forgiving. – Jim T Apr 15 '09 at 12:20
2

tables should be used for tabular data! We should always try to use the correct HTML for the given content in which to markup. So not just div's (span, ul, li, dl, strong, em ... etc) This ensures that the content is not just looking right but is right (for SEO and accesibile reasons)

By not using tables it allows us to transform the content from one look and feel to the next without having to change the HTML, see Zen Garden

For now though with current browsers CSS table like layouts can be done but are tricky. there are techniques to get round many of the issues, weather they are done though global wrappers with background images, or positioning fixes... where both articles also refer to using Javascript to progressively enhance a page to get those additional classes you may require.

or of course you could use some XSL as a middle ware to help do formating if processing from a CMS.

nickmorss
  • 621
  • 1
  • 6
  • 10
  • Yes, but tables are harder to get looking right, and at the end of the day, that is the decisive factor in HTML. – CurtainDog Apr 15 '09 at 09:30
  • ZenGarden is ultimately primitive. Any more or less complex design will require reshuffling of all of your divs and consequently of the style sheet. – User Apr 15 '09 at 09:37
  • but the principle behind ZenGraden & the markup off CSS is that they are abstract, reshuffeling divs is not alwasy required if you consider that you con position things abolsutly & relativly, e.g move the navigation from the bottom of the HTML to the top of the render for SEO / accessibility – nickmorss Apr 15 '09 at 13:19
1

Alternate row colors in a table without manually (or with the aid of a script) assigning alternate styles to each row.

Determine one element's position relative to another. For example you can't use CSS to determine which position one box is in a bunch of floated boxes using the same class. Would be nice to for example know if one box is the first box floated, or the second, or the last.

Handle widows. A widow is a word that dangles off the end of a paragraph, that is a single word starts the last line on a paragraph. It's a big nono on print design, but in the world of web it's hard to control.

Bjorn
  • 69,215
  • 39
  • 136
  • 164
  • Can be done with CSS 3 selectors: http://www.w3.org/TR/css3-selectors/#nth-child-pseudo – Greg Apr 15 '09 at 08:38
  • Is this something that can be done automatically using only tables? Or are Javascript and by hand the only ways that this can be done? – Mike Cooper Apr 15 '09 at 08:40
  • I added some other stuff. That css3 stuff is nice. Can't wait for it to happen. – Bjorn Apr 15 '09 at 08:41
  • Don't really understand what you mean by the second example, but sounds interesting if you could elaborate – wheresrhys Apr 15 '09 at 11:11
  • wheresrhys: have special styles for some floated boxes depending on where they are. This could lead to having uniform columns with floats. – Bjorn Apr 15 '09 at 15:40
1

Floating elements in multiple columns, where text in each cell can expand the height of the element, but the entire row below must be pushed down if this happens.

 ---   ---   --- 
|AAA| |BBB| |CCC|
 ---   ---   --- 
 ---   ---   --- 
|AAA| |BBB| |CCC|
|   | |BBB| |   |
 ---   ---   --- 
 ---   ---   ---
|AAA| |BBB| |CCC|
 ---   ---   ---
Kamiel Wanrooij
  • 12,164
  • 6
  • 37
  • 43
0

An image cannot placed in exact center of a cell with align attribute.It can be done with some brute force .

Warrior
  • 39,156
  • 44
  • 139
  • 214
0

Sounds obvious but you can't change content with CSS, it can only be used for styling.

Jeremy French
  • 11,707
  • 6
  • 46
  • 71
  • It's a stylesheet. That's what HTML is for. Also you can put in content http://www.w3schools.com/Css/pr_gen_content.asp – Dead account Apr 15 '09 at 09:19
  • Technically, you can use the :after pseudoelement to insert an image or text. You are not changing the DOM tree, but it acts like you did. Please correct me if I'm wrong. – Stefano Borini Apr 15 '09 at 12:01
  • you can use content with pseudoelements to change how things look on the page, but you are changing the look, not the content. – Jeremy French Apr 15 '09 at 15:18
0

Rory, I think you're absolutely right. Vertical alignment can be done with line-height, and creating lay-outs in CSS really isn't that hard. In fact, it's far more flexible when using absolute/relative positioning and floats.

People still using tables for design should really brush up with the current standards.

Going on topic, with CSS3 coming up it's hard to think of stuff CSS can't do. Image manipulation, content manipulation, advanced selectors, it's all going to be possible. But if I had to name one thing, it's that with CSS you can't (and won't) be able to rotate elements.

SolidSmile
  • 3,206
  • 4
  • 21
  • 24
  • vertical alignment for blocks can't be done with line-height - believe me I've tried, and if you look through various other questions on stack overflow there's lots of other people who have tried and failed. CSS3 will be damn cool when it's widely supported! – wheresrhys Apr 15 '09 at 12:59
0

I was unable to use a transparency to create a variable-height text area across all pages. I believe it's impossible. I ultimately just wrote a quick javascript function to reset the height after the page load, to get the layout to work. See http://peterchristopher.com to see what I mean by transparency for the text area.

-3

There is absolutely nothing tables can do that CSS can't.

There seems to be a common misconception that HTML & CSS should be easy. It isn't. If you find yourself wanting to use tables then its your CSS skills that need improving not the technology (although the technology does obviously have plenty of holes that could do with improving).

roryf
  • 29,592
  • 16
  • 81
  • 103
  • 1
    Your answer is worthless without addressing the other answers which do describe counter-examples. –  Apr 15 '09 at 08:54
  • That answer is to be heard from someone who hasn't tried to make a complex professional design for enterprise sites. CSS just stays away. – User Apr 15 '09 at 09:31
  • @Mastermind I am a professional. I've built several large websites using entirely CSS. What an 'enterprise' site is I have no idea. Some day you will learn. – roryf Apr 15 '09 at 09:53
  • @Iraimbilanja I take your point, but a lot of the answers don't give examples that tables will solve either. – roryf Apr 15 '09 at 09:54
  • 1
    Would love to know who upvoted this and why. Pity upvote? Sorry but this answer deserves eveyr downvote it gets and then some. – cletus Apr 15 '09 at 11:44
  • Apologies if I offended anyone but I stand by the statement, I maybe just could have presented it better. – roryf Apr 15 '09 at 13:11