w3resource

CSS Properties: How to set the collapsing borders model for a table?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Declaration of HTML5 document type -->
<html>
<head>
<style>/* CSS style start*/
table {
border-collapse: collapse; /* Specifies the border collapse behavior for the table */
}
table, td, th {
border: 1px solid red; /* Sets a red solid border with 1px width for table, td, and th elements */
}
</style>
</head>
<body>
<table><!-- Start of the table -->
<tr><!-- Start of the table row -->
<th>First Name</th><!-- Table header cell containing "First Name" -->
<th>Last Name </th><!-- Table header cell containing "Last Name" -->
</tr><!-- End of the table row -->
<tr><!-- Start of another table row -->
<td>Jack</td><!-- Table data cell containing "Jack" -->
<td>Bell</td><!-- Table data cell containing "Bell" -->
</tr><!-- End of the table row -->
<tr><!-- Start of another table row -->
<td>Peter</td><!-- Table data cell containing "Peter" -->
<td>Jhon</td><!-- Table data cell containing "Jhon" -->
</tr><!-- End of the table row -->
</table><!-- End of the table -->
</body>
</html>

Explanation:

  • This HTML document creates a table with borders around its cells.
  • The CSS style block defines the style for the table, table cells (td), and table header cells (th).
  • border-collapse: collapse; specifies that table borders should be collapsed into a single border.
  • border: 1px solid red; sets a red solid border with 1px width for the table, td, and th elements.
  • Inside the body, a table is defined with three rows (tr) and two columns (th and td).
  • Table header cells (th) contain "First Name" and "Last Name" respectively.
  • Table data cells (td) contain names like "Jack", "Bell", "Peter", and "Jhon".

Live Demo:

See the Pen border-collapse-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes Yes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.