Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply a border only inside a table?

I am trying to figure out how to add border only inside the table. When I do:

table {     border: 0; } table td, table th {     border: 1px solid black; } 

The border is around the whole table and also between table cells. What I want to achieve is to have border only inside the table around table cells (without outer border around the table).

Here is markup I'm using for tables (even though I think that is not important):

<table>     <tr>         <th>Heading 1</th>         <th>Heading 2</th>     </tr>     <tr>         <td>Cell (1,1)</td>         <td>Cell (1,2)</td>     </tr>     <tr>         <td>Cell (2,1)</td>         <td>Cell (2,2)</td>     </tr>     <tr>         <td>Cell (3,1)</td>         <td>Cell (3,2)</td>     </tr> </table> 

And here are some basic styles I apply to most of my tables:

table {     border-collapse: collapse;     border-spacing: 0; } 
like image 542
Richard Knop Avatar asked Aug 10 '09 21:08

Richard Knop


People also ask

Can we apply border to a cell in a table?

To add a border to your table, you need to define the <style> of your table. Remember to add borders also for <th> and <td> tags to have a complete table. Set the border-collapse property as well (if you don't define the border-collapse, it will use border-collapse: separate by default).

How do I add internal lines to a table border in CSS?

CSS Table Borders. When you use CSS to add borders to tables, it only adds the border around the outside of the table. If you want to add internal lines to the individual cells of that table, you need to add borders to the interior CSS elements. You can use the HR tag to add lines inside individual cells.

How do I add a border to one row in a table?

If you want to add a border only to the bottom of the table row, you can apply the CSS border-bottom property to <td> elements that are placed within a <tr> tag.


2 Answers

If you are doing what I believe you are trying to do, you'll need something a little more like this:

table {   border-collapse: collapse; } table td, table th {   border: 1px solid black; } table tr:first-child th {   border-top: 0; } table tr:last-child td {   border-bottom: 0; } table tr td:first-child, table tr th:first-child {   border-left: 0; } table tr td:last-child, table tr th:last-child {   border-right: 0; } 

jsFiddle Demo

The problem is that you are setting a 'full border' around all the cells, which make it appear as if you have a border around the entire table.

Cheers.

EDIT: A little more info on those pseudo-classes can be found on quirksmode, and, as to be expected, you are pretty much S.O.L. in terms of IE support.

like image 120
theIV Avatar answered Sep 19 '22 01:09

theIV


this works for me:

table {     border-collapse: collapse;     border-style: hidden; }  table td, table th {     border: 1px solid black; } 

view example ...

tested in FF 3.6 and Chromium 5.0, IE lacks support; from W3C:

Borders with the 'border-style' of 'hidden' take precedence over all other conflicting borders. Any border with this value suppresses all borders at this location.

like image 40
anthonyrisinger Avatar answered Sep 17 '22 01:09

anthonyrisinger