views:

506

answers:

5

Hi,

I'm trying to construct a two row table similar to following:

---
-

where the bottom is filling the space of the upper row. Is this possible using CSS? In HTML I can do things like colspan.

+1  A: 
<div style="width: 100%;">
    <div style="float: left; width: 33%;">Row 1 - Cell 1</div>
    <div style="float: left; width: 34%;">Row 1 - Cell 2</div>
    <div style="float: left; width: 33%;">Row 1 - Cell 3</div>
</div>
<div style="clear: left; width: 100%;">
Row 2 - Cell 1
</div>
Dustin Laine
I can't know the width of the cells.
rFactor
Then don't put a width. It should not matter. But if you want them to span the same width then the two main divs need to have the same width.
Dustin Laine
+3  A: 

That isn't part of the purview of CSS. colspan describes the structure of the page's content, or gives some meaning to the data in the table, which is HTML's job.

mwc
A: 

You could trying using a grid system like http://960.gs/

Your code would be something like this, assuming you're using a "12 column" layout:

<div class="container_12">
<div class="grid_4">1</div><div class="grid_4">2</div><div class="grid_4">3</div>
<div class="clear"></div>
<div class="grid_12">123</div>
</div>
Jeff
+4  A: 

I must respectfully disagree with the answer that colspan is exclusively indicative of structure, and the implicit notion that you shouldn't even be trying to solve your problem this way. To say this with no room for dispute is, at worst, naive, or at best, disingenuous. It is not in service to the person asking the question.

If you have a section of a table that you choose to span multiple columns for appearance's sake, then it isn't structural. It's appearance. And, despite this, there's no simple, elegant CSS analog for it. That's the honest answer, and I say that as a CSS fan. CSS does many things very, very well. Tables are not among them.

That may not be the popular answer, but I believe it to be the honest one. The truth is that the distinction between structure and presentation is not a bright white line, but a decidedly blurred one. I won't presume to tell you your need for spanning columns is right or wrong. Searches on this very issue will return a variety of solutions that include a bevy of alternatives, including absolute positioning, sizing, along with a similar variety of browser- and circumstance-specific caveats. Read, and make the best informed decision you can based on what you find.

David
Tables are structural elements and just because using colspan changes its appearance does not mean it's not. CSS is used to style elements and not change the structure. The W3C discusses table structure here: http://www.w3.org/TR/html401/struct/tables.html#h-11.2
Rob
Rob, understand your position, but look in the W3C recommendations - specifically pertaining to the entire table paradigm - and you'll find that part of their consideration was, most certainly, the *presentation* of tables. This idea that tables were purely conceived as structure is a contemporary fiction I think we'd all be better served to stop spreading. The point is we need to stop being dogmatic about tables. It would be wrong for me to say they're *always* presentation, and it is just as wrong for you to say they're *always* structure. Reality is simply not that cut-and-dried.
David
+1  A: 

There is no colspan in css as far as I know, but there will be column-span for multi column layout in the near future, but since it is only a draft in CSS3, you can check it in here. Anyway you can do a workaround using div and span with table-like display like this.

This would be the HTML:

<div class="table">
  <div class="row">
    <span class="cell red first"></span>
    <span class="cell blue fill"></span>
    <span class="cell green last"></span>
  </div>
</div>
<div class="table">
  <div class="row">
    <span class="cell black"></span>
  </div>
</div>

And this would be the css:

  /* this is to reproduce table-like structure
     for the sake of table-less layout. */
  .table { display:table; table-layout:fixed; width:100px; }
  .row { display:table-row; height:10px; }
  .cell { display:table-cell; }

  /* this is where the colspan tricks works. */
  .span { width:100%; }

  /* below is for visual recognition test purposes only. */
  .red { background:red; }
  .blue { background:blue; }
  .green { background:green; }
  .black { background:black; }

  /* this is the benefit of using table display, it is able 
     to set the width of it's child object to fill the rest of 
     the parent width as in table */
  .first { width: 20px; }
  .last { width: 30px; }
  .fill { width: 100%; }

The only reason to use this trick is to gain the benefit of table-layout behaviour, I use it alot if only setting div and span width to certain percentage didn't fullfil our design requirement.

But if you don't need to benefit from the table-layout behaviour, then durilai's answer would suit you enough.

Hendra Uzia