tags:

views:

40

answers:

5

Is there a way, using CSS, to show borders in a table between columns only (not on the outer edges)?

+1  A: 

I may be simplifying the issue, but does td {border-right: 1px solid red;} work for your table setup?

DavidT
No, because then there will be a border on the right of the table. I don't want a border on the right or left edge of the table, only between columns.
dmr
If you apply a class to your final column then you can remove the border-right declaration from the final one. It does require manual adjustment, but if it's a static page then it's actually the best solution.
hollsk
+6  A: 

Not without tricky css selectors and extra markup and the like.

Something like this might do (using CSS selectors):

table {
border:none;
border-collapse: collapse;
}

table td {
border-left: 1px solid #000;
border-right: 1px solid #000;
}

table td:first-child {
border-left: none;
}

table td:last-child {
border-right: none;
}
Bryan Ross
Note: IE6-8 does not support `:last-child` (and spotty on `:first-child` according to http://www.quirksmode.org/css/contents.html
Scott
Just use a left border, then there's no need for the `:last-child`.
jeroen
@jereon: True, didn't really think it through all the way. But the basic point is the same.
Bryan Ross
@Bryan, my comment was mainly directed at Scott to counter the problem he brought up; I completely agree with the idea of your answer, you can even get rid of the first line as far as I´m concerned :-) +1
jeroen
+1  A: 

Borders on tables are always a bit flaky. One possibility would be to add a border-right declaration to each table cell except for the ones in right-most column. If you're using any kind of table-spacing this won't work very well.

Another option would be to use a 1px high background image with the borders inside it, but that'll only work if you can guarantee the width of each cell at all times.

Another possibility is to experiment with colgroup / col. This had fairly horrible support cross-browser the last time i looked at it but could have improved since then: http://www.webmasterworld.com/forum83/6826.htm

hollsk
+1  A: 

There's no easy way of doing this, other than doing something like class="lastCell" on the last td in each tr, and then setting your css up like this:

#table td {
    border-right: 5px solid red
}

.lastCell {
    border-right: none;
}
SteveH
+1  A: 

You need to set a border-right on the td's then target the last tds in a row to set the border to none. Ways to target:

  1. Set a class on the last td of each row and use that
  2. If it is a set number of cells and only targeting newer browers then 3 cells wide can use td + td + td
  3. Or better (with new browsers) td:last-child
Scott