6

Is this possible to create a tree component using only HTML and CSS that contains lines connecting nodes? I'm trying to avoid nested nodes containing text with CSS styles

See the mockup as an example. It heavily inspired by Autodesk Maya inspector tree

I achieved the following but I don't like it because the lines are not connected:

https://codepen.io/xotonic/pen/JRLAOR

HTML:

<ul>
<li class="container"><p><r>Testing</r> </p>
    <ul>
        <li><p><r>Testing 1</r></p></li>
        <li><p><r>Testing 2</r></p></li>
        <li class="container"><p><r>Testing</r> </p>
            <ul>
                <li><p><r>Testing 1</r></p></li>
                <li><p><r>Testing 2</r></p></li>
                <li><p><r>Testing 3</r></p></li>
            </ul>
        </li>
    </ul>
</li>
<li class="container"><p><r>Testing</r> </p>
    <ul>
        <li><p><r>Testing 1</r></p></li>
        <li><p><r>Testing 2</r></p></li>
        <li><p><r>Testing 3</r></p></li>
    </ul>
</li>
<li class="container"><p><r>Testing </r></p>
    <ul>
        <li class="empty"><p><r>empty</r></p></li>
    </ul>
</li>

CSS:

ul, li { list-style: none; margin: 0; padding: 0; }
ul { padding-left: 1em; }
li { padding-left: 1em;
  border: 1px dotted black;
  border-width: 0 0 1px 1px; 
}
li.container { border-bottom: 0px; 
}
li.empty { font-style: italic;
  color: silver;
  border-color: silver;
}
p { margin: 0;
  background: white;
  position: relative;
  top: 0.5em; 
  
}
p:before {
  content: '';
  position: absolute;
  width: 1em;
  border: 1px solid black;
  height: 1em;
  border-radius: 1em;
  margin-left: 0.5em;
}
r {
  margin-left: 2em;
}
ul { 
  border-top: 1px dotted black; 
  margin-left: -1em;     
  padding-left: 2em; 
}
ul li:last-child ul {
  border-left: 1px solid white;
  margin-left: -17px;
}
2
  • 1
    Yes. Have you tried something to achieve this? Commented Oct 9, 2016 at 19:21
  • @Xufox I try to add :before pseudo-element to draw circle in content div, but lines drawing to the div instead circle.
    – Enbugger
    Commented Oct 9, 2016 at 19:25

3 Answers 3

8

Dylancwood provides CSS for a nested structure as in the question's example (ul in ul instead of div in div as in the answer).

See https://gist.github.com/dylancwood/7368914

To stay with nesting is semantically correct (and still looks correct without any CSS at all)! (Manuel Matuzović wrote a really good article on ol vs. ul .vs div.)

Anyways, Dylancwood uses light-weight border-left in combination with :before for drawing the vertical and horizontal connecting lines. A working fiddle is linked in his gist.

6

I made my own implementation. There are more additional elements.

https://github.com/xotonic/flex-tree

1
  • 1
    what a pity, you are using divs instead of uls in your github code, though a nested structure would be semantically correct (as in your question's code).
    – jasie
    Commented Jan 21, 2020 at 9:50
1

Yes this solution seems right: Draw a Tree Structure With Only CSS

  • Defines :before and :after for each list item
  • Use position absolute and negative left to position them both to the left of the list item
  • Use one to draw the vertical line and the other to draw the horizontal line
    • Lines are defined as a border-left or border-top with a top value to move the line down
    • The height and top of the vertical line can also be customized, depending on whether a list item is a last sibling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.