Css nested list with active item to fill the whole row

 ̄綄美尐妖づ 提交于 2019-12-11 16:39:50

问题


I have an unlimited nested list that serves as a menu. I have an .active list item to add a background color to the anchor.

I would like it to fill the whole row with the background color, not start a few tabs in. That means I would need the a tag to fill the whole row and at the same time have the text appear indented.

.menu {
  background: #eee;
  width: 300px;
}

.menu > ul {
  margin-left: -25px;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  padding-left: 25px;
}

li.active a {
  background: red;
}

a {
  display: block;
}
<div class="menu">
<ul>
  <li><a href="#">Level 1</a></li>
  <li>
    <ul>
      <li>
        <ul>
          <li class="active">
            <a href="#">Level 3</a>
          </li>
        </ul>
        <a href="#">Level 2</a>
       </li>
    </ul>
    <a href="#">Level 1</a>
   </li>
  <li><a href="#">Level 1</a></li>
</ul>
</div>

回答1:


I would use a pesudo-element to create the background like this:

.menu {
  background: #eee;
  width: 300px;
  overflow: hidden;
}

.menu>ul {
  margin-left: -25px;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  padding-left: 25px;
  position: relative;
}

li.active:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: -1000px; /* a big value so it works with any level*/
  background: red;
  z-index: 0;
}

a {
  display: block;
  position: relative;
  z-index: 1;
}
<div class="menu">
  <ul>
    <li><a href="#">Level 1</a></li>
    <li>
      <ul>
        <li>
          <ul>
            <li class="active">
              <a href="#">Level 3</a>
            </li>
          </ul>
          <a href="#">Level 2</a>
        </li>
      </ul>
      <a href="#">Level 1</a>
    </li>
    <li><a href="#">Level 1</a></li>
  </ul>
</div>


来源:https://stackoverflow.com/questions/48705507/css-nested-list-with-active-item-to-fill-the-whole-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!