css属性table
2015-11-05 09:32
621 查看
table的属性:
HTML <table> cellpadding 属性
把表格单元边沿与单元内容之间的间距设置为 10 像素:cellpadding 属性规定单元边沿与单元内容之间的空间,以像素计。
注意:请勿将该属性与 cellspacing 属性相混淆,cellspacing 属性规定的是单元之间的空间。
border-collapse
用于表格属性, 表示表格的两边框合并为一条。
可能的值
值 | 描述 |
separate | 默认值。边框会被分开。不会忽略 border-spacing 和 empty-cells 属性。 |
collapse | 如果可能,边框会合并为一个单一的边框。会忽略 border-spacing 和 empty-cells 属性。 |
inherit | 规定应该从父元素继承 border-collapse 属性的值。 |
折
-----------------------------------------------------------------------------------------------CSS如何设置table表格边框样式
对table设置css样式边框,分为几种情况:
1、只对table设置边框
2、对td设置边框
3、对table和td技巧性设置表格边框
4、对table和td设置背景,实现完美表格边框
-----------------------------------------------------------------------------
一、只对表格table标签设置边框 - TOP
只对table标签设置border(边框)样式,将让此表格最外层table一个边框,而表格内部不产生边框样式。案例详细如下:
1、对应css代码
<style>
.table-a table{border:1px solid #F00}
/* css注释:只对table标签设置红色边框样式 */
</style>
2、对应html代码片段
<div class="table-a">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="105">站名</td>
<td width="181">网址</td>
<td width="112">说明</td>
</tr>
<tr>
<td>DIVCSS5</td>
<td>www.divcss5.com</td>
<td>CSS学习</td>
</tr>
<tr>
<td>CSS5</td>
<td>www.css5.com.cn</td>
<td>CSS切图</td>
</tr>
</table>
</div>
3、案例截图
css对table设置边框案例截图-仅对table标签设置边框
4、在线演示
查看案例
二、对td设置边框 - TOP
对table表格td设置边框样式,表格对象内td将实现边框样式,但中间部分td会导致出现双边框。详细案例教程如下:
1、对应css代码
<style>
.table-b table td{border:1px solid #F00}
/* css注释:只对table td标签设置红色边框样式 */
</style>
2、对应html源代码片段
<div class="table-b">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="105">站名</td>
<td width="181">网址</td>
<td width="112">说明</td>
</tr>
<tr>
<td>DIVCSS5</td>
<td>www.divcss5.com</td>
<td>CSS学习</td>
</tr>
<tr>
<td>CSS5</td>
<td>www.css5.com.cn</td>
<td>CSS切图</td>
</tr>
</table>
3、案例截图
对td设置边框后,中间出现双边框样式 案例截图
4、在线演示
查看案例
三、对table和td技巧性设置表格边框 - TOP
如上第二点,只对表格对象td设置单一边框样式,中间部分td与td标签之间就会出现双边框现象。解决方法:对td只设置两个边的边框,对table也设置两个边的边框样式。
解释:对td设置左与上边框,这样td与td相邻就会只出现单一边框样式,这样就会出现表格右侧和下部没有边框,这个时候我们设置table右和下 边框解决显示右侧和下侧td剩下未显示边框。
1、对应css代码:
<style>
.table-c table{border-right:1px solid #F00;border-bottom:1px solid #F00}
.table-c table td{border-left:1px solid #F00;border-top:1px solid #F00}
/*
css 注释:
只对table td设置左与上边框;
对table设置右与下边框;
为了便于截图,我们将css 注释说明换行排版
*/
</style>
2、对应html源代码片段:
<div class="table-c">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="105">站名</td>
<td width="181">网址</td>
<td width="112">说明</td>
</tr>
<tr>
<td>DIVCSS5</td>
<td>www.divcss5.com</td>
<td>CSS学习</td>
</tr>
<tr>
<td>CSS5</td>
<td>www.css5.com.cn</td>
<td>CSS切图</td>
</tr>
</table>
3、表格边框实现案例截图
CSS完美实现html table表格边框样式截图
4、在线演示
查看案例
推荐此方法设置table表格边框样式。
四、对table和td设置背景,实现完美表格边框 - TOP
1、基础设置1)、先设置table css背景为红色
2)、设置table单元之间间距为1
使用DW软件辅助设置table表格单元间距为1,具体DW软件可视化操作步骤简要说明,首先(视图模式)选中表格后,对应DW软件编辑窗口底部会“属性”面板会出现对应table表格属性设置地方,我们将“间隔”填写为“1”。这个时候我们会看到table表格标签里cellspacing值为“1”(cellspacing="1")。
借助DW软件设置表格单元之间间距
或
直接对<table>标签内cellspacing="1"即可,得到:
<table width="400" border="0" cellspacing="1" cellpadding="0">
3)、设置table td背景为白色
2、css代码:
<style>
.table-d table{ background:#F00}
.table-d table td{ background:#FFF}
/*
css注释:设置table背景为红色,td背景为白色 */
</style>
3、对应html源代码:
<div class="table-d">
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td width="105">站名</td>
<td width="181">网址</td>
<td width="112">说明</td>
</tr>
<tr>
<td>DIVCSS5</td>
<td>www.divcss5.com</td>
<td>CSS学习</td>
</tr>
<tr>
<td>CSS5</td>
<td>www.css5.com.cn</td>
<td>CSS切图</td>
</tr>
</table>
</div>
4、表格边框案例截图
设置表格背景颜色实现边框样式截图
五、css table表格边框实现总结 - TOP
以上四种方式实现table表格边框样式方法,推荐使用第三和第四种方法来解决表格边框样式。希望DIVCSS5整理总结html table边框布局方法对大家有帮助并能掌握,平时需要时灵活运用。所有方式在线演示:
查看案例
---------------------------------------------------------------------------------------------------------------------------------
border-collapse 用于表格属性,
表示表格的两边框合并为一条。
可能的值
值 | 描述 |
separate | 默认值。边框会被分开。不会忽略 border-spacing 和 empty-cells 属性。 |
collapse | 如果可能,边框会合并为一个单一的边框。会忽略 border-spacing 和 empty-cells 属性。 |
inherit | 规定应该从父元素继承 border-collapse 属性的值。 |
折叠
1. 单像素边框CSS表格
这是一个很常用的表格样式。border-collapse: collapse;
源代码:
<!-- CSS goes in the document HEAD or added to your external stylesheet --> <style type="text/css"> table.gridtable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #666666; border-collapse: collapse; } table.gridtable th { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; } table.gridtable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; } </style> <!-- Table goes in the document BODY --> <table class="gridtable"> <tr> <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th> </tr> <tr> <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td> </tr> <tr> <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td> </tr> </table>
2. 带背景图的CSS样式表格
和上面差不多,不过每个格子里多了背景图。cell-blue.jpg
cell-grey.jpg
1. 下载上面两张图,命名为cell-blue.jpg和cell-grey.jpg
2. 拷贝下面的代码到你想要的地方,记得修改图片url
<!-- CSS goes in the document HEAD or added to your external stylesheet --> <style type="text/css"> table.imagetable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.imagetable th { background:#b5cfd2 url('cell-blue.jpg'); border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } table.imagetable td { background:#dcddc0 url('cell-grey.jpg'); border-width: 1px; padding: 8px; border-style: solid; border-color: #999999; } </style> <!-- Table goes in the document BODY --> <table class="imagetable"> <tr> <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th> </tr> <tr> <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td> </tr> <tr> <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td> </tr> </table>
3. 自动换整行颜色的CSS样式表格(需要用到JS)
这个CSS样式表格自动切换每一行的颜色,在我们需要频繁更新一个大表格的时候很有用。代码:
<!-- Javascript goes in the document HEAD --> <script type="text/javascript"> function altRows(id){ if(document.getElementsByTagName){ var table = document.getElementById(id); var rows = table.getElementsByTagName("tr"); for(i = 0; i < rows.length; i++){ if(i % 2 == 0){ rows[i].className = "evenrowcolor"; }else{ rows[i].className = "oddrowcolor"; } } } } window.onload=function(){ altRows('alternatecolor'); } </script> <!-- CSS goes in the document HEAD or added to your external stylesheet --> <style type="text/css"> table.altrowstable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9; border-collapse: collapse; } table.altrowstable th { border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; } table.altrowstable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; } .oddrowcolor{ background-color:#d4e3e5; } .evenrowcolor{ background-color:#c3dde0; } </style> <!-- Table goes in the document BODY --> <table class="altrowstable" id="alternatecolor"> <tr> <th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th> </tr> <tr> <td>Text 1A</td><td>Text 1B</td><td>Text 1C</td> </tr> <tr> <td>Text 2A</td><td>Text 2B</td><td>Text 2C</td> </tr> </tr> <tr> <td>Text 3A</td><td>Text 3B</td><td>Text 3C</td> </tr> <tr> <td>Text 4A</td><td>Text 4B</td><td>Text 4C</td> </tr> <tr> <td>Text 5A</td><td>Text 5B</td><td>Text 5C</td> </tr> </table> <!-- The table code can be found here: http://www.textfixer/resources/css-tables.php#css-table03 -->
4. 鼠标悬停高亮的CSS样式表格 (需要JS)
纯CSS显示表格高亮在IE中显示有问题,所以这边使用了JS来做高亮(由于csdn博客限制了js的使用,我会在近期将博客迁移放到自己的web主机上)。<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">onmouseout="this.style.backgroundColor='#d4e3e5';</span>
onmouseover="this.style.backgroundColor='#ffff66';"
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';"> <td>Item 1A</td><td>Item 1B</td><td>Item 1C</td> </tr>
有一点要小心的是,不要定义格子的背景色。
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
table.hovertable {
font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
}
table.hovertable th {
background-color:#c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
table.hovertable tr {
background-color:#d4e3e5;
}
table.hovertable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}
</style>
<!-- Table goes in the document BODY -->
<table class="hovertable">
<tr>
<th>Info Header 1</th><th>Info Header 2</th><th>Info Header 3</th>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';"> <td>Item 1A</td><td>Item 1B</td><td>Item 1C</td> </tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 2A</td><td>Item 2B</td><td>Item 2C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 3A</td><td>Item 3B</td><td>Item 3C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 4A</td><td>Item 4B</td><td>Item 4C</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td>Item 5A</td><td>Item 5B</td><td>Item 5C</td>
</tr>
</table>
最常见的几种CSS样式表格都在这了,希望对大家有帮助
相关文章推荐
- CSS 背景
- CSS padding 属性
- CSS border 属性和 border-collapse 属性
- 一个自定义的窗体样式MessageBox控件
- CSS中cursor属性汇总
- 【CSS3】---曲线阴影翘边阴影
- 【CSS3】---认识CSS3
- 【CSS3】---阴影 box-shadow
- 【CSS3】---结构性伪类选择器—nth-child(n)+nth-last-child(n)
- 【CSS3】---CSS after实现分隔线
- 【CSS3】---文本阴影text-shadow
- 编写灵活、稳定、高质量的 HTML 和 CSS 代码的规范
- 【CSS3】---属性选择器
- 【CSS3】---last-of-type选择器+nth-last-of-type(n)选择器
- 【CSS3】---text-overflow 与 word-wrap
- 【CSS3】---嵌入字体@font-face
- 【CSS3】---only-child选择器+only-of-type选择器
- 【CSS3】---为边框应用图片 border-image
- 【CSS3】---层模型position之fixed固定定位、absolute绝对定位和relative相对定位
- 【CSS3】---练习制作导航菜单