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.No | Values & Description |
---|---|
1 | separate This is the default value and have each table cell separate borders. |
2 | collapse This specifies the border to not be drawn between table cell values. |
3 | initial For setting this property to initial value. |
4 | inherit To inherit the parent property value |
Example
Let us look at an example for the borderCollapse property −
<!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 −