943,625 Members | Top Members by Rank

Ad:
Jul 2nd, 2009
0

HTML Tables vs CSS & divs - examples/help

Expand Post »
Admittedly, I'm new to CSS but I am having some difficulty converting tables to CSS. Here is a CSS example for a simple 4 cell table that works fine -- but only without the DOCTYPE declaration (any DOCTYPE declaration)!

Does anyone know what, if anything, I am doing wrong ? Any help would be appreciated.

(Also I would like to see any successful examples anyone would like to post.)

HTML and CSS Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3.  
  4. <html>
  5. <head>
  6. <style type="text/css">
  7. .TblContainer {
  8. width:400px;
  9. font-family:Arial,Helvetica,sans-serif;
  10. background-color:yellow;
  11. border:1px solid #CCCCCC;
  12. margin:3px 3px 3px 3px;
  13. }
  14.  
  15. .Row {
  16. margin:3px 3px 3px 3px;
  17. }
  18.  
  19. .Cell {
  20. color:#000000;
  21. border:1px solid #CCCCCC;
  22. float:left;
  23. font-size:16px;
  24. text-align:center;
  25. width:49%;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="TblContainer">
  31. <div class="Row">
  32. <div class="Cell">Cell 1</div>
  33. <div class="Cell">Cell 2</div>
  34. </div>
  35. <div class="Row">
  36. <div class="Cell">Cell 3</div>
  37. <div class="Cell">Cell 4</div>
  38. </div>
  39. </div>
  40. </body>
  41. </html>
Reputation Points: 10
Solved Threads: 0
Light Poster
FeralReason is offline Offline
32 posts
since May 2009
Jul 2nd, 2009
0

Re: HTML Tables vs CSS & divs - examples/help

HTML tables are used for displaying tabular data on the page.
Div's are used to build the entire layout of the page. It gives more layout control using CSS.

And i dont see any thing wrong with your above code and it works fine with my browser . But i recommend you should use Html tables itself as you want just tabluar data.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
siji44 is offline Offline
12 posts
since May 2007
Jul 2nd, 2009
0

Re: HTML Tables vs CSS & divs - examples/help

Click to Expand / Collapse  Quote originally posted by siji44 ...
HTML tables are used for displaying tabular data on the page.
Div's are used to build the entire layout of the page. It gives more layout control using CSS.

And i dont see any thing wrong with your above code and it works fine with my browser . But i recommend you should use Html tables itself as you want just tabluar data.
Thanx for the reply. I understand (and actually agree with) the argument but (call it self-education) I am still interested in determining to what extent css can do tables without using table tags.

In the code shown, the background should be yellow -- showing that the TblContainer div is in fact containing everything. It only shows up as a yellow band at the top of the cells in IE, FF and Chrome when I load it -- unless I delete the DOCTYPE. Are you seeing something different ?

Thanx for your help.
Reputation Points: 10
Solved Threads: 0
Light Poster
FeralReason is offline Offline
32 posts
since May 2009
Jul 3rd, 2009
0

Re: HTML Tables vs CSS & divs - examples/help

I have checked your code. The problem is with the positiong of floated div's in a nested div. Please add these css:-

.Row{
margin:3px 3px 3px 3px;
clear: both;
}


.Cell {
	color:#000000;
		border:1px solid #CCCCCC;
		float:left;
		font-size:16px;
		text-align:center;
		width:49%;
                   margin-right:3px;	}


I hope this will help you and for detailed explanation please go through this site:
http://www.positioniseverything.net/easyclearing.html
Last edited by peter_budo; Jul 6th, 2009 at 11:09 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
siji44 is offline Offline
12 posts
since May 2007
Jul 3rd, 2009
0

Re: HTML Tables vs CSS & divs - examples/help

Click to Expand / Collapse  Quote originally posted by siji44 ...
I have checked your code. The problem is with the positiong of floated div's in a nested div. Please add these css:-

.Row{
margin:3px 3px 3px 3px;
clear: both;
}


.Cell {
color:#000000;
border:1px solid #CCCCCC;
float:left;
font-size:16px;
text-align:center;
width:49%;
margin-right:3px; }


I hope this will help you and for detailed explanation please go through this site:
http://www.positioniseverything.net/easyclearing.html

Thanks much for your replay. When I add the "clear:both" line the lower edge of the container moves down to contain the first row but not the second. Adding the "margin-right:3px" statement causes all 4 cells to stack vertically rather than appear 2 to a row horizontally. However, your reference was golden !

The reference has a link to a more recent article on this problem, offering up several solutions. The most elegant is to place an "overflow:auto" in the container. This works perfectly without any other changes.

One other solution that came from my son in San Francisco is to simple set the height of the row. Apparently floating the cells causes the container to lose track of the height of what it is enclosing.

All-in-all: "overflow:auto" seems to be the best solution.

Thanx much.
Reputation Points: 10
Solved Threads: 0
Light Poster
FeralReason is offline Offline
32 posts
since May 2009
Jul 8th, 2009
1

Re: HTML Tables vs CSS & divs - examples/help

You are designing for the quirks mode of the browsers without the doctype. If you are in quirks mode, the browsers do weird things.

Standard mode puts the surrounding styles (margin, border, padding) OUTSIDE the width and height declarations, so you have to leave extra space for them.

Don't use pixels and points. Use percents and em, so the page is compatible with multiple screen resolutions.
Last edited by MidiMagic; Jul 8th, 2009 at 2:26 am.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
Jul 8th, 2009
0

Re: HTML Tables vs CSS & divs - examples/help

Very good! Never heard of quirks mode but (after a short search for my education) this certainly explains the problem. (http://www.quirksmode.org/css/quirksmode.html - section on overflow:visible)

Thanx for the advice !
Reputation Points: 10
Solved Threads: 0
Light Poster
FeralReason is offline Offline
32 posts
since May 2009
Jul 11th, 2009
0

Re: HTML Tables vs CSS & divs - examples/help

When you 'float' an element, it's like you're taking it on a higher plane. (think of the page as the airport and the floated div an... err... flying airplane)
Since the elements contained by the main div are all floated, the main div, in effect, has no 'content'. So all you see is its borders (the small band at the top).

The overflow: auto is a good solution... provided the 'overflowed' div doesn't span 100% of the page width.
If your overflowed div has 100% width, and the page exceeds one 'page length', the scrollbar that appears on the right causes the div have a less than 100% displaying area. So some browsers will display a horizontal scrollbar. Which is ugly. =(
I've had to reduce the width of my main container divs due to that problem.
And I'm still waiting for the table-cell, table-row, etc. values for the display property to be supported by all browsers.
Reputation Points: 70
Solved Threads: 15
Posting Whiz
kanaku is offline Offline
378 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: Really basic Noob question- centering page
Next Thread in HTML and CSS Forum Timeline: Divs not postioned well in main div





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC