HTML <caption> 标签的 align 属性 详解
HTML基础 2023-07-12 15:14:433小码哥的IT人生shichen
HTML <caption> 标签的 align 属性
实例
带有与底部对齐的 caption 元素的表格:
<table border="1"> <caption align="bottom">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table>
完整实例【亲自试一试】:
<html> <body> <p>align="left":</p> <table border="1"> <caption align="left">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> <p>align="right":</p> <table border="1"> <caption align="right">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> <p>align="top":</p> <table border="1"> <caption align="top">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> <p>align="bottom":</p> <table border="1"> <caption align="bottom">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
定义和用法
align 属性规定 caption 元素的对齐方式。
该属性将 caption 作为块元素向表格的左边、右边、顶部、底部进行对齐。
浏览器支持
除了 Firefox,其他主流浏览器都无法正确地支持 align 属性。
注释:所有主流浏览器都支持 "top" 和 "bottom" 值。Chrome 和 Safari 不支持 "left" 和 "right"。Internet Explorer 和 Opera 只能对 caption 内部的文本(而不是整个 caption 元素)进行对齐。
兼容性注释
在 HTML 4.01 中,不赞成使用 caption 元素的 align 属性;在 XHTML 1.0 Strict DTD 中,不支持 caption 元素的 align 属性。
请使用 CSS 代替。
CSS 语法:<caption style="caption-side:left">
注释:主流浏览器对 CSS 的 caption-side 属性的支持不是很好。
完整实例【CSS 实例:定位表格标题】:
<html> <body> <p>align="left":</p> <table border="1"> <caption align="left">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> <p>align="right":</p> <table border="1"> <caption align="right">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> <p>align="top":</p> <table border="1"> <caption align="top">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> <p>align="bottom":</p> <table border="1"> <caption align="bottom">Monthly savings</caption> <tr> <th>Month</th> <th>Savings</th> </tr> <tr> <td>January</td> <td>$100</td> </tr> </table> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
在我们的 CSS 教程中,您可以找到更多有关 caption-side 属性的细节。
语法
<caption align="value">
属性值
值 | 描述 |
---|---|
left | 标题在表格的左边。 |
right | 标题在表格的右边。 |
top | 标题在表格的上边。 |
bottom | 标题在表格的下边。 |