How To Stylize A Table Border In HTML

Styling rows and columns – crafting the visual gridwork of a webpage – that’s where the magic begins.

Ever stared at a blank table, yearning to bring it to life? To transform those plain borders into something truly remarkable?

When it comes to web design, knowing how to stylize a table border in HTML isn’t just about aesthetics. It’s about clarity, readability, and guiding the user’s eyes to what matters most.

You’ll discover the artistry behind HTML and CSS, learn the essential tricks for creating sleek, responsive tables, and understand advanced customization techniques to make border designs sing.

By the end of this exploration, you’ll have the toolkit to turn any mundane table into a standout feature on your website, with insights into the HTML and CSS frameworks, border properties, and responsive design principles.

Basic HTML Table Structure

Creating an HTML Table

In the vast universe of web design, HTML tables are fundamental building blocks. They’re the unsung heroes of structured data.

Basic HTML Table Tags (<table><tr><td><th>)

Understanding the DNA of an HTML table begins with these core tags:

  • <table>: The grand container of all table elements.
  • <tr> (table row): Within the table, there’s the <tr> tag. This tag is like the backbone, supporting each row.
  • <td> (table data): Nested in each row, the <td> tags hold the individual data cells.
  • <th> (table header): When a row needs a header, in comes the <th> tag – bold and centered, showing off its importance.

Example of a Simple HTML Table

Imagine a table that gently whispers, “simplicity”. Here’s an example, stripped to its very essence.

<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
    <td>Data 3</td>
  </tr>
  <tr>
    <td>Data 4</td>
    <td>Data 5</td>
    <td>Data 6</td>
  </tr>
</table>

Adding Borders to HTML Tables

Using the HTML Border Attribute

To style an HTML table, traditionalists might leap towards adding borders with the HTML border attribute. It’s like painting with watercolors – simple and readily accessible.

Syntax and Implementation

Here’s the syntax, plain and straightforward:

<table border="1">
  <!-- rows and cells go here -->
</table>

In the blink of an eye, borders appear. A straightforward approach — like grabbing the first pencil you see.

Example with HTML Border Attribute

Imagine a table brought to life with borders. Just a touch of the border attribute:

<table border="1">
  <tr>
    <th>Item</th>
    <th>Quantity</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Apples</td>
    <td>10</td>
    <td>$1.00</td>
  </tr>
  <tr>
    <td>Oranges</td>
    <td>5</td>
    <td>$2.00</td>
  </tr>
</table>

A burst of borders appears around each cell, each containing a story told in data.

Using CSS for Table Borders

Stepping into CSS – the upgrade, the redux. CSS border properties provide a symphony of possibilities. It’s like switching from a bicycle to a jet plane.

Introduction to CSS Border Property

CSS, our versatile friend, comes with the magical border property. Instead of being built-in, it’s like an extension pack. With border, you unlock widths, colors, styles, and so much more – truly styling a table border in HTML.

Example with CSS Border Property

Let’s weave some magic with CSS.

<style>
  table, th, td {
    border: 1px solid black;
  }
</style>

<table>
  <tr>
    <th>Item</th>
    <th>Quantity</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>Bananas</td>
    <td>6</td>
    <td>$1.20</td>
  </tr>
  <tr>
    <td>Grapes</td>
    <td>2</td>
    <td>$3.50</td>
  </tr>
</table>

Border Styles and Customizations

Collapsing Table Borders

A neat trick up the sleeve—collapsing borders can instantly change the visual vibe.

Understanding border-collapse Property

This is where CSS border-collapse property steps in. This property can merge the borders of a table into one unified work of simplicity and elegance.

table {
  border-collapse: collapse;
}

This unassuming line delicately tells browsers, “Hey, let’s combine those borders.” Simple yet powerful, just like a minimalist’s dream.

Example of Collapsed Table Borders

Imagine a table that whispers sophistication through its neatly unified borders:

<style>
  table {
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid black;
  }
</style>

