纯CSS打造的Family tree(族谱)

jerry CSS 2015年08月20日 收藏

Family tree(族谱),也称家谱,用来记录家族世系繁衍辈份关系。本文结合实例,不借助任何js脚本,使用纯CSS打造一个漂亮的Family tree(族谱),也可以应用的企业组织架构图中。

HTML

我们使用div#tree来包含整个族谱结构,内部以ul和li元素构建数据源。实际开发中这些族谱数据源可以从数据库中读取,就像得到一个无限级的分类列表,以下是主要的html结构。

  1. <div class="tree">
  2. <ul>
  3. <li>
  4. <a href="#">Parent</a>
  5. <ul>
  6. <li>
  7. <a href="#">Child</a>
  8. <ul>
  9. <li><a href="#">Grand Child</a></li>
  10. </ul>
  11. </li>
  12. <li>
  13. <a href="#">Child</a>
  14. <ul>
  15. <li><a href="#">Grand Child</a></li>
  16. <li>
  17. <a href="#">Grand Child</a>
  18. <ul>
  19. <li><a href="#">Great Grand Child</a></li>
  20. <li><a href="#">Great Grand Child</a></li>
  21. <li><a href="#">Great Grand Child</a></li>
  22. </ul>
  23. </li>
  24. <li><a href="#">Grand Child</a></li>
  25. </ul>
  26. </li>
  27. </ul>
  28. </li>
  29. </ul>
  30. </div>

CSS

我们使用css中的 :before、:after 两个伪类的content属性来构建元素间的连接线,同时使用了css3实现元素的圆角效果,并加上了鼠标滑上hover效果,这样鼠标滑向族谱中的一个节点元素,与之相关的后辈元素都会有hover效果,完整的css代码如下:

  1. .tree ul {
  2. padding-top: 20px; position: relative;
  3. transition: all 0.5s;
  4. -webkit-transition: all 0.5s;
  5. -moz-transition: all 0.5s;
  6. }
  7. .tree li {
  8. float: left; text-align: center;
  9. list-style-type: none;
  10. position: relative;
  11. padding: 20px 5px 0 5px;
  12. transition: all 0.5s;
  13. -webkit-transition: all 0.5s;
  14. -moz-transition: all 0.5s;
  15. }
  16. /*We will use ::before and ::after to draw the connectors*/
  17. .tree li::before, .tree li::after{
  18. content: '';
  19. position: absolute; top: 0; right: 50%;
  20. border-top: 1px solid #ccc;
  21. width: 50%; height: 20px;
  22. }
  23. .tree li::after{
  24. right: auto; left: 50%;
  25. border-left: 1px solid #ccc;
  26. }
  27. /*We need to remove left-right connectors from elements without
  28. any siblings*/
  29. .tree li:only-child::after, .tree li:only-child::before {
  30. display: none;
  31. }
  32. /*Remove space from the top of single children*/
  33. .tree li:only-child{ padding-top: 0;}
  34. /*Remove left connector from first child and
  35. right connector from last child*/
  36. .tree li:first-child::before, .tree li:last-child::after{
  37. border: 0 none;
  38. }
  39. /*Adding back the vertical connector to the last nodes*/
  40. .tree li:last-child::before{
  41. border-right: 1px solid #ccc;
  42. border-radius: 0 5px 0 0;
  43. -webkit-border-radius: 0 5px 0 0;
  44. -moz-border-radius: 0 5px 0 0;
  45. }
  46. .tree li:first-child::after{
  47. border-radius: 5px 0 0 0;
  48. -webkit-border-radius: 5px 0 0 0;
  49. -moz-border-radius: 5px 0 0 0;
  50. }
  51. /*Time to add downward connectors from parents*/
  52. .tree ul ul::before{
  53. content: '';
  54. position: absolute; top: 0; left: 50%;
  55. border-left: 1px solid #ccc;
  56. width: 0; height: 20px;
  57. }
  58. .tree li a{
  59. border: 1px solid #ccc;
  60. padding: 5px 10px;
  61. text-decoration: none;
  62. color: #666;
  63. font-family: arial, verdana, tahoma;
  64. font-size: 11px;
  65. display: inline-block;
  66. border-radius: 5px;
  67. -webkit-border-radius: 5px;
  68. -moz-border-radius: 5px;
  69. transition: all 0.5s;
  70. -webkit-transition: all 0.5s;
  71. -moz-transition: all 0.5s;
  72. }
  73. /*Time for some hover effects*/
  74. /*We will apply the hover effect the the lineage of the element also*/
  75. .tree li a:hover, .tree li a:hover+ul li a {
  76. background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
  77. }
  78. /*Connector styles on hover*/
  79. .tree li a:hover+ul li::after,
  80. .tree li a:hover+ul li::before,
  81. .tree li a:hover+ul::before,
  82. .tree li a:hover+ul ul::before{
  83. border-color: #94a0b4;
  84. }

要过大年了,祝愿关注Helloweba的朋友新的一年里身体健康,工作顺利!

下载地址