How can I apply a border only inside a table?

前端 未结 9 473
盖世英雄少女心
盖世英雄少女心 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:56

    For ordinary table markup, here's a short solution that works on all devices/browsers on BrowserStack, except IE 7 and below:

    table { border-collapse: collapse; }
    
    td + td,
    th + th { border-left: 1px solid; }
    tr + tr { border-top: 1px solid; }
    

    For IE 7 support, add this:

    tr + tr > td,
    tr + tr > th { border-top: 1px solid; }
    

    A test case can be seen here: http://codepen.io/dalgard/pen/wmcdE

    0 讨论(0)
  • 2020-11-30 17:58

    Due to mantain compatibility with ie7, ie8 I suggest using first-child and not last-child to doing this:

    table tr td{border-top:1px solid #ffffff;border-left:1px solid #ffffff;}
    
    table tr td:first-child{border-left:0;}
    
    table tr:first-child td{border-top:0;}
    

    You can learn about CSS 2.1 Pseudo-classes at: http://msdn.microsoft.com/en-us/library/cc351024(VS.85).aspx

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题