简体   繁体   English

CSS中的两个nav元素

[英]Two nav elements in CSS

Problem: 问题:

Writing common CSS code in order to be applied for two nav elements. 编写常见的CSS代码以便应用于两个nav元素。 I have searched all over Google and Stackoverflow without any success. 我在Google和Stackoverflow上搜索都没有成功。 It seems it's not common to use two nav elements while W3C allows it. 在W3C允许的情况下,使用两个导航元素似乎并不常见。

HTML code: HTML代码:

<!-- Global navigation -->
<nav role="main">
    <ul>
        <li><a href="index.html" class="active">Startpage</a></li>
        <li><a href="cars.html">Cars</a></li>
        <li><a href="about.html">About us</a></li>
    </ul>
</nav>

<!-- Local navigation -->
<nav role="sub">
    <ul>
        <li><a href="ferrari.html" class="pressed">Ferrari</a></li>
        <li><a href="bmw.html">BMW</a></li>
        <li><a href="volo.html">Volvo</a></li>
    </ul>
</nav>

CSS code: CSS代码:

How would I write CSS code in order for the two navigation elements to have the same layout, but different styling (color, font size, etc.)? 我如何编写CSS代码,以使两个导航元素具有相同的布局,但不同的样式(颜色,字体大小等)?

You could do something like this: 你可以这样做:

 nav ul li { width:100px; display:inline-block; margin:5px; padding:5px; color:#333; text-align: center; } nav[role="main"] ul li { background-color:#aaa; } nav[role="sub"] ul li { background-color:#eee; } 
  <!-- Global navigation --> <nav role="main"> <ul> <li><a href="index.html" class="active">Startpage</a></li> <li><a href="cars.html">Cars</a></li> <li><a href="about.html">About us</a></li> </ul> </nav> <!-- Local navigation --> <nav role="sub"> <ul> <li><a href="ferrari.html" class="pressed">Ferrari</a></li> <li><a href="bmw.html">BMW</a></li> <li><a href="volo.html">Volvo</a></li> </ul> </nav> 

Not sure what you mean by 不知道你是什么意思

same appearance but different styling 外观相同但样式不同

You can use the role attribute as a CSS selector, as shown here: 您可以将role属性用作CSS选择器,如下所示:

 nav[role="main"], nav[role="sub"] { background: #222; color: #f40; } nav[role="main"] a, nav[role="sub"] a { color: #fff; } 
 <nav role="main"> <ul> <li><a href="index.html" class="active">Startpage</a> </li> <li><a href="cars.html">Cars</a> </li> <li><a href="about.html">About us</a> </li> </ul> </nav> <!-- Local navigation --> <nav role="sub"> <ul> <li><a href="ferrari.html" class="pressed">Ferrari</a> </li> <li><a href="bmw.html">BMW</a> </li> <li><a href="volo.html">Volvo</a> </li> </ul> </nav> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM