Family tree(族譜),也稱家譜,用來(lái)記錄家族世系繁衍輩份關(guān)系。本文結(jié)合實(shí)例,不借助任何js腳本,使用純CSS打造一個(gè)漂亮的Family tree(族譜),也可以應(yīng)用的企業(yè)組織架構(gòu)圖中。
HTML
我們使用div#tree來(lái)包含整個(gè)族譜結(jié)構(gòu),內(nèi)部以u(píng)l和li元素構(gòu)建數(shù)據(jù)源。實(shí)際開發(fā)中這些族譜數(shù)據(jù)源可以從數(shù)據(jù)庫(kù)中讀取,就像得到一個(gè) 無(wú)限級(jí)的分類列表 ,以下是主要的html結(jié)構(gòu)。
<div class="tree"> <ul> <li> <a href="#">Parent</a> <ul> <li> <a href="#">Child</a> <ul> <li><a href="#">Grand Child</a></li> </ul> </li> <li> <a href="#">Child</a> <ul> <li><a href="#">Grand Child</a></li> <li> <a href="#">Grand Child</a> <ul> <li><a href="#">Great Grand Child</a></li> <li><a href="#">Great Grand Child</a></li> <li><a href="#">Great Grand Child</a></li> </ul> </li> <li><a href="#">Grand Child</a></li> </ul> </li> </ul> </li> </ul> </div>
?
CSS
我們使用css中的 :before、:after 兩個(gè)偽類的content屬性來(lái)構(gòu)建元素間的連接線,同時(shí)使用了css3實(shí)現(xiàn)元素的圓角效果,并加上了鼠標(biāo)滑上hover效果,這樣鼠標(biāo)滑向族譜中的一個(gè) 節(jié)點(diǎn)元素,與之相關(guān)的后輩元素都會(huì)有hover效果,完整的css代碼如下:
.tree ul { padding-top: 20px; position: relative; transition: all 0.5s; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; } .tree li { float: left; text-align: center; list-style-type: none; position: relative; padding: 20px 5px 0 5px; transition: all 0.5s; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; } /*We will use ::before and ::after to draw the connectors*/ .tree li::before, .tree li::after{ content: ''; position: absolute; top: 0; right: 50%; border-top: 1px solid #ccc; width: 50%; height: 20px; } .tree li::after{ right: auto; left: 50%; border-left: 1px solid #ccc; } /*We need to remove left-right connectors from elements without any siblings*/ .tree li:only-child::after, .tree li:only-child::before { display: none; } /*Remove space from the top of single children*/ .tree li:only-child{ padding-top: 0;} /*Remove left connector from first child and right connector from last child*/ .tree li:first-child::before, .tree li:last-child::after{ border: 0 none; } /*Adding back the vertical connector to the last nodes*/ .tree li:last-child::before{ border-right: 1px solid #ccc; border-radius: 0 5px 0 0; -webkit-border-radius: 0 5px 0 0; -moz-border-radius: 0 5px 0 0; } .tree li:first-child::after{ border-radius: 5px 0 0 0; -webkit-border-radius: 5px 0 0 0; -moz-border-radius: 5px 0 0 0; } /*Time to add downward connectors from parents*/ .tree ul ul::before{ content: ''; position: absolute; top: 0; left: 50%; border-left: 1px solid #ccc; width: 0; height: 20px; } .tree li a{ border: 1px solid #ccc; padding: 5px 10px; text-decoration: none; color: #666; font-family: arial, verdana, tahoma; font-size: 11px; display: inline-block; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; transition: all 0.5s; -webkit-transition: all 0.5s; -moz-transition: all 0.5s; } /*Time for some hover effects*/ /*We will apply the hover effect the the lineage of the element also*/ .tree li a:hover, .tree li a:hover+ul li a { background: #c8e4f8; color: #000; border: 1px solid #94a0b4; } /*Connector styles on hover*/ .tree li a:hover+ul li::after, .tree li a:hover+ul li::before, .tree li a:hover+ul::before, .tree li a:hover+ul ul::before{ border-color: #94a0b4; }
?
要過(guò)大年了,祝愿關(guān)注Helloweba的朋友新的一年里身體健康,工作順利!
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
