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

前端之家收集整理的这篇文章主要介绍了html – 如何在嵌套列表样式中分隔样式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个列表,列表中也有列表.
我在父列表上设置样式,但我想要父母和子列表的不同样式,但它们混合在一起我不能分开它们.

HTML文件

  1. <ul id="accountNavigation">
  2. <li><a href="#">Something</a></li>
  3. <li id="userNavigation">
  4. <img src="https://si0.twimg.com/profile_images/135460415/UAB_dragon_head_normal.png" alt=""/>
  5. <a href="#">Username</a>
  6. <div class="showme">
  7. <ul id="userNavigationSubMenu">
  8. <li>test</li>
  9. <li>test</li>
  10. <li>test</li>
  11. <li>test</li>
  12. <li>test</li>
  13. <li>test</li>
  14. <li>test</li>
  15.  
  16. </ul>
  17. </div>
  18. </li>
  19. </ul>

CSS文件

  1. body{background:#ff0000;}
  2. #accountNavigation{ list-style: none;float: right;height: 44px;}
  3. #accountNavigation li{ float: left;color: #fff;height: 44px;}
  4. #accountNavigation li:hover{ background: #ddd;cursor: pointer;}
  5. #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;}
  6. #accountNavigation li img{ position: absolute;top: 12px;left: 10px;width: 22px;height: 22px;}
  7. #userNavigation{position: relative;}
  8. #userNavigation a {padding-left: 38px !important;}
  9.  
  10. #userNavigation{}
  11. #userNavigation:hover{}
  12. #userNavigation:hover .showme{display: inline;}
  13. .showme
  14. {
  15. display: none;
  16. width: 140px;
  17. height: 200px;
  18. background: #f5f5f5;
  19. margin: 0px auto;
  20. padding: 10px 5px 0px 5px;
  21. border: 1px solid #ddd;
  22. border-top: none;
  23. z-index: 10;
  24. position: absolute;
  25. right:0;
  26. top: auto;
  27.  
  28. }
  29. #userNavigation ul { list-style: none;}

这是fiddle.

解决方法

只需使用>直接/直接后代组合子,以及用于指定您要定位的li(或ul)元素的ID:
  1. #accountNavigation { /* outer ul element */
  2. }
  3.  
  4. #accountNavigation > li { /* outer ul element's children li */
  5. }
  6.  
  7. #accountNavigation > li > ul { /* first 'inner' ul element */
  8. }
  9.  
  10. #accountNavigation > li > ul > li { /* first 'inner' ul element's li children */
  11. }

当然,您可以更通用,只需使用:

  1. ul { /* targets all ul elements */
  2. /* general styles */
  3. }
  4. ul li { /* targets all li elements within a ul */
  5. /* general styles */
  6. }
  7. ul li ul { /* targets all ul elements within an li element,itself within a ul */
  8. /* overrule general 'outer' styles */
  9. }
  10. ul li ul li { /* targets all li elements within a ul element,within an li element,itself within a ul...and so on */
  11. /* overrule general 'outer' styles */
  12. }

使用一般方法

  1. <ul>
  2. <li>This should be green!</li>
  3. <li>This is also green...
  4. <ul>
  5. <li>But this is not,it's,um...blue!</li>
  6. <li>And so on...</li>
  7. </ul></li>
  8. <li>This is green too,just because.</li>
  9. </ul>

以下CSS应证明其用途:

  1. ul li {
  2. color: green; /* the 'general'/'default' settings */
  3. margin-left: 10%;
  4. }
  5.  
  6. ul li ul li {
  7. color: blue; /* this overrides the 'general' color setting */
  8. /* the margin is not overridden however */
  9. }​

JS Fiddle demo.

参考文献:

> CSS Selectors (Level 3),at the W3.org.

猜你在找的HTML相关文章