D
D
Diesel-nick2016-08-02 06:24:35
css
Diesel-nick, 2016-08-02 06:24:35

CSS Sidebar - how to keep the tree view and make the selection of the entire line (ignoring indentation)?

How to make a garden bar in the form of a tree with (i.e. the descendant menu should recede from the edge by a greater distance than the parent) and at the same time a:hoverselect the entire line (and not the part where it starts <a>)
Example at jsfiddle.net
d65a4849556144d3a46a658c6785bf27.png

<span>Sidebar</span>
<div id="sidebar">
  <ul>
    <li class="nav-item">
      <a href="#" class="nav-link">One item</a>
    </li>
    <li class="nav-item">
      <a href="#" class="nav-link">Two item</a>
    </li>
    <ul class="submenu">
      <li class="nav-item">
        <a href="#" class="nav-link">Submenu one item</a>
      </li>
      <ul class="submenu">
        <li class="nav-item">
          <a href="#" class="nav-link">Submenu one item</a>
        </li>
        <li class="nav-item">
          <a href="#" class="nav-link">Submenu two item</a>
        </li>
      </ul>
      <li class="nav-item">
        <a href="#" class="nav-link">Submenu two item</a>
      </li>
    </ul>
  </ul>
</div>

#sidebar {
  background: #e8e8ee;
  width: 300px;
  ul {
    list-style: none;
  }
  .nav-link {
    display: block;
    line-height: 40px;
    text-decoration: none;
    &:hover {
      background: green;
    }
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
artem_music, 2016-08-02
@artem_music

You can try pseudo-elements

ul{
padding-left:20px;
}
li{
position:relative;
}
a:hover:after{
content:'';
position:absolute;
left:0;
top:0;
width:100%;
height:100%;
background:green;
}

A
Ankhena, 2016-08-02
@Ankhena

:hover is applied not to the link, but to li
for links to increase the indent, for li - no.
If the number of nesting levels is known, then styles.
If not, then you have to consider, for example, js.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question