<table>
  <tr>
    <th>Weekday</th>
    <th>Task</th>
  </tr>
  <tr>
    <td>Monday</td>
    <td>Meeting</td>
  </tr>
  <tr>
    <td>Tuesday</td>
    <td>Workshop</td>
  </tr>
</table>

All borders merged into a harmonious, single-pathway grid.

Styling Table Borders

Let’s dive headfirst into a sea of border possibilities.

Border Width

First stop, the land of thickness.

th, td {
  border-width: 2px;
}

A line, bold and confident, makes a statement.

Border Color

Color. Because who said borders should always be black?

th, td {
  border-color: blue;
}

Now, you’ve got a cool blue frame around each cell, making data pop.

Border Style

The fashion show of borders:

  • Solid
th, td {
  border-style: solid;
}
  • Dotted
th, td {
  border-style: dotted;
}
  • Dashed
th, td {
  border-style: dashed;
}
  • Double
th, td {
  border-style: double;
}
  • Groove
th, td {
  border-style: groove;
}
  • Ridge
th, td {
  border-style: ridge;
}
  • Inset
th, td {
  border-style: inset;
}
  • Outset
th, td {
  border-style: outset;
}
  • None
th, td {
  border-style: none;
}
  • Hidden
th, td {
  border-style: hidden;
}

Example of Various Border Styles

Visions danced through my mind when seeing all these styles in action:

<style>
  table {
    border-collapse: collapse;
  }
  th, td {
    border-width: 2px;
    border-color: green;
    border-style: solid;
  }
  th {
    border-style: double;
  }
  td.dotted {
    border-style: dotted;
  }
  td.dashed {
    border-style: dashed;
  }
</style>

<table>
  <tr>
    <th>Type</th>
    <th>Example</th>
  </tr>
  <tr>
    <td class="dotted">Dotted</td>
    <td class="dotted">Dotted border</td>
  </tr>
  <tr>
    <td class="dashed">Dashed</td>
    <td class="dashed">Dashed border</td>
  </tr>
</table>

A table wearing multiple outfits, each cell showcasing a new style.

Adding Rounded Borders

Ah, the finishing touch—rounded borders. The elegance of a subtle curve, achieved with border-radius.

Using border-radius Property

th, td {
  border-radius: 10px;
}

Instantly, corners soften, and the table stands transformed, approachable.

Example with Rounded Borders

Visualize a table that’s as smooth as jazz:

<style>
  table {
    border-collapse: separate;
  }
  th, td {
    border: 2px solid purple;
    border-radius: 15px;
  }
</style>

<table>
  <tr>
    <th>Event</th>
    <th>Date</th>
  </tr>
  <tr>
    <td>Conference</td>
    <td>March 10</td>
  </tr>
  <tr>
    <td>Workshop</td>
    <td>April 22</td>
  </tr>
</table>

 

Your beautiful data deserves to be online

wpDataTables can make it that way. There’s a good reason why it’s the #1 WordPress plugin for creating responsive tables and charts.

An actual example of wpDataTables in the wild

And it’s really easy to do something like this:

  1. You provide the table data
  2. Configure and customize it
  3. Publish it in a post or page

And it’s not just pretty, but also practical. You can make large tables with up to millions of rows, or you can use advanced filters and search, or you can go wild and make it editable.

“Yeah, but I just like Excel too much and there’s nothing like that on websites”. Yeah, there is. You can use conditional formatting like in Excel or Google Sheets.

Did I tell you you can create charts too with your data? And that’s only a small part. There are lots of other features for you.

Methods of Applying CSS

Inline CSS

Ever felt the need for speed? That’s inline CSS for you. Quick and to the point.

Syntax and Usage

When the pursuit of instant gratification is strong, embed those styles directly in your tags. All within the confines of HTML, whispering to the elements directly.

<table style="border: 2px solid red;">
  <tr>
    <th style="border: 1px solid blue;">Day</th>
    <th style="border: 1px solid blue;">Activity</th>
  </tr>
  <tr>
    <td style="border: 1px solid green;">Monday</td>
    <td style="border: 1px solid green;">Gym</td>
  </tr>
