views:

259

answers:

6

Hi

Maybe there is already a question about this subject, but I couldn't find it.

My question is simple,
Can I use only divs on a page layout?

I got myself in trouble to create a div with corners for example.

A_____B______C  
|            |
D     E      F
|            |
G_____H______I

take all letters as divs with some background, while letter E is where the content is placed, and it is dynamic, so it can get any height, while the width will be 100% for whole window(there is another div as the menu floating left of that div, but I didn't consider it here).
in table I did that really quick with no hack, but with divs I just couldn't.
I couldn't make the height of div D and F expand correctly with divs, the rest was ok

so,
1. can div really replace tables for layout?
2. can div replace tables without css compatibility-hacks?

(btw, that wasn't my only problem with div and css for layouts where table did it easily)

Thanks,
Joe

+3  A: 
  1. Yes.
  2. Sadly, no. Your example will almost certainly require some "hacking" to work in all browsers. Some things that were easy to do with layout tables are very, very complex to implement using pure CSS.

For your example, the following questions should provide you some pointers to work with.

Pekka
Actually, his example can be created in less markup with divs than with tables ;)
Tatu Ulmanen
Nice! I will take a look at your link.
Pekka
+1  A: 

Not really answering your question, but Yahoo has some nice grid CSS tools that really help with doing div-based layouts. These might be useful as a reference.

http://developer.yahoo.com/yui/grids/

Andy White
A: 

You should make rows with the divs, as you would with tables.

  • Put abc in a div | let's call it div 1
  • Put def in a div | let's call it div 2
  • Put ghi in a div | let's call it div 3

Let e determine the height of div 2, and let 2 determine the heights of d and f. Using the proper position, such as position:relative, and display:table-cell you should be able to manage.

Not going to create the whole solution.

Vian Esterhuizen
Too complicated, can be done easier, take a look at my solution. :)
Tatu Ulmanen
I think he wants the overall div to be the width of the window. Using a combination of width:100%, mine should work. Although I didn't take into consideration that my solution wouldn't work in ie 6/7. Other solutions are better.
Vian Esterhuizen
I tried this like 2 months ago and it didn't work for me, I guess I did something wrong. Can you give me an example of this please?
Jonathan
Hi Jonathan,My solutions won't work in IE 6/7.Sorry
Vian Esterhuizen
+3  A: 

Your example is completely doable in basic CSS using absolute positioning inside relatively positioned element. Take a look at this:

http://www.ulmanen.fi/stuff/box.php

So, in answer to your question, divs really replace tables for layout.

And what goes for question number two; tables should be used where tables are needed: in tabular data. If you need to present something in a table, use a table. Just don't use them for layout.

Tatu Ulmanen
Hi, thanks for the asnwer, I looked your example and it didn't work on ie8 and ie7(compatibility view of ie8), can that be done in ie too?
Jonathan
@Jonathan, it works now, I just forgot to add a DOCTYPE. Without it, IE goes into quirks mode where everything goes apeshit.
Tatu Ulmanen
+1  A: 
  1. can div really replace tables for layout?

Generally it's worth aiming for. But for everything? No. There are common table constructs which cannot be reproduced with CSS, particularly where you are mixing fixed-pixel, font-relative and viewport-relative measurements in one table. Complex liquid form layouts are the usual case for this.

In theory you could replace <table>, <tr> and <td> with divs styled as display: table-cell et al. However this won't work in IE, and is of questionable usefulness: you are still leaking the layout concerns into the markup just as much as if it were a table. (Plus you can't do spanned cols/rows like this.)

I got myself in trouble to create a div with corners for example.

You mean with image corners? That's really easy. But the trick is not to try to do it by positioning elements. Instead, use nested divs, each with its own background. For example:

<div id="foo"><div class="left"><div class="right">
    Content.
</div></div></div>

#foo { background: url(mainbackground.gif); }
#foo .left { background: url(leftborder.gif) top left repeat-y; }
#foo .right { background: url(rightborder.gif) top right repeat-y; padding: 32px; }

That gives you a left and right border image laid over the main background. You can do the same three times to get a full table-like with 8 border images, or just nest divs nine deep. You can reduce the number of divs required from 3 to 2 if you can include the main background image in one of the border images (this may require very wide images if the element may grow large). You can use padding on some of the elements if you need the border images to be transparent (ie, the main background image isn't to be rendered on the edges).

In the future, this will become much easier and remove the need for so much nested markup, thanks to CSS3's proposed multiple backgrounds per element and border images.

bobince