How can I apply a border only inside a table?

前端 未结 9 499
盖世英雄少女心
盖世英雄少女心 2020-11-30 17:24

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;
}
         


        
9条回答
  •  情深已故
    2020-11-30 17:59

    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.

提交回复
热议问题