</table>

Everything right there, like painting each part while standing as close as possible.

Example of Inline CSS for Table Borders

Consider this:

<table style="border: 3px dashed black; width: 80%;">
  <tr>
    <th style="border: 1px solid black; padding: 10px;">Name</th>
    <th style="border: 1px solid black; padding: 10px;">Role</th>
  </tr>
  <tr>
    <td style="border: 1px solid grey; padding: 10px;">Alice</td>
    <td style="border: 1px solid grey; padding: 10px;">Developer</td>
  </tr>
  <tr>
    <td style="border: 1px solid grey; padding: 10px;">Bob</td>
    <td style="border: 1px solid grey; padding: 10px;">Designer</td>
  </tr>
</table>

Color-coded borders for each cell, like jazz riffs painted on a visual canvas.

External CSS

Now comes the symphony, the grand orchestration of design – external CSS. A scalable, centralized approach.

Benefits of Using External CSS

Why scatter art when it can be centralized? With external CSS, the elegance of updating styles in one place can’t be overstated.

  • Scalability: Your style guide grows with your project.
  • Maintainability: Change one file, refresh, and voilà – your entire site reflects the update.
  • Separation of Concerns: Keep structure (HTML) and style (CSS) distinct, like yin and yang.

Syntax and Implementation

Linking the style sheets to your HTML? It’s like calling your best friend and saying, “Hey, let’s hang out.”

<head>
  <link rel="stylesheet" href="styles.css">
</head>

A simple, elegant line. The bridge between your HTML and the realm of CSS.

Example with External CSS

Let’s say you’ve got a styles.css file. Inside, it’s a treasure trove of wonders.

/* styles.css */
table {
  border-collapse: collapse;
  width: 100%;
}

th, td {
  border: 2px solid purple;
  padding: 15px;
}

th {
  background-color: #f2f2f2;
}

And your HTML? A minimalist whisper, leaning on its robust partner.

<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <table>
    <tr>
      <th>Planet</th>
      <th>Diameter</th>
    </tr>
    <tr>
      <td>Earth</td>
      <td>12,742 km</td>
    </tr>
    <tr>
      <td>Mars</td>
      <td>6,779 km</td>
    </tr>
  </table>
</body>

Advanced Border Customizations

Pharma table created with wpDataTables

Combining Multiple Styles

Borders don’t have to be mundane. Think of them as a painter’s brush strokes – you can mix, match, and layer to create something uniquely yours.

Creating Complex Border Designs

Dabbling with options, you can turn a simple border into a canvas of complexities. It’s the art of blending various styles, line weights, and colors to make each cell a masterpiece.

th, td {
  border-top: 2px solid black;
  border-right: 2px dashed blue;
  border-bottom: 2px double green;
  border-left: 2px groove purple;
  padding: 10px;
}

Every side of a cell distinct, every line telling a part of a story.

Example of Combined Border Styles

Picture this – a table showcasing an array of combined styles, reflecting depth and texture.

<style>
  table {
    border-collapse: collapse;
  }
  th, td {
    border-top: 2px solid black;
    border-right: 2px dashed blue;
    border-bottom: 2px double green;
    border-left: 2px groove purple;
    padding: 10px;
  }
</style>

<table>
  <tr>
    <th>Art Form</th>
    <th>Complexity</th>
  </tr>
  <tr>
    <td>Painting</td>
    <td>Advanced</td>
  </tr>
  <tr>
    <td>Sculpture</td>
    <td>Medium</td>
  </tr>
</table>

A graphical symphony, with borders playing the leading roles. Each header and cell sits in a distinct frame – solid, dashed, double, groove – a wondrous mosaic.

Customizing Borders for Specific Elements

Thinking beyond uniformity, you can tailor borders for different elements, inject personality into headers and data cells.

Table Header (<th>) Borders

