html - 如何在嵌套列表样式中分隔样式?

标签 html css nested html-lists nested-lists

我有一个列表,列表中也有列表。
我在父列表上设置了样式,但我想为父列表和子列表设置不同的样式,但它们以某种方式混合在一起,我无法将它们分开。

HTML 文件:

<ul id="accountNavigation">
  <li><a href="#">Something</a></li>                    
  <li id="userNavigation">
    <img src="https://si0.twimg.com/profile_images/135460415/UAB_dragon_head_normal.png" alt=""/>
    <a href="#">Username</a>
    <div class="showme">
      <ul id="userNavigationSubMenu">
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>
        <li>test</li>

      </ul>
    </div>
  </li>
</ul>

CSS 文件:

body{background:#ff0000;}
#accountNavigation{ list-style: none;float: right;height: 44px;}
#accountNavigation li{ float: left;color: #fff;height: 44px;}
#accountNavigation li:hover{ background: #ddd;cursor: pointer;}
#accountNavigation li a{ text-decoration: none;color: #fff;line-height: 44px;font-weight: bold;font-size: 13px;height: 44px;padding: 15px 27px 0 14px;outline: none;}
#accountNavigation li img{ position: absolute;top: 12px;left: 10px;width: 22px;height: 22px;}
#userNavigation{position: relative;}
#userNavigation a {padding-left: 38px !important;}

#userNavigation{}
#userNavigation:hover{}
#userNavigation:hover .showme{display: inline;}
.showme
{
  display: none;
  width: 140px;
  height: 200px;
  background: #f5f5f5;
  margin: 0px auto;
  padding: 10px 5px 0px 5px;
  border: 1px solid #ddd;
  border-top: none;
  z-index: 10;
  position: absolute;
  right:0;
  top: auto;

}
#userNavigation ul { list-style: none;}

这是 fiddle .

最佳答案

只需使用 > 直接/直接后代组合器和一个 id 来指定哪个 li(或 ul) 您定位的元素:

#accountNavigation { /* outer ul element */
}

#accountNavigation > li { /* outer ul element's children li */
}

#accountNavigation > li > ul { /* first 'inner' ul element */
}

#accountNavigation > li > ul > li { /* first 'inner' ul element's li children */
}

当然,您可以更通用并简单地使用:

ul { /* targets all ul elements */
    /* general styles */
}
ul li { /* targets all li elements within a ul */
    /* general styles */
}
ul li ul { /* targets all ul elements within an li element, itself within a ul */
    /* overrule general 'outer' styles */
}
ul li ul li { /* targets all li elements within a ul element,
                 within an li element, itself within a ul...and so on */
    /* overrule general 'outer' styles */
}

使用一般方法:

<ul>
    <li>This should be green!</li>
    <li>This is also green...
        <ul>
            <li>But this is not, it's, um...blue!</li>
            <li>And so on...</li>
        </ul></li>
    <li>This is green too, just because.</li>
</ul>

以下 CSS 应演示其用法:

ul li {
    color: green; /* the 'general'/'default' settings */
    margin-left: 10%;
}

ul li ul li {
    color: blue; /* this overrides the 'general' color setting */
                 /* the margin is not overridden however */
}​

JS Fiddle demo .

引用资料:

关于html - 如何在嵌套列表样式中分隔样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12787342/

相关文章:

CSS 动画宽度从 0px 到内联 css 定义的 % 值

javascript - 如何在 JavaScript 中创建嵌套元素?

python - 用单个 for 遍历嵌套列表

正则表达式正在运行,但代码看起来很糟糕

html - IE 条件注释不起作用

c# - 从 PDF 转换为 HTML

css - IE11 @media 宽度

javascript - 获取 div 标签内容并将其应用于 javascript 中的内联样式

HTML5 地理位置 : logic to get initial position as well as watchLocation?

javascript - Javascript 可以修改 HTML 流而不是 DOM 树吗?