I am trying to create a simple 3x3 grid (using <table> ) with all cells having a 1px border. If I just use CSS to provide all the <td> elements with a 1px border, then the inner borders will 2px and create a 2px border, so I process each <td> differently. I managed to do it this way and use nth child to reduce CSS. However, my question is that some logical way that uses even fewer CSS selectors does not work.
Here is my HTML
<!DOCTYPE html> <html> <head> <title>3x3 grid</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <table> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> </table> </body> </html>
Here is the CSS that works:
td{ border: 1px #000; width:30px; height: 30px; text-align: center; border-style: solid; } tr:nth-child(2) td{ border-top: 0px; } tr:nth-child(3) td{ border-top: 0px } td:nth-child(1){ border-right: 0px; } td:nth-child(3){ border-left: 0px; } table{ border-spacing: 0px; border-collapse: separate; }
Here is CSS that uses one smaller selector and should work, but somehow all the top cells end up without top borders.
td{ border: 1px #000; border-top: 0px; width:30px; height: 30px; text-align: center; border-style: solid; } tr:nth-child(1) td{ border-top: 1px; } td:nth-child(1){ border-right: 0px; } td:nth-child(3){ border-left: 0px; } table{ border-spacing: 0px; border-collapse: separate; }
I tested it with both Safari and Firefox. Also, please tell me if there is a better way to do this.
source share