Skip to content

Tables, borders, and border-collapse

Need a table? For your table data, not for layout? (We’re clear on that issue, right?) Okay, make one. Here’s how to do the borders using CSS.

The border property is what you use to add a border to a table with CSS. You might have a rule such as this one:

table {
border: 1px solid #333;
}

That would add a one pixel, solid border in a gray color around the table. But it wouldn’t add borders to the table cells themselves. So write this rule, too.

td, th {
border: 1px solid #333;
}

Now everything has a border. In fact, there are two borders around everything, because each individual td, th, and table has its own borders.

To remove the space between these borders and make them appear as a single border line, use border-collapse. Put it in the table rule only. The possible values for border-collapse are separate, collapse, and inherit. You want them to collapse.

table {
border: 1px solid #333;
border-collapse: collapse;
}

Now you can get rid of those inline HTML attributes in your table code that set the borders and move table presentation to the CSS, where it belongs. Peace and joy will follow you where ever you go.

Stumble it!  


4 Comments

  1. I was coding at the weekend. Sometimes you are either lazy or just need to get the job done, either way I never founfd this solution. I ended up coding cells individually border-right etc. Yep, it must have been late!!

    Monday, February 4, 2008 at 3:39 pm | Permalink
  2. Michael R. Bernstein wrote:

    Virginia, you may find this related resource of use:
    http://icant.co.uk/csstablegallery/

    Sunday, February 10, 2008 at 5:00 pm | Permalink
  3. vdebolt wrote:

    I agree. A great site. I’ve recommended it before here and I mentioned it in my book, too.

    Monday, February 11, 2008 at 6:21 am | Permalink
  4. Thanks a lot for this code, it helped out a lot on my site!

    Tuesday, December 9, 2008 at 4:16 pm | Permalink

What can you contribute?

Your email is never published nor shared. Required fields are marked *
*
*

CommentLuv badge