Headers – bold, distinct, the guiding stars of your table.

th {
  border: 3px solid darkred;
  background-color: #f0f0f0;
  text-transform: uppercase;
  padding: 12px;
}

A strong frame for the alpha elements, setting them apart.

Table Data (<td>) Borders

Now, about the data cells – let’s give them their unique twist.

td {
  border: 1px dotted darkgreen;
  padding: 8px;
}

Each piece of data wrapped in an understated yet chic dotted line, creating a pleasant contrast to the strong header borders.

Example of Customized Borders for Specific Elements

An example where headers and data cells stand in harmony yet distinct, each with their own flair.

<style>
  table {
    border-collapse: collapse;
    width: 100%;
  }
  th {
    border: 3px solid darkred;
    background-color: #f0f0f0;
    text-transform: uppercase;
    padding: 12px;
  }
  td {
    border: 1px dotted darkgreen;
    padding: 8px;
  }
</style>

<table>
  <tr>
    <th>Component</th>
    <th>Status</th>
  </tr>
  <tr>
    <td>Engine</td>
    <td>Operational</td>
  </tr>
  <tr>
    <td>Wheels</td>
    <td>In Use</td>
  </tr>
</table>

FAQ On Stylizing A Table Border In HTML

How can I add a simple border to an HTML table?

Just need to set the border attribute directly in your table tag. Simple:

<table border="1">
  <!-- rows and cells here -->
</table>

There you go! A straightforward way to get that basic border around your table.

How do I use CSS to style table borders?

CSS does the heavy lifting. Use the border property in your styles:

table, th, td {
  border: 1px solid black;
}

This way, you achieve consistent styling for the entire table, including table data and table headers.

What is the border-collapse CSS property, and why use it?

border-collapse merges the borders between table cells. This unifies your borders.

table {
  border-collapse: collapse;
}

It cleans up the design, creating a seamless grid without double borders.

How can I change the border color of my table?

Setting the border color is simple with CSS. Just add:

th, td {
  border: 1px solid blue;
}

This gives each cell a nice, blue border. Tweak the color to fit your design.

How do I create different border styles?

CSS allows for various styles like solid, dashed, dotted:

th, td {
  border-style: dotted;
}

Try out different styles for a unique look for each table element.

Can I make the table borders rounded?

Yes! Use the border-radius property for adding smooth, rounded edges.

th, td {
  border-radius: 10px;
}

This softens your table, making it visually more pleasing.

How do I apply different border styles to specific table elements?

Style table headers and table data separately:

th {
  border: 2px solid red;
}
td {
  border: 1px dotted blue;
}

This gives your table a customized touch, highlighting important sections.

What’s the syntax for inline CSS for table borders?

Inline CSS places styles directly within the HTML tags:

<td style="border: 1px solid black;">Data</td>

Quick and effective for small adjustments, but not scalable.

How do I use external CSS to style table borders?

Link an external CSS file in your HTML:

<head>
  <link rel="stylesheet" href="styles.css">
</head>

Then, define your styles in styles.css for a more maintainable approach.

How can I use multiple border styles in one table?

Mix and match different borders using CSS properties.

th, td {
  border-top: 2px solid black;
  border-right: 2px dashed blue;
  border-bottom: 2px double green;
  border-left: 2px groove purple;
}

This creates a visually dynamic table with varying border styles.

Conclusion

Mastering how to stylize a table border in HTML unveils a world of creative possibilities. By leveraging HTML and CSS together, we transform mundane tables into data-rich visuals that captivate and guide users.

Whether you’re employing the straightforward border attribute for quick fixes or diving into the deep complexity of CSS properties like border-collapseborder-radius, and intricate border styles – each method brings its charm.

Let’s not forget the flexibility of Inline CSS for one-off adjustments versus the scalability of External CSS for large projects. Each technique has its nuance and application, making your webpage not just functional but beautifully structured.


Milan Jovanovic
Milan Jovanovic

Product Lead

Articles: 281