79706521

Date: 2025-07-18 17:14:49
Score: 0.5
Natty:
Report link

Yes, you can absolutely create a compelling hierarchy tree from a nested <ul> structure using pure CSS! It's a common and fun challenge. Your current approach is already very close; the key is to fine-tune the positioning and dimensions of the pseudo-elements (::before and ::after) to draw those connecting lines accurately.

Here's a refined CSS solution that should give you the desired command-line tree look, along with an explanation of the adjustments.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Hierarchy Tree</title>
    <style>
        body {
            font-family: monospace; /* For that terminal-like feel */
            color: #333;
            padding: 20px;
        }

        ul {
            list-style: none;
            margin: 0;
            padding-left: 20px; /* Adjust as needed for initial indent */
            position: relative;
        }

        li {
            position: relative;
            padding-left: 20px; /* Space for the horizontal line and text */
            margin-bottom: 5px; /* Small space between items */
            line-height: 1.5; /* Vertical spacing for text */
        }

        /* Horizontal line for each item */
        li::before {
            content: '';
            position: absolute;
            top: 0.75em; /* Adjust to align with the text baseline */
            left: -5px; /* Pull the line slightly to the left to connect with parent's vertical line */
            width: 15px; /* Length of the horizontal line */
            height: 0;
            border-top: 1px solid #999;
        }

        /* Vertical line for connecting child branches */
        li::after {
            content: '';
            position: absolute;
            top: 0.75em; /* Start from the same height as the horizontal line */
            left: -5px; /* Align with the horizontal line start */
            height: calc(100% + 5px); /* Extend beyond the current li to connect to siblings/next parent */
            border-left: 1px solid #999;
        }

        /* Hide vertical line for the last child in a branch */
        li:last-child::after {
            height: 0.75em; /* Only extend to the horizontal line of the current li */
        }

        /* Remove the initial horizontal line for the very first item (if desired) */
        ul:first-child > li:first-child::before {
            border-top: none;
        }
        /* Remove the initial vertical line for the very first item (if desired) */
        ul:first-child > li:first-child::after {
            border-left: none;
        }


        /* Style for the nested ULs to create proper alignment */
        li > ul {
            padding-left: 20px; /* Indent for nested lists */
            margin-top: 5px; /* Adjust spacing between parent and child UL */
        }

        /* Special handling for the vertical line coming *down* from a parent */
        li:not(:last-child) > ul::before {
            content: '';
            position: absolute;
            top: -5px; /* Align with the bottom of the parent's vertical line */
            left: 0;
            height: 10px; /* Length of the connecting vertical line */
            border-left: 1px solid #999;
        }
    </style>
</head>
<body>

    <ul>
        <li>1830 Palmyra
            <ul>
                <li>1837 Kirtland
                    <ul>
                        <li>1840 Nauvoo
                            <ul>
                                <li>1841 Liverpool
                                    <ul>
                                        <li>1849 Liverpool
                                            <ul>
                                                <li>1854 Liverpool
                                                    <ul>
                                                        <li>1871 SaltLakeCity
                                                            <ul>
                                                                <li>1877 SaltLakeCity</li>
                                                            </ul>
                                                        </li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                                <li>1842 Nauvoo
                                    <ul>
                                        <li>1858 NewYork
                                            <ul>
                                                <li>1874 Iowa
                                                    <ul>
                                                        <li>2013 SaltLakeCity</li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>

</body>
</html>

What's Changed and Why?

Let's break down the key adjustments and the thought process behind them:

Tips for Customization

With these adjustments, you should achieve a much cleaner and more accurate visual representation of your timeline/tree structure using only HTML and CSS. Give it a try! You'll love the results.

Reasons:
  • Blacklisted phrase (0.5): Why?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Amritanshu