2016-07-04 5 views
2

하위 요소가 상위 요소를 따라갈 수있는 방법이 있습니까?패밀리 트리 CSS

문제는 하나의 자식이 있고 부모에게 큰 텍스트가있는 경우 커넥터의 위치가 실제로 나빠진다는 것입니다. 어떻게 해결할 수 있습니까? 당신의 .tree li 규칙 float:left에서 display: inline-block

Fiddle demo

* {margin: 0; padding: 0;} 
 

 
.tree ul { 
 
\t padding-top: 20px; position: relative; \t 
 
\t transition: all 0.5s; 
 
\t -webkit-transition: all 0.5s; 
 
\t -moz-transition: all 0.5s; 
 
} 
 

 
.tree li { 
 
\t float: left; text-align: center; 
 
\t list-style-type: none; 
 
\t position: relative; 
 
\t padding: 20px 5px 0 5px; 
 
\t transition: all 0.5s; 
 
\t -webkit-transition: all 0.5s; 
 
\t -moz-transition: all 0.5s; 
 
} 
 

 
/*We will use ::before and ::after to draw the connectors*/ 
 
.tree li::before, .tree li::after{ 
 
\t content: ''; 
 
\t position: absolute; top: 0; right: 50%; 
 
\t border-top: 1px solid #ccc; 
 
\t width: 50%; height: 20px; 
 
} 
 
