HTML DOM Style borderCollapse Property


The borderCollapse property is used for setting or returning whether <table>elements should have shared or separate borders. It can take two values, separate and collapse.

Syntax

Following is the syntax for −

Setting the borderCollapse property −

object.style.borderCollapse = "separate|collapse|initial|inherit"

Values

The property values are explained as follows −

Sr.NoValues & Description
1separate
This is the default value and have each table cell separate borders.
2collapse
This specifies the border to not be drawn between table cell values.
3initial
For setting this property to initial value.
4inherit
To inherit the parent property value

Example

Let us look at an example for the borderCollapse property −

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
   div {
      display: flex;
      float: left;
   }
   table {
      border: 3px solid black;
   }
   td {
      border: 3px solid lightgreen;
   }
   th {
      border: 3px solid lightblue;
   }
</style>
<script>
   function collapseBorder(){
      document.getElementById("t1").style.borderCollapse="collapse";
      document.getElementById("Sample").innerHTML="The table borders are now collapsed";
   }
</script>
</head>
<body>
<table id="t1">
<tr>
<th>FRUITS</th>
<th>PRICE </th>
</tr>
<tr>
<td>MANGO </td>
<td>40</td>
</tr>
<tr>
<td>APPLE</td>
<td>50</td>
</tr>
</table>
<p>Collapse the above table borders by clicking the below button</p>
<button onclick="collapseBorder()">COLLAPSE BORDER</button>
<p id="Sample"></p>
</body>
</html>

Output

This will produce the following output −

On clicking the COLLAPSE BORDER button −

Updated on: 15-Feb-2021

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements