Flexible, Multi-Level Nesting Solution
This is very similar to another question I answered here, and I've composed a similar solution for you below. You will want valid html by having all nested li
elements inside their own ul
(as others have noted here), and it would be best to control all this by some class on the outermost ul
(though that is not required, but makes targeting this list a whole lot easier).
The key here is supplying the background
through the :before
pseudo-element, which is made to span the whole width of the outermost ul
.
Here is my demo jsbin.
HTML
<ul class="full-width-list">
<li>A</li>
<li>
<ul>
<li>B</li>
<li>
<ul>
<li>B</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
.full-width-list {
position: relative;
padding: 0 0 0 4px;
}
.full-width-list ul {
margin: 0;
padding: 0
}
.full-width-list li {
list-style:none;
padding: 0;
margin: 0;
height: 1.2em;
line-height: 1.2em;
}
.full-width-list ul > li {
margin-top: 4px;
padding: 0 0 0 36px;
}
.full-width-list li:first-child:before {
content: '';
height: 1.2em;
position: absolute;
left: 0;
right: 0;
z-index: -1;
background:red;
}
.full-width-list li:first-child:hover:before {
background:green
}
Limitations
This solution has two main limitations:
- None of the
ul
or li
elements can have a position
other than the default static
set on them, as the :before
pseudo-element of the li
elements needs to have its only positioned parent be the .full-width-list
element.
- There has to be a set
height
on the li
items. In my example I use 1.2em
, but whatever height
you set, it means that the li
elements cannot go to two or more lines of text (so this solution only works with a single line of text).