Nested lists with full-width border and indentation
Asked Answered
F

3

6

I'm trying to achieve the following result:

Styled nested list

So far, I've written the following:

a {
    text-decoration: none;
}
ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
}
li {
    border-top: 1px solid;
    border-bottom: 1px solid;
    padding-left: 1em;
    line-height: 2em;
}
li li {
    margin-left: -1em;
    padding-left: 2em;
    border-bottom: none;
}
li li li {
    margin-left: -2em;
    padding-left: 3em;
    border-bottom: none;
}

Demo: https://jsfiddle.net/9h891a0s/1/

However, I am looking for a solution that would allow for infinite depth. Is there a clean solution for this?

Felice answered 24/4, 2015 at 16:29 Comment(0)
Y
5

Take a look of this by using the position:absolute tricks for the borders.

body {
    margin: 0;
    padding: 1em;
}
body > ul {
    border-bottom: 1px solid black;
    overflow: hidden;
}
a {
    text-decoration: none;
}
ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
}
li {
    line-height: 2em;
    position: relative;
}
li ul {
    padding-left: 1em;
}
li:after {
    content:"\00A0";
    position: absolute;
    width: 200%;
    left: -100%;
    top: 0;
    border-top: 1px solid black;
    z-index: -1;
}
<ul>
    <li><a href="#">Level 1</a>
        <ul>
            <li><a href="#">Level 2</a></li>
            <li>
                <a href="#">Level 2</a>
                <ul>
                    <li><a href="#">Level 3</a></li>
                    <li><a href="#">Level 3</a></li>
                    <li><a href="#">Level 3</a></li>
                </ul>
            </li>
            <li><a href="#">Level 2</a></li>
        </ul>
    </li>
</ul>

Fiddle Demo: http://jsfiddle.net/Lr5cmoo6/

Yorgen answered 24/4, 2015 at 17:18 Comment(1)
If you're curious, \00A0 is unicode for a non-breaking space.Yorgen
C
2

You could fake the borders by applying a repeating linear-gradient as the background of the top level ul. Then you'd need to just need a single rule for your list items to set your padding.

Here's an example:

body{
    font-family:arial;
    font-size:14px;
    margin:0;
}
body>ul{
    background:linear-gradient(0deg,#000 3.333%,#fff 3.333%);
    background-size:100% 30px;
}
ul{
    margin:0;
    padding:0;
    list-style-type:none;
}
li{
    line-height:30px;
    padding:0 1em;
}
<ul>
    <li><a href="#">Level 1</a>
        <ul>
            <li><a href="#">Level 2</a></li>
            <li>
                <a href="#">Level 2</a>
                <ul>
                    <li><a href="#">Level 3</a></li>
                    <li><a href="#">Level 3</a></li>
                    <li><a href="#">Level 3</a></li>
                </ul>
            </li>
            <li><a href="#">Level 2</a></li>
        </ul>
    </li>
</ul>
Cohune answered 24/4, 2015 at 17:19 Comment(0)
N
1

There's no way to increase the margin/padding of an element by the nesting.

May this could help you:

a {
    text-decoration: none;
}
ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
}
li {
    border-top: 1px solid;
    border-bottom: 1px solid;
    line-height: 2em;
}
li li {
    border-bottom: none;
}
li li a {
    margin-left: 1em;
}
li li li a {
    margin-left: 2em;
}
li li li li a {
    margin-left: 3em;
}
Nemertean answered 24/4, 2015 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.