Explain Codes LogoExplain Codes Logo

Removing unwanted table cell borders with CSS

html
responsive-design
css
best-practices
Nikita BarsukovbyNikita Barsukov·Oct 2, 2024
TLDR

If you fancy a borderless table, apply border: 0; to your <td> or <th> elements and slap on border-collapse: collapse; to the <table> element:

table { border-collapse: collapse; } td, th { border: 0; // Poof! Borders be gone! }

This banishes all borders. However, if you want to keep specific borders, just specify the border you want, e.g., border-bottom: 1px solid black;, for a subtle underline.

CSS techniques to remove table cell borders

Removing unwanted table cell borders is a common task when designing tables. Let's dive into some key techniques:

Effective use of border-collapse

The border-collapse: collapse; property is your first line of defense against pesky cell borders. It overrides the browser's default border spacing and puts you in the driver's seat.

table { border-collapse: collapse; // Uniform border control, like a brave border patrol }

Overriding default borders with ‘!important’

The border-style: hidden!important property is a fail-safe solution to enforce your no-border policy. It's like playing a trump card in a game of bridge.

td, th { border-style: hidden!important; // A hidden charm to magically disappear all borders }

Bloating default spacing

Spaces and padding can imitate borders. Counter this by setting border=0 cellpadding=0 cellspacing=0 attributes on <table>. This nukes default border and spacing impostors.

<table border="0" cellpadding="0" cellspacing="0"> <!-- your dynamite table content --> </table>

Advantage of separating ‘thead’ and ‘tbody’

Separate the <thead> and <tbody> to maintain different styles for headers and body rows without unnecessary borders.

<table> <thead> <!-- your shiny headers --> </thead> <tbody> <!-- your juicy data --> </tbody> </table>

Handling browser-specific table-border issues

Working with borderless tables often brings out some browser-specific quirks, as we are twisting the borders to our whims. Here's what you need to know:

The ‘border-spacing’ villain

border-spacing: 0; can be your trusty sidekick or your greatest nemesis, depending on browser support. While it can blast away space between cells, use it with caution on older versions of Internet Explorer.

table { border-spacing: 0; // Squeeze them cells with a big tight group hug }

The pretty embellishment: background colors

As we can't use borders to discern table sections, apply consistent background colors to thead, tr, and alternating tr for visual uniformity. A little color never hurt nobody!

thead tr, tbody tr:nth-child(odd) { background-color: #f2f2f2; // Gives it a nice turtle shell pattern, don't you think? }

The unexpected ally: ‘cellpadding’

Use cellpadding to banish default cell padding. It's a way to add white space without adding border-like spacing.

<table cellpadding="0"> <!-- your top-notch table content --> </table>