CSS中怎么实现表格斜线效果

这期内容当中小编将会给大家带来有关CSS中怎么实现表格斜线效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

首先创建一个结构:

示例代码

<divclassdivclass="out"> <b>类别</b> <em>姓名</em> </div>

我们用<divclass="out">作为对角线的容器,我们来设置斜线样式,关键代码如下:

示例代码

.out  {  border-top:40px#D6D3D6solid;/*上边框宽度等于表格***行行高*/  width:0px;/*让容器宽度为0*/  height:0px;/*让容器高度为0*/  border-left:80px#BDBABDsolid;/*左边框宽度等于表格***行***格宽度*/  position:relative;/*让里面的两个子容器绝对定位*/  }

<b>和<em>两个标签来设置两个分类,分别将它们设置为块状结构display:block;清除其默认的字体样式font-style:normal;因其父容器设置了相对定位,所以设置其为绝对定位,这样可以将它偏移到你想指定的位置了。

示例代码

b{font-style:normal;display:block;  position:absolute;top:-40px;  left:-40px;width:35px;}  em{font-style:normal;display:block;  position:absolute;top:-25px;left:-70px;width:55x;}

这样一个斜线对角线就模拟出来了。

◆这种对角线模拟法也有缺点:

1.宽高度必须是已知的

2.宽高的长度不能差得太大,你可以试试将宽度拉得比高度长好几倍,看看效果。

3.还有就是斜线条不能设置颜色。

完整的代码如下:

示例代码

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <title>用CSS实现网页表格斜线-CSS在线</title> <styletypestyletype="text/css"> *{padding:0;margin:0;}  caption{font-size:14px;font-weight:bold;}  table{border-collapse:collapse;border:1px#525152solid;  width:50%;margin:0auto;margin-top:100px;}  th,td{border:1px#525152solid;text-align:center;  font-size:12px;line-height:30px;background:#C6C7C6;}  th{background:#D6D3D6;}  /*表格斜线*/  .out{  border-top:40px#D6D3D6solid;/*上边框宽度等于表格***行行高*/  width:0px;/*让容器宽度为0*/  height:0px;/*让容器高度为0*/  border-left:80px#BDBABDsolid;/*左边框宽度等于表格***行***格宽度*/  position:relative;/*让里面的两个子容器绝对定位*/  }  b{font-style:normal;display:block;position:absolute;  top:-40px;left:-40px;width:35px;}  em{font-style:normal;display:block;position:absolute;  top:-25px;left:-70px;width:55x;}  .t1{background:#BDBABD;}  </style> </head> <body> <table> <caption>用CSS实现网页表格斜线-  <AhrefAhref='http://www.csscss.org'>CSS在线</A></caption> <tr> <thstylethstyle="width:80px;"> <divclassdivclass="out"> <b>类别</b> <em>姓名</em> </div> </th> <th>年级</th> <th>班级</th> <th>成绩</th> <th>班级均分</th> </tr> <tr> <tdclasstdclass="t1">张三</td> <td>三</td> <td>2</td> <td>62</td> <td>61</td> </tr> <tr> <tdclasstdclass="t1">李四</td> <td>三</td> <td>1</td> <td>48</td> <td>67</td> </tr> <tr> <tdclasstdclass="t1">王五</td> <td>三</td> <td>5</td> <td>79</td> <td>63</td> </tr> <tr> <tdclasstdclass="t1">赵六</td> <td>三</td> <td>4</td> <td>89</td> <td>66</td> </tr> </table> </body> </html>

上述就是小编为大家分享的CSS中怎么实现表格斜线效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。