调用DIV+CSS实现模拟表格对角线

开发 前端
这里向大家描述一下用DIV+CSS模拟表格对角线,有时在插入文档时,要用到表格对角线,常见的作法是用图片的方式来处理,还有就是用vml来画对角线,这里就和大家分享一下新方法。

本文和大家重点讨论一下如何使用DIV+CSS模拟表格对角线,用边框线来摸拟斜线,我们知道,如果将一个DIV的边框线设置得足够宽并定义了不同的颜色时,其相邻的两条边框线交界处就是斜线。

用DIV+CSS模拟表格对角线

有时在插入文档时,要用到表格对角线,常见的作法是用图片的方式来处理,还有就是用vml来画对角线,能不能用html+css方式来实现呢?答案是肯定的,下面我们来摸拟一个表格对角线。

原理:

用边框线来摸拟斜线,我们知道,如果将一个DIV的边框线设置得足够宽并定义了不同的颜色时,其相邻的两条边框线交界处就是斜线。知道了这个原理,我们就可以用border-left和border-top来模拟出斜线的效果。

我们先创建一个结构:

  1. <divclassdivclass="out"> 
  2. <b>类别</b> 
  3. <em>姓名</em> 
  4. </div> 
  5.  

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

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

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

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

这样一个斜线对角线就模拟出来了。知道了原理,你可以变成很多有趣的东西出来,祝你好运!

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

◆宽高度必须是已知的

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

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

另:以上代码只测试了IE6和ff3,其它浏览器未做测试,请朋友们测试一下。#p#

完整的代码:

  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"  
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> 
  6. <title>用div+css模拟表格对角线</title> 
  7. <styletypestyletype="text/css"> 
  8. *{padding:0;margin:0;}  
  9. caption{font-size:14px;font-weight:bold;}  
  10. table{border-collapse:collapse;border:1px#525152solid;  
  11. width:50%;margin:0auto;margin-top:100px;}  
  12. th,td{border:1px#525152solid;text-align:center;  
  13. font-size:12px;line-height:30px;background:#C6C7C6;}  
  14. th{background:#D6D3D6;}  
  15. /*模拟对角线*/  
  16. .out{  
  17. border-top:40px#D6D3D6solid;/*上边框宽度等于表格***行行高*/  
  18. width:0px;/*让容器宽度为0*/  
  19. height:0px;/*让容器高度为0*/  
  20. border-left:80px#BDBABDsolid;/*左边框宽度等于表格***行***格宽度*/  
  21. position:relative;/*让里面的两个子容器绝对定位*/  
  22. }  
  23. b{font-style:normal;display:block;position:absolute;  
  24. top:-40px;left:-40px;width:35px;}  
  25. em{font-style:normal;display:block;position:absolute;  
  26. top:-25px;left:-70px;width:55x;}  
  27. .t1{background:#BDBABD;}  
  28. </style> 
  29. </head> 
  30. <body> 
  31. <table> 
  32. <caption>用div+css模拟表格对角线</caption> 
  33. <tr> 
  34. <thstylethstyle="width:80px;"> 
  35. <divclassdivclass="out"> 
  36. <b>类别</b> 
  37. <em>姓名</em> 
  38. </div> 
  39. </th> 
  40. <th>年级</th> 
  41. <th>班级</th> 
  42. <th>成绩</th> 
  43. <th>班级均分</th> 
  44. </tr> 
  45. <tr> 
  46. <tdclasstdclass="t1">张三</td> 
  47. <td></td> 
  48. <td>2</td> 
  49. <td>62</td> 
  50. <td>61</td> 
  51. </tr> 
  52. <tr> 
  53. <tdclasstdclass="t1">李四</td> 
  54. <td></td> 
  55. <td>1</td> 
  56. <td>48</td> 
  57. <td>67</td> 
  58. </tr> 
  59. <tr> 
  60. <tdclasstdclass="t1">王五</td> 
  61. <td></td> 
  62. <td>5</td> 
  63. <td>79</td> 
  64. <td>63</td> 
  65. </tr> 
  66. <tr> 
  67. <tdclasstdclass="t1">赵六</td> 
  68. <td></td> 
  69. <td>4</td> 
  70. <td>89</td> 
  71. <td>66</td> 
  72. </tr> 
  73. </table> 
  74. </body> 
  75. </html> 
  76.  

【编辑推荐】

  1. DIV滚动条属性及样式设置方式
  2. JavaScript动态创建div属性和样式
  3. SPAN元素和DIV元素的区别
  4. CSS2.0中page-break-after属性用法
  5. 探究CSS中border:none;与border:0;的区别

 

 

责任编辑:佚名 来源: 蓝色理想
相关推荐

2010-09-14 14:23:08

DIV+CSS

2010-09-14 12:58:41

DIV+CSS圆角

2010-08-16 15:32:06

DIV+CSS

2010-08-25 09:11:57

DIVCSS

2010-08-24 10:32:34

DIV+CSS

2010-08-27 14:05:40

DIV+CSS

2010-08-30 10:46:13

DIV+CSS

2010-09-14 11:19:23

DIV+CSS技术

2010-08-30 13:09:40

DIVCSS

2010-08-25 12:47:40

DIVCSS

2010-08-16 15:19:35

DIV+CSS教程

2012-08-06 09:40:36

DIV

2011-05-17 09:51:27

Div+CSS

2010-08-24 11:25:06

DIVCSS

2010-08-16 13:17:47

DIV+CSS

2010-08-30 11:08:53

DIV+CSS

2010-08-23 14:30:14

DIV+CSS

2011-05-26 18:05:01

DIV+CSS

2010-08-16 16:17:21

2011-07-05 16:26:48

DIV+CSS
点赞
收藏

51CTO技术栈公众号