HTML Colspan 是表格中用來將列分割到儲存格中的屬性。它允許使用者將單一表格單元格劃分為一個或多個儲存格或列的寬度。 HTML 的此屬性用作電子表格中包含的合併儲存格選項,與 Excel 相同。此屬性用於描述實際列數除以各個列。
colspan 表屬性在兩個表元素中使用,如
HTML 中的 colspan 屬性可以如下使用:
文法:
<td colspan="value">table content…</td>
文法:
<th colspan="value">table content…</th>
文法:
<ElementName cols="value">……………….</ElementName>
下面解釋HTML中colspan的程式碼實作。
代碼:
<!DOCTYPE html> <html> <head> <title>HTML colspan Attribute with <td> tag</title> <style> table, th, td { border: 2 px solid blue; border-collapse: collapse; padding: 5px; text-align:left; } </style> </head> <body> <center> <h1 style="color: blue;">State With it’s capital </h1> <h2>HTML colspan Attribute</h2> <table> <tr> <th>State </th> <th>Capital </th> </tr> <tr> <td>Andhra Pradesh</td> <td>Hyderabad</td> </tr> <tr> <td>Bihar </td> <td>Patna</td> </tr> <tr> <td>Goa </td> <td>Panaji</td> </tr> <tr> <td>Maharashtra </td> <td>Mumbai </td> </tr> <tr> <td>Nagaland</td> <td>Kohima </td> </tr> <tr> <td>Punjab </td> <td>Chandigarh </td> </tr> <tr> <td> Gujarat</td> <td> Gandhinagar</td> </tr> <tr> <td>Haryana </td> <td>Chandigarh </td> </tr> <tr> <td colspan="2">India</td> </tr> </table> </center> </body> </html>
輸出:
代碼:
<!DOCTYPE html> <html> <head> <title>HTML colspan Attribute</title> <style> table, th, td { border: 3px solid red; border-collapse: collapse; } </style> </head> <body> <center> <h1 style="color: grey;">Population</h1> <h2>HTML colspan Attribute with <th> tag</h2> <table> <tr> <th colspan="2">Gender</th> </tr> <tr> <td>Male</td> <td>18</td> </tr> <tr> <td>Female</td> <td>24</td> </tr> </table> </center> </body> </html>
輸出:
以下是不同的範例。
這個範例狀態 colspan 屬性將與表頭以及
HTML 程式碼:
<!DOCTYPE html> <html> <head> <style> table, th, td { border: 2px solid blue; border-collapse: collapse; padding:5px; } </style> </head> <body> <table> <h1 style="text-align: center;">Employee Details</h1> <tr> <th colspan="1"></th> <th colspan="2">Software</th> <th colspan="2">Hardware</th> <th colspan="2">Marketing</th> <th colspan="2">HR</th> <th colspan="3">Others</th> </tr> <tr> <th >Department</th> <th>.NET</th> <th>JAVA</th> <th>Embedded</th> <th>Mechanical</th> <th>Indoor</th> <th>Outdoor</th> <th>Onside</th> <th>Offside</th> <th>Accounts</th> <th>Assets</th> <th>Canteen</th> </tr> <tr> <td>Head Person</td> <td>John</td> <td>Disuja</td> <td>Kartik</td> <td>Gary</td> <td>Sreja</td> <td>Divya</td> <td>Sandesh</td> <td>Krutika</td> <td>P. Roy</td> <td>Mohan</td> <td>Joggy</td> </tr> <tr> <td>Experience(in years)</td> <td>17</td> <td>12</td> <td>8</td> <td>7</td> <td>12</td> <td>11</td> <td>5</td> <td>9</td> <td>7</td> <td>12</td> <td>5</td> </tr> <tr> <td>No of Teams</td> <td>5</td> <td>5</td> <td>7</td> <td>7</td> <td>3</td> <td>2</td> <td>2</td> <td>2</td> <td>5</td> <td>5</td> <td>5</td> </tr> <tr> <td>Assigned Projects/Task</td> <td>18</td> <td>15</td> <td>7</td> <td>11</td> <td>5</td> <td>5</td> <td>2</td> <td>2</td> <td>4</td> <td>10</td> <td>6</td> </tr> <tr> <td>No of employees</td> <td>182</td> <td>150</td> <td>75</td> <td>82</td> <td>34</td> <td>25</td> <td>2</td> <td>5</td> <td>4</td> <td>10</td> <td>6</td> </tr> <tr> <td>Weekly standup</td> <td>Monday</td> <td>Monday</td> <td>Friday</td> <td>Friday</td> <td>Tuseday</td> <td>Tuseday</td> <td>Everyday</td> <td>Everyday</td> <td>Friday</td> <td>Friday</td> <td>Friday</td> </tr> <tr> <td>Appriasal Month</td> <td>March</td> <td>March</td> <td>March</td> <td>March</td> <td>June</td> <td>June</td> <td>March</td> <td>March</td> <td>June</td> <td>June</td> <td>June</td> </tr> </table> </body> </html>
輸出:
在這個範例中,我們將解釋如何使用 colspan 標籤來分割列,以及使用 colspan 標籤將產生的實際輸出如下:
HTML 程式碼:
<!DOCTYPE html> <html> <head> <title>HTML colspan Attribute</title> <style> table, th, td { border: 1px solid black; border-collapse: collapse; padding:8px; } </style> </head> <body> <center> <h1 style="color: greenyellow;">Weather in India</h1> <table> <tr> <th> Temperature (in degree Celsius)</th> </tr> <tr> <td >Agartala</td> <td >17</td> </tr> <tr> <td>Amritsar</td> <td>23</td> </tr> <tr> <td>Aurangabad</td> <td>26</td> </tr> <tr> <td>Bangalore</td> <td>23</td> </tr> <tr> <td>Bhopal</td> <td>13</td> </tr> <tr> <td>Chennai</td> <td>25</td> </tr> <tr> <td>Delhi</td> <td>11</td> </tr> <tr> <td>Haridwar</td> <td>8</td> </tr> <tr> <td>Hubbali</td> <td>25</td> </tr> <tr> <td>Indore</td> <td>15</td> </tr> <tr> <td>Kanyakumari</td> <td>28</td> </tr> <tr> <td>Katra</td> <td>8</td> </tr> <tr> <td>Mumbai</td> <td>26</td> </tr> <tr> <td colspan="2"><b>Average Temperature = 19.07</b></td> </tr> </table> </center> </body> </html>
輸出:
在此範例中,我們將在
HTML 程式碼:
<!DOCTYPE html> <html> <head> <title>HTML colspan Attribute</title> <style> table, th, td { border: 2px solid black; border-collapse: collapse; padding:8px; } </style> </head> <body> <center> <h1 style="color:deepskyblue;">Pune Highlights</h1> <table> <tr> <th colspan="2"> Famous Area in Pune</th> </tr> <tr> <td colspan="1">Koregaon Park</td> <td >Kalyani nagar</td> </tr> <tr> <td colspan="1">PMC</td> <td >PCMC</td> </tr> <tr> <td colspan="2">Kothrud</td> </tr> <tr> <td colspan="2">Chakan</td> </tr> <tr> <td colspan="1">Vishrantwadi</td> <td >Lohgaon</td> </tr> <tr> <td colspan="1">Hadpsar</td> <td >Swargate</td> </tr> <tr> <td colspan="1">Decan</td> <td >Baner</td> </tr> <tr> <td colspan="1">Bavdhan</td> <td >Wakad</td> </tr> <tr> <td colspan="1">Pimpri Chinchwad</td> <td >Akurdi</td> </tr> <tr> <td colspan="2" >Vimannagar </td> </tr> <tr> <td colspan="1">Wagholi</td> <td >Katraj</td> </tr> <tr> <td colspan="1">Hinjewadi</td> <td >Khadkwasla</td> </tr> <tr> <td colspan="1">Camp</td> <td >Laxmi Road</td> </tr> </table> </center> </body> </html>
輸出:
以上是HTML 科爾斯潘的詳細內容。更多資訊請關注PHP中文網其他相關文章!