Technology Sharing

[HTML Introduction] Lesson 10 - Table, that is, table tag

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

In this section, we will talk about tables in HTML. For example, we often see student transcripts, such as Excel, which has cells one by one, these are all tables.

The table tag name is table

Table of contents

1 Some sub-tags in the table

1.1 Header Area

1.2 Table Content Area

1.3 Rows and columns

2 A little practical experience

2.1 Make a simple transcript

2.2 Add border lines

2.3 Cross-bank

2.4 Spanning Columns


1 Some sub-tags in the table

1.1 Header Area

For example, the top header of a transcript must havestudent IDstudent namesujectscoreOverall resultThese fields, right.

These fields will be placed in the table header, which requires a thead tag.

1.2 Table Content Area

Then the table content area needs to have more detailed data. For example, the student number is 001, the name is Zhang San, the subject is mathematics, and the score is 80. If there are no other subjects, the total score is 80. If there are other subjects, add them up, right?

All of this content will be placed in the table content area, which requires a tbody tag.

1.3 Rows and columns

Tables are arranged in rows, and then cut into columns. After cutting, they become cells, piece by piece, in the shape of small squares.

Also, note:The rows contain the columns, remember this

The row label name is tr  。

The cell labels for the table header are th , the cell tags for the table body area aretd

2 A little practical experience

2.1 Make a simple transcript

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>我的第一个网页</title>
  6. <style type="text/css">
  7. </style>
  8. </head>
  9. <body>
  10. <table>
  11. <thead>
  12. <tr>
  13. <th>学号</th>
  14. <th>姓名</th>
  15. <th>语文成绩</th>
  16. <th>数学成绩</th>
  17. <th>HTML成绩</th>
  18. <th>总成绩</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <tr>
  23. <td>1</td>
  24. <td>张三</td>
  25. <td>80</td>
  26. <td>90</td>
  27. <td>96</td>
  28. <td>266</td>
  29. </tr>
  30. <tr>
  31. <td>2</td>
  32. <td>李四</td>
  33. <td>71</td>
  34. <td>80</td>
  35. <td>100</td>
  36. <td>251</td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </body>
  41. </html>

2.2 Add border lines

This display is a bit messy, add a border line to make the table look better. border="1" , see the code below, then refresh the webpage to see if it looks much better.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>我的第一个网页</title>
  6. <style type="text/css">
  7. </style>
  8. </head>
  9. <body>
  10. <table border="1">
  11. <thead>
  12. <tr>
  13. <th>学号</th>
  14. <th>姓名</th>
  15. <th>语文成绩</th>
  16. <th>数学成绩</th>
  17. <th>HTML成绩</th>
  18. <th>总成绩</th>
  19. </tr>
  20. </thead>
  21. <tbody>
  22. <tr>
  23. <td>1</td>
  24. <td>张三</td>
  25. <td>80</td>
  26. <td>90</td>
  27. <td>96</td>
  28. <td>266</td>
  29. </tr>
  30. <tr>
  31. <td>2</td>
  32. <td>李四</td>
  33. <td>71</td>
  34. <td>80</td>
  35. <td>100</td>
  36. <td>251</td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </body>
  41. </html>

2.3 Cross-bank

Of course, we can also do a cross-row display. For example, if Zhang San and Li Si are in Class 1, we can put them in the same big row, and then the students in Class 1 can occupy 2 rows.

Then add Class 2, with Wang Wu and Zhao Liu in it, and let them occupy one large row as well. In this way, Class 2 will have two small rows.

Here you need to set rowspan Attributes.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>我的第一个网页</title>
  6. <style type="text/css">
  7. </style>
  8. </head>
  9. <body>
  10. <table border="1">
  11. <thead>
  12. <tr>
  13. <th>学号</th>
  14. <th>班级</th>
  15. <th>姓名</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <tr>
  20. <td>1</td>
  21. <td rowspan="2">1班</td>
  22. <td>张三</td>
  23. </tr>
  24. <tr>
  25. <td>2</td>
  26. <td>李四</td>
  27. </tr>
  28. <tr>
  29. <td>3</td>
  30. <td rowspan="2">2班</td>
  31. <td>王五</td>
  32. </tr>
  33. <tr>
  34. <td>4</td>
  35. <td>赵六</td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. </body>
  40. </html>

Look at the code above. We set the rowspan attribute for the td cell. Then, for the second cell of class 1, there is no need to add the same td code.

2.4 Spanning Columns

Cross-column means that several columns have the same characteristics, so it is enough to display one column of information. Just like the cross-row just now, the purpose is not to display redundant information, because they have the same information attributes, both are Class 1, so it is enough to display one Class 1 across the row.

Cross-column means merging several columns. Let's take an example.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>我的第一个网页</title>
  6. <style type="text/css">
  7. </style>
  8. </head>
  9. <body>
  10. <table border="1">
  11. <thead>
  12. <tr>
  13. <th>学号</th>
  14. <th>班级</th>
  15. <th>姓名</th>
  16. <th>成绩</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr>
  21. <td>1</td>
  22. <td rowspan="2">1班</td>
  23. <td>张三</td>
  24. <td>90</td>
  25. </tr>
  26. <tr>
  27. <td>2</td>
  28. <td>李四</td>
  29. <td>80</td>
  30. </tr>
  31. <tr>
  32. <td colspan="4">1班总人数:2,总成绩:170</td>
  33. </tr>
  34. <tr>
  35. <td>3</td>
  36. <td rowspan="2">2班</td>
  37. <td>王五</td>
  38. <td>70</td>
  39. </tr>
  40. <tr>
  41. <td>4</td>
  42. <td>赵六</td>
  43. <td>100</td>
  44. </tr>
  45. <tr>
  46. <td>4</td>
  47. <td>冯七</td>
  48. <td>75</td>
  49. </tr>
  50. <tr>
  51. <td colspan="4">2班总人数:3,总成绩:245</td>
  52. </tr>
  53. </tbody>
  54. </table>
  55. </body>
  56. </html>

Haha, the code above,In fact, the effect of this screenshot cannot be achievedIf you get the code and find a problem, remembertell meoh.