表格标签

表格标签 - 图1<table>表格

属性不要使用标签属性,使用css定义表格样式。

  • border属性 定义表格分割线。
  • cellpadding属性 单元格内边距。
  • cellspacing属性 单元格之间的间距。

<caption>表格标题
<colgroup>给表格的列分组,对表格每列增加样式属性(每一列的共有属性)
<col />空元素,增加每一列的自有属性。
<tr>表格行
<th>table header 行单元格

<td>数据单元格
属性用于合并单元格

  • colspan属性 在一行中的合并指定列数,变成一个单元格
  • rowspan属性 指定两行的单元格,合并成一个单元格

<thead>表格页眉,给表格的行分组
<tbody>表格主体,给表格的行分组
<tfoot>表格页尾,给表格的行分组

  • 关键点1这三个标签是HTML5新增的表格标签,封装表格行,提供有用的表格语义化信息。
  • 关键点2浏览器渲染引擎会使用这三个标签,按照、、的顺序渲染表格。当数据行特别多时,将表头和结尾优先显示,用户体验更好。即使你写乱这三个标签的顺序,浏览器仍然能正确解析表格。
  • 关键点3提供屏幕阅读器的可访问性。

表格标签-UA样式表

  1. /* tables */
  2. table {
  3. display: table; //同display: block不同之处就是width默认值
  4. border-spacing: 2px;
  5. border-collapse: separate;
  6. /* XXXldb do we want this if we're border-collapse:collapse ? */
  7. box-sizing: border-box;
  8. text-indent: 0;
  9. }
  10. table[align="left"] {
  11. float: left;
  12. }
  13. table[align="right"] {
  14. float: right;
  15. text-align: start;
  16. }
  17. /* border collapse rules */
  18. /* Set hidden if we have 'frame' or 'rules' attribute.
  19. Set it on all sides when we do so there's more consistency
  20. in what authors should expect */
  21. /* Put this first so 'border' and 'frame' rules can override it. */
  22. table[rules] {
  23. border-width: thin;
  24. border-style: hidden;
  25. }
  26. /* 'border' before 'frame' so 'frame' overrides
  27. A border with a given value should, of course, pass that value
  28. as the border-width in pixels -> attr mapping */
  29. /* :-moz-table-border-nonzero is like [border]:not([border="0"]) except it
  30. also checks for other zero-like values according to HTML attribute
  31. parsing rules */
  32. table:-moz-table-border-nonzero {
  33. border-width: thin;
  34. border-style: outset;
  35. }
  36. table[frame] { //frame属性定义表格的边框
  37. border: thin hidden;
  38. }
  39. /* specificity must beat table:-moz-table-border-nonzero rule above */
  40. table[frame="void"] { border-style: hidden; }
  41. table[frame="above"] { border-style: outset hidden hidden hidden; }
  42. table[frame="below"] { border-style: hidden hidden outset hidden; }
  43. table[frame="lhs"] { border-style: hidden hidden hidden outset; }
  44. table[frame="rhs"] { border-style: hidden outset hidden hidden; }
  45. table[frame="hsides"] { border-style: outset hidden; }
  46. table[frame="vsides"] { border-style: hidden outset; }
  47. table[frame="box"],
  48. table[frame="border"] { border-style: outset; }
  49. /* Internal Table Borders */
  50. /* 'border' cell borders first */
  51. table:-moz-table-border-nonzero > * > tr > td,
  52. table:-moz-table-border-nonzero > * > tr > th,
  53. table:-moz-table-border-nonzero > * > td,
  54. table:-moz-table-border-nonzero > * > th,
  55. table:-moz-table-border-nonzero > td,
  56. table:-moz-table-border-nonzero > th
  57. {
  58. border-width: thin;
  59. border-style: inset;
  60. }
  61. /* collapse only if rules are really specified */
  62. table[rules]:not([rules="none"], [rules=""]) {
  63. border-collapse: collapse;
  64. }
  65. /* only specified rules override 'border' settings
  66. (increased specificity to achieve this) */
  67. table[rules]:not([rules=""])> tr > td,
  68. table[rules]:not([rules=""])> * > tr > td,
  69. table[rules]:not([rules=""])> tr > th,
  70. table[rules]:not([rules=""])> * > tr > th,
  71. table[rules]:not([rules=""])> td,
  72. table[rules]:not([rules=""])> th
  73. {
  74. border-width: thin;
  75. border-style: none;
  76. }
  77. table[rules][rules="none"] > tr > td,
  78. table[rules][rules="none"] > * > tr > td,
  79. table[rules][rules="none"] > tr > th,
  80. table[rules][rules="none"] > * > tr > th,
  81. table[rules][rules="none"] > td,
  82. table[rules][rules="none"] > th
  83. {
  84. border-width: thin;
  85. border-style: none;
  86. }
  87. table[rules][rules="all"] > tr > td,
  88. table[rules][rules="all"] > * > tr > td,
  89. table[rules][rules="all"] > tr > th,
  90. table[rules][rules="all"] > * > tr > th,
  91. table[rules][rules="all"] > td,
  92. table[rules][rules="all"] > th
  93. {
  94. border-width: thin;
  95. border-style: solid;
  96. }
  97. table[rules][rules="rows"] > tr,
  98. table[rules][rules="rows"] > * > tr {
  99. border-block-start-width: thin;
  100. border-block-end-width: thin;
  101. border-block-start-style: solid;
  102. border-block-end-style: solid;
  103. }
  104. table[rules][rules="cols"] > tr > td,
  105. table[rules][rules="cols"] > * > tr > td,
  106. table[rules][rules="cols"] > tr > th,
  107. table[rules][rules="cols"] > * > tr > th {
  108. border-inline-start-width: thin;
  109. border-inline-end-width: thin;
  110. border-inline-start-style: solid;
  111. border-inline-end-style: solid;
  112. }
  113. table[rules][rules="groups"] > colgroup {
  114. border-inline-start-width: thin;
  115. border-inline-end-width: thin;
  116. border-inline-start-style: solid;
  117. border-inline-end-style: solid;
  118. }
  119. table[rules][rules="groups"] > tfoot,
  120. table[rules][rules="groups"] > thead,
  121. table[rules][rules="groups"] > tbody {
  122. border-block-start-width: thin;
  123. border-block-end-width: thin;
  124. border-block-start-style: solid;
  125. border-block-start-style: solid;
  126. }
  127. /* caption inherits from table not table-outer */
  128. caption {
  129. display: table-caption;
  130. text-align: center;
  131. }
  132. table[align="center"] > caption {
  133. margin-inline-start: auto;
  134. margin-inline-end: auto;
  135. }
  136. table[align="center"] > caption[align="left"]:dir(ltr) {
  137. margin-inline-end: 0;
  138. }
  139. table[align="center"] > caption[align="left"]:dir(rtl) {
  140. margin-inline-start: 0;
  141. }
  142. table[align="center"] > caption[align="right"]:dir(ltr) {
  143. margin-inline-start: 0;
  144. }
  145. table[align="center"] > caption[align="right"]:dir(rtl) {
  146. margin-inline-end: 0;
  147. }
  148. tr {
  149. display: table-row;
  150. vertical-align: inherit;
  151. }
  152. col {
  153. display: table-column;
  154. }
  155. colgroup {
  156. display: table-column-group;
  157. }
  158. tbody {
  159. display: table-row-group;
  160. vertical-align: middle;
  161. }
  162. thead {
  163. display: table-header-group;
  164. vertical-align: middle;
  165. }
  166. tfoot {
  167. display: table-footer-group;
  168. vertical-align: middle;
  169. }
  170. /* for XHTML tables without tbody */
  171. table > tr {
  172. vertical-align: middle;
  173. }
  174. td {
  175. display: table-cell; //行内块元素
  176. vertical-align: inherit;
  177. text-align: unset;
  178. padding: 1px;
  179. }
  180. th {
  181. display: table-cell;
  182. vertical-align: inherit;
  183. font-weight: bold;
  184. padding: 1px;
  185. }
  186. tr > form:-moz-is-html, tbody > form:-moz-is-html,
  187. thead > form:-moz-is-html, tfoot > form:-moz-is-html,
  188. table > form:-moz-is-html {
  189. /* Important: don't show these forms in HTML */
  190. display: none !important;
  191. }
  192. table[bordercolor] > tbody,
  193. table[bordercolor] > thead,
  194. table[bordercolor] > tfoot,
  195. table[bordercolor] > col,
  196. table[bordercolor] > colgroup,
  197. table[bordercolor] > tr,
  198. table[bordercolor] > * > tr,
  199. table[bordercolor] > tr > td,
  200. table[bordercolor] > * > tr > td,
  201. table[bordercolor] > tr > th,
  202. table[bordercolor] > * > tr > th {
  203. border-color: inherit;
  204. }