CSS Properties: How to collapse table border into a single border?
Solution:
HTML Code:
<!DOCTYPE html><!-- Declaration of HTML5 document type -->
<html>
<head>
<title>How to collapse table border into a single border</title><!-- Title of the HTML document -->
<style>/* CSS style start*/
table#mytable {
border-collapse:collapse; /* Sets the border-collapse property to collapse for the table with id "mytable" */
}
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 id="mytable"><!-- Start of the table with id "mytable" -->
<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 with id "mytable" -->
</body>
</html>
Explanation:
- This HTML document creates a table with collapsed borders.
- The CSS style block defines the style for the table with id "mytable" and its cells (td and 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 id "mytable" and 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-collapse-answer by w3resource (@w3resource) on CodePen.
See the solution in the browser
Supported browser
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics