您现在的位置是:网站首页> 编程资料编程资料

Div+CSS对HTML的table表格定位用法实例浅析Table 和 div 的简介及用法

2021-09-05 825人已围观

简介 这篇文章主要介绍了Div+CSS对HTML的table表格定位用法实例,文中讲到了CSS的定位差异问题需要的朋友可以参考下

关于css定位有很多文章讲述:

如果有一个元素div ,他的position属性设置为absolute,那么这个div 的位置取决于其父元素中position值设置为relative的元素。如果在其父元素中没有一个元素的position值是relative、absolute、或者fixed,那么这个div位置将以 body位置为参考。

但是对于某些浏览器来说似乎不是每个元素都遵从这一规则,有如下一段代码

CSS Code复制内容到剪贴板
  1. table{     
  2.     margin-left100px;     
  3.     margin-top50px;     
  4. }     
  5. td{     
  6.     height150px;     
  7.     width100px;     
  8.     positionrelative;     
  9. }     
  10. td div{     
  11.     height30px;     
  12.     width50px;     
  13.     background-colorred;     
  14.     positionabsolute;     
  15.     left10px;     
  16.     top:50px;     
  17. }     
  18. border="1">     
  19.          
  20.              
  21.              
  22.              
  23.             
    这是一个positionabsolute元素
         
  24.              
  25.          
  26.     

由于div元素的尺寸较小,理想的情况是div总是在最后一个td中,但是在firefox中div并不以td为参考,而是body。
所以要实现这个效果的兼容方式是在td中添加一个能应用position:relative的元素,上述代码可以更改为

XML/HTML Code复制内容到剪贴板
  1. <table border="1">     
  2.     <tr>     
  3.         <td>td>     
  4.         <td>td>     
  5.         <td>     
  6.                       <div style="position:relative;"     
  7.             <div>这是一个position:absolute元素div>     
  8.                       div>     
  9.         td>     
  10.     tr>     
  11. table>    

这样就可以保证 div元素始终在td中。


table的td相对定位实例
下面我们来看一个处理td相对定位的实例,这里我们建两个table样式:table和table2

CSS Code复制内容到剪贴板
  1. .table,.table2   
  2. {   
  3. overflow:hidden;   
  4.   
  5. }   
  6. .table > .header   
  7. {   
  8. position:relative;   
  9. height:40px;   
  10. background-color:#84a9cc;   
  11. }   
  12. .header > .header-title   
  13. {   
  14. margin:0 auto;line-height:40px;color:#fff;width:80px;text-aligncenter;font-size:14px;   
  15. }   
  16. .header > .header-add   
  17. {   
  18. background-color#488FD2;   
  19. color#FFFFFF;   
  20. cursorpointer;   
  21. height20px;   
  22. line-height20px;   
  23. padding10px;   
  24. positionabsolute;   
  25. rightright: 0;   
  26. text-aligncenter;   
  27. top: 0;   
  28. width45px;   
  29. }   
  30. .header > .header-search   
  31. {   
  32. background-color#fff;   
  33. height30px;   
  34. line-height20px;   
  35. positionabsolute;   
  36. rightright80px;   
  37. text-aligncenter;   
  38. top5px;   
  39. width200px;   
  40. }   
  41. .table > .body,.table2 > .body   
  42. {   
  43. border1px solid #BCC6D0;/border-style:none solid solid solid;/background-color:#fff;   
  44. }   
  45. .header-search > input   
  46. {   
  47. border:none;   
  48. border-right1px solid #BCC6D0;   
  49. color#666666;   
  50. font-size14px;   
  51. height: 100%;   
  52. line-height: 100%;   
  53. width170px;   
  54. float:left;   
  55. }   
  56. .header-search > .search-logo   
  57. {   
  58. float:left;width:29px;height:30px;   
  59. background:url(…/…/images/global/serachBlue.png) center no-repeat;   
  60. }   
  61. tbody > .tr   
  62. {   
  63. height:20px;line-height:20px;   
  64. }   
  65. th   
  66. {   
  67. display:inline-block;margin-right:-3px;   
  68. }   
  69. th + th   
  70. {   
  71. margin-left:-3px;   
  72. } &#

相关内容

-六神源码网