.tree li::after{ 
 
\t right: auto; left: 50%; 
 
\t 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 { 
 
\t 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{ 
 
\t border: 0 none; 
 
} 
 
/*Adding back the vertical connector to the last nodes*/ 
 
.tree li:last-child::before{ 
 
\t border-right: 1px solid #ccc; 
 
\t border-radius: 0 5px 0 0; 
 
\t -webkit-border-radius: 0 5px 0 0; 
 
\t -moz-border-radius: 0 5px 0 0; 
 
} 
 
.tree li:first-child::after{ 
 
\t border-radius: 5px 0 0 0; 
 
\t -webkit-border-radius: 5px 0 0 0; 
 
\t -moz-border-radius: 5px 0 0 0; 
 
} 
 

 
/*Time to add downward connectors from parents*/ 
 
.tree ul ul::before{ 
 
\t content: ''; 
 
\t position: absolute; top: 0; left: 50%; 
 
\t border-left: 1px solid #ccc; 
 
\t width: 0; height: 20px; 
 
} 
 

 
.tree li a{ 
 
\t border: 1px solid #ccc; 
 
\t padding: 5px 10px; 
 
\t text-decoration: none; 
 
\t color: #666; 
 
\t font-family: arial, verdana, tahoma; 
 
\t font-size: 11px; 
 
\t display: inline-block; 
 
\t border-radius: 5px; 
 
\t -webkit-border-radius: 5px; 
 
\t -moz-border-radius: 5px; \t 
 
\t transition: all 0.5s; 
 
\t -webkit-transition: all 0.5s; 
 
\t -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 { 
 
\t 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{ 
 
\t border-color: #94a0b4; 
 
}
<!-- 
 
We will create a family tree using just CSS(3) 
 
The markup will be simple nested lists 
 
--> 
 
<div class="tree"> 
 
\t <ul> 
 
\t \t <li> 
 
\t \t \t <a href="#">Parent sdkfjnsdkfsdnfksdfnskfns</a> 
 
\t \t \t <ul> 
 
\t \t \t \t <li> 
 
\t \t \t \t \t <a href="#">Child</a> 
 
\t \t \t \t </li> 
 
\t \t \t </ul> 
 
\t \t </li> 
 
\t </ul> 
 
</div>

+0

번호. CSS는 '내리막 길'에서만 흐릅니다. 부모는 자녀로부터 규칙을 읽을 수 없습니다. 아이들은 규칙을 오르막으로 밀 수 없습니다. –

+0

찾으시는 분 이것은 : https://jsfiddle.net/yau5juka/1/ – LGSon

+0

사례가 있다면 문제를 해결하는 방법에 대한 아이디어가 있습니까? 아니면 다른 접근 방법에 대한 조언을 해 주시겠습니까? Nevermind – Curunir

답변

2

변경

/*Now the CSS*/ 
 
* {margin: 0; padding: 0;} 
 

 
.tree ul { 
 
\t padding-top: 20px; position: relative; 
 
\t transition: all 0.5s; 
 
\t -webkit-transition: all 0.5s; 
 
\t -moz-transition: all 0.5s; 
 
} 
 

 
.tree li { 
 
    display: inline-block; 
 
\t text-align: center; 
 
\t list-style-type: none; 
 
\t position: relative; 
 
\t padding: 20px 5px 0 5px; \t 
 
\t transition: all 0.5s; 
 
\t -webkit-transition: all 0.5s; 
 
\t -moz-transition: all 0.5s; 
 
} 
 

 
/*We will use ::before and ::after to draw the connectors*/ 
 

 
.tree li::before, .tree li::after{ 
 
\t content: ''; 
 
\t position: absolute; top: 0; right: 50%; 
 
\t border-top: 1px solid #ccc; 
 
\t width: 50%; height: 20px; 
 
} 
 
.tree li::after{ 
 
\t right: auto; left: 50%; 
 
\t 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 { 
 
\t 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{ 
 
\t border: 0 none; 
 
} 
 
/*Adding back the vertical connector to the last nodes*/ 
 
.tree li:last-child::before{ 
 
\t border-right: 1px solid #ccc; 
 
\t border-radius: 0 5px 0 0; 
 
\t -webkit-border-radius: 0 5px 0 0; 
 
\t -moz-border-radius: 0 5px 0 0; 
 
} 
 
.tree li:first-child::after{ 
 
\t border-radius: 5px 0 0 0; 
 
\t -webkit-border-radius: 5px 0 0 0; 
 
\t -moz-border-radius: 5px 0 0 0; 
 
} 
 

 
/*Time to add downward connectors from parents*/ 
 
.tree ul ul::before{ 
 
\t content: ''; 
 
\t position: absolute; top: 0; left: 50%; 
 
\t border-left: 1px solid #ccc; 
 
\t width: 0; height: 20px; 
 
} 
 

 
.tree li a{ 
 
\t border: 1px solid #ccc; 
 
\t padding: 5px 10px; 
 
\t text-decoration: none; 
 
\t color: #666; 
 
\t font-family: arial, verdana, tahoma; 
 
\t font-size: 11px; 
 
\t display: inline-block; 
 
\t 
 
\t border-radius: 5px; 
 
\t -webkit-border-radius: 5px; 
 
\t -moz-border-radius: 5px; 
 
\t 
 
\t transition: all 0.5s; 
 
\t -webkit-transition: all 0.5s; 
 
\t -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 { 
 
\t 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{ 
 
\t border-color: #94a0b4; 
 
} 
 

 
/*Thats all. I hope you enjoyed it. 
 
Thanks :)*/
<!-- 
 
We will create a family tree using just CSS(3) 
 
The markup will be simple nested lists 
 
--> 
 
<div class="tree"> 
 
\t <ul> 
 
\t \t <li> 
 
\t \t \t <a href="#">Parent sdkfjnsdkfsdnfksdfnskfns</a> 
 
\t \t \t <ul> 
 
\t \t \t \t <li> 
 
\t \t \t \t \t <a href="#">Child</a> 
 
\t \t \t \t </li> 
 
\t \t \t </ul> 
 
\t \t </li> 
 
\t </ul> 
 
</div>

+1

에서 온 것 같습니다. 모든 것이 왼쪽으로 떠있었습니다 ... 절대 위치 지정에 문제가 있다는 것을 지적하려고했습니다. 좋은 캐치! – Scott