如何在 HTML 中创建合并边框?


我们使用 border-collapse 属性在 HTML 中创建合并边框。border-collapse 是一个 CSS 属性,用于设置表格边框在 HTML 中是合并成单个边框还是保持分离。

Border-collapse 属性有四个值:separate、collapse、initial、inherit。

使用 collapse 值

如果将 collapse 作为 border-collapse 属性的值传递,则表格的边框将简单地合并成单个边框。以下是创建 HTML 中合并边框的语法。

border-collapse: collapse;

示例 1

在下面给出的示例中,我们尝试在 HTML 中创建一个合并边框 -

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table, tr, th, td { border: 1px solid black; border-collapse: collapse; } </style> </head> <body> <h2>Tables in HTML</h2> <table style="width: 100%"> <tr> <th>First Name </th> <th>Job role</th> </tr> <tr> <td>Tharun</td> <td>Content writer</td> </tr> <tr> <td>Akshaj</td> <td>Content writer</td> </tr> </table> </body> </html>

以下是上面示例程序的输出。

示例 2

让我们再看一个使用 collapse 作为 border-collapse 属性值的示例 -

<!DOCTYPE html> <html> <head> <style> table {border-collapse: collapse; } table, td, th { border: 1px solid blue; } </style> </head> <body> <h1>Technologies</h1> <table> <tr> <th>IDE</th> <th>Database</th> </tr> <tr> <td>NetBeans IDE</td> <td>MySQL</td> </tr> </table> </body> </html>

使用 separate 作为值

如果我们通过将 **separate** 作为 **border-collapse 属性**的值来创建合并边框,则各个单元格将被包裹在单独的边框中。

border-collapse:separate;

示例 1

在下面给出的示例中,我们尝试在 HTML 中创建一个单独的合并边框。

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table,tr,th,td { border:1px solid black; border-collapse:separate; } </style> </head> <body> <h2>Tables in HTML</h2> <table style="width: 100%"> <tr> <th >First Name </th> <th>Job role</th> </tr> <tr> <td >Tharun</td> <td >Content writer</td> </tr> <tr> <td >Akshaj</td> <td >Content writer</td> </tr> </table> </body> </html>

以下是上面示例程序的输出。

示例 2

让我们再看一个使用 separate 作为 border-collapse 属性值的示例 -

<!DOCTYPE html> <html> <head> <style> table { border-collapse: separate; } table, td, th { border: 1px solid blue; } </style> </head> <body> <h1>Technologies</h1> <table> <tr> <th>IDE</th> <th>Database</th> </tr> <tr> <td>NetBeans IDE</td> <td>MySQL</td> </tr> </table> </body> </html>

使用 initial 作为值

如果将 **initial** 作为 border-collapse 属性的值传递,则将其设置为其默认值,即 separate。以下是使用 HTML 中 border-collapse 属性的 initial 属性的语法。

border-collapse:initial;

示例

下面给出一个示例,它在 HTML 中使用了 border-collapse 属性的 initial 属性。

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> table,tr,th,td { border:1px solid black; border-collapse:initial; } </style> </head> <body> <h2>Tables in HTML</h2> <table style="width: 100%"> <tr> <th >First Name </th> <th>Job role</th> </tr> <tr> <td >Tharun</td> <td >Content writer</td> </tr> <tr> <td >Akshaj</td> <td >Content writer</td> </tr> </table> </body> </html>

更新于: 2022年10月18日

15K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.