You can use all kinds of extra spaghetti markup or you can add one class to your table, like so:
<table class="FunkifyMyBackgounds">
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
</table>
.
and use some very simple jQuery javascript.
<script type="text/javascript">
function SetAllSpecialCellBackgrounds (bNeedToCreateStructure)
{
var zCellsToBackgroundify = $(".FunkifyMyBackgounds td");
//--- Set each cell's funky background.
zCellsToBackgroundify.each (function (idx) {SetA_SpecialCellBackground ($(this), idx, bNeedToCreateStructure);} );
}
function SetA_SpecialCellBackground (zJnode, K, bNeedToCreateStructure)
{
if (bNeedToCreateStructure)
{
//--- Add our special background structure.
var sIdName = 'idSpecialCellBG_Container' + K;
zJnode.append
(
'<div id="' + sIdName + '" class="SplitCellBackground">' +
'<div class="TopOfCell"> <\/div><div class="BottomOfCell"> <\/div>' +
'<\/div>'
);
}
ResizeA_SpecialCellBackground (zJnode);
}
function ResizeA_SpecialCellBackground (zJnode)
{
var zCellBG_Frame = zJnode.find ('div.SplitCellBackground');
//--- Set the background container to match the cell dimensions.
zCellBG_Frame[0].style.width = zJnode.outerWidth (false) + 'px';
zCellBG_Frame[0].style.height = zJnode.outerHeight (false) + 'px';
//--- Position absolutely; Adjust for margin, if needed.
var aContentPos = zJnode.offset ();
//--- Redundant for IE. Tested and IE really seems to need it, jQuery has failed me!
zCellBG_Frame[0].style.top = aContentPos.top + 'px';
zCellBG_Frame[0].style.left = aContentPos.left + 'px';
zCellBG_Frame.offset (aContentPos);
}
$(document).ready
(
function ()
{
SetAllSpecialCellBackgrounds (true);
/*--- Globally catch table cell resizes caused by the browser window change.
For other changes we could fire on DOMSubtreeModified, but IE and Opera are
poopy-heads! (Per usual)
So a cross-browser, good-enough solution is just to use a timer. Keep it just under
a second per usability guidelines.
*/
$(window).resize (function() {SetAllSpecialCellBackgrounds (false);} );
setInterval (function() {SetAllSpecialCellBackgrounds (false);}, 444);
}
);
</script>
.
Required CSS:
/***** Start of split-cell, specific styles. ****
*/
.SplitCellBackground, .TopOfCell, .BottomOfCell {
margin: 0;
padding: 0;
width: 100%;
height: 50%;
z-index: -10;
}
.SplitCellBackground {
position: absolute;
width: 10em;
height: 10em;
}
.TopOfCell {
background: #33FF33;
}
.BottomOfCell {
background: #FF33FF;
}
/***** End of split-cell, specific styles. *****/
.
You can see the complete code, in action at: http://scratchpad2.com/public/TableWithSplitBackground.htm .
It works with all major browsers but would require a a slight tweak for IE6 (available for a $30,000 donation to my Paypal. ;-) )