更改当前选项卡下拉菜单颜色

时间:2017-03-19 03:47:40

标签: html css css3

我对下拉菜单的文字颜色有疑问。下拉文本颜色基于主链接文本颜色。当我选择该链接时,它会更改为当前选项卡颜色,但也会更改下拉文本颜色。我想知道如何使下拉菜单颜色独立于主标签文本颜色。

这是HTML:

<nav id="nav-wrap">

           <a class="mobile-btn" href="#nav-wrap" title="Show navigation">Show navigation</a>
            <a class="mobile-btn" href="#" title="Hide navigation">Hide navigation</a>

           <ul id="nav" class="nav">

               <li><a href="index.html">Home</a></li>
               <li class="current"><a href="blog.html">Blog</a>
              </li>
              <li><span><a href="portfolio-index.html">Resources</a></span>
                 <ul>
                    <li><a href="alcoholre.html">Alcohol</a></li>
                    <li><a href="drugsre.html">Drugs</a></li>
                    <li><a href="mentalhealthre.html">Mental Health</a></li>
                    <li><a href="suicidere.html">Suicide</a></li>
                 </ul>
              </li>
               <li><a href="about.html">About</a></li>
              <li><a href="contact.html">Contact</a></li>

           </ul> <!-- end #nav -->

        </nav> <!-- end #nav-wrap -->

这是CSS:

#nav-wrap ul, #nav-wrap li, #nav-wrap a {
margin: 0;
padding: 0;
border: none;
outline: none;
}

/* nav-wrap */
#nav-wrap {
position: relative;
font: 15px raleway-heavy, sans-serif;
text-transform: uppercase;
letter-spacing: 1.5px;
float: right;
margin-top: 32px;
margin-right: 20px;
z-index: 99999;
}

/* hide toggle button */
#nav-wrap > a.mobile-btn {
display: none;
border-radius: 3px;
}

ul#nav {
min-height: 48px;
width: auto;

/* left align the menu */
text-align: left;
}
ul#nav li {
position: relative;
list-style: none;
height: 48px;
display: inline-block;
}

/* Links */
ul#nav li a {

/* 8px padding top + 8px padding bottom + 32px line-height = 48px */

display: inline-block;
padding: 8px 11px;
line-height: 32px;
text-decoration: none;
text-align: left;
color: #ffffff;

-webkit-transition: color .2s ease-in-out;
-moz-transition: color .2s ease-in-out;
-o-transition: color .2s ease-in-out;
-ms-transition: color .2s ease-in-out;
transition: color .2s ease-in-out;
}

ul#nav li a:active { background-color: transparent !important; }
ul#nav li:hover > a,
ul#nav li.current a { color: #00b2ee; }

/* adds down arrow */
ul#nav span:after {
width: 0;
height: 0px;
border: 4px solid transparent;
border-bottom: none;
border-top-color: #00b2ee;
content: '';
vertical-align: middle;
display: inline-block;
position: relative;
right: 5px;
}

/* Sub Menu
----------------------------------------------------- */
ul#nav ul {
position: absolute;
top: 100%;
left: 0;
background: #c7c7c7;
min-width: 100%; 


border-radius: 5px 5px 7px 7px;

/* for transition effects */
opacity: 0;
filter: alpha(opacity=0);

-webkit-transition: opacity .25s ease .1s;
-moz-transition: opacity .25s ease .1s;
-o-transition: opacity .25s ease .1s;
-ms-transition: opacity .25s ease .1s;
transition: opacity .25s ease .1s;
}

/* Third level sub menu
ul#nav ul ul {
position: absolute;
top: 0;
left: 100%;

border-radius: 0 3px 3px 3px;
}
*/

ul#nav ul li {
padding: 0;
display: block;
text-align: left;

/* for transition effects */
height: 0;
overflow: hidden;

-webkit-transition: height .25s ease .1s;
-moz-transition: height .25s ease .1s;
-o-transition: height .25s ease .1s;
-ms-transition: height .25s ease .1s;
transition: height .25s ease .1s;
}

/*On Hover */
ul#nav li:hover > ul { 
opacity: 1; filter: alpha(opacity=100);
}
ul#nav li:hover > ul li {
height: 42px;
overflow: visible;
border-bottom: 1px solid #26272C;
color: #ffffff;
}
ul#nav li:hover > ul li:last-child  { border: none; }

/* Sub Menu Anchor links */
ul#nav ul li a {
padding: 6px 15px;
margin: 0;
white-space: nowrap;
font-size: 13px;
}

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

在css脚本的末尾添加以下内容

ul#nav ul li a:hover{
color:red;
}

示例:

&#13;
&#13;
#nav-wrap ul, #nav-wrap li, #nav-wrap a {
margin: 0;
padding: 0;
border: none;
outline: none;
}

/* nav-wrap */
#nav-wrap {
position: relative;
font: 15px raleway-heavy, sans-serif;
text-transform: uppercase;
letter-spacing: 1.5px;
float: right;
margin-top: 32px;
margin-right: 20px;
z-index: 99999;
}

/* hide toggle button */
#nav-wrap > a.mobile-btn {
display: none;
border-radius: 3px;
}

ul#nav {
min-height: 48px;
width: auto;

/* left align the menu */
text-align: left;
}
ul#nav li {
position: relative;
list-style: none;
height: 48px;
display: inline-block;
}

/* Links */
ul#nav li a {

/* 8px padding top + 8px padding bottom + 32px line-height = 48px */

display: inline-block;
padding: 8px 11px;
line-height: 32px;
text-decoration: none;
text-align: left;
color: #ffffff;

-webkit-transition: color .2s ease-in-out;
-moz-transition: color .2s ease-in-out;
-o-transition: color .2s ease-in-out;
-ms-transition: color .2s ease-in-out;
transition: color .2s ease-in-out;
}

ul#nav li a:active { background-color: transparent !important; }
ul#nav li:hover > a,
ul#nav li.current a { color: #00b2ee; }

/* adds down arrow */
ul#nav span:after {
width: 0;
height: 0px;
border: 4px solid transparent;
border-bottom: none;
border-top-color: #00b2ee;
content: '';
vertical-align: middle;
display: inline-block;
position: relative;
right: 5px;
}

/* Sub Menu
----------------------------------------------------- */
ul#nav ul {
position: absolute;
top: 100%;
left: 0;
background: #c7c7c7;
min-width: 100%; 


border-radius: 5px 5px 7px 7px;

/* for transition effects */
opacity: 0;
filter: alpha(opacity=0);

-webkit-transition: opacity .25s ease .1s;
-moz-transition: opacity .25s ease .1s;
-o-transition: opacity .25s ease .1s;
-ms-transition: opacity .25s ease .1s;
transition: opacity .25s ease .1s;
}

/* Third level sub menu
ul#nav ul ul {
position: absolute;
top: 0;
left: 100%;

border-radius: 0 3px 3px 3px;
}
*/

ul#nav ul li {
padding: 0;
display: block;
text-align: left;

/* for transition effects */
height: 0;
overflow: hidden;

-webkit-transition: height .25s ease .1s;
-moz-transition: height .25s ease .1s;
-o-transition: height .25s ease .1s;
-ms-transition: height .25s ease .1s;
transition: height .25s ease .1s;
}

/*On Hover */
ul#nav li:hover > ul { 
opacity: 1; filter: alpha(opacity=100);
}
ul#nav li:hover > ul li {
height: 42px;
overflow: visible;
border-bottom: 1px solid #26272C;
color: #ffffff;
}
ul#nav li:hover > ul li:last-child  { border: none; }

/* Sub Menu Anchor links */
ul#nav ul li a {
padding: 6px 15px;
margin: 0;
white-space: nowrap;
font-size: 13px;
}
ul#nav ul li a:hover{
color:red;
}
&#13;
<nav id="nav-wrap">

           <a class="mobile-btn" href="#nav-wrap" title="Show navigation">Show navigation</a>
            <a class="mobile-btn" href="#" title="Hide navigation">Hide navigation</a>

           <ul id="nav" class="nav">

               <li><a href="index.html">Home</a></li>
               <li class="current"><a href="blog.html">Blog</a>
              </li>
              <li><span><a href="portfolio-index.html">Resources</a></span>
                 <ul>
                    <li><a href="alcoholre.html">Alcohol</a></li>
                    <li><a href="drugsre.html">Drugs</a></li>
                    <li><a href="mentalhealthre.html">Mental Health</a></li>
                    <li><a href="suicidere.html">Suicide</a></li>
                 </ul>
              </li>
               <li><a href="about.html">About</a></li>
              <li><a href="contact.html">Contact</a></li>

           </ul> <!-- end #nav -->

        </nav> <!-- end #nav-wrap -->
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我使用您的代码创建了fiddle。将鼠标悬停在下拉菜单链接上时,文本颜色将更改为当前菜单选项卡上使用的蓝色。如果您不想在下拉菜单中使用蓝色文字悬停并希望使用白色文字链接,只需删除&#34; ul#nav li:hover&gt;一个,&#34;从您的代码开始,它将完成工作。

现在您的新CSS代码应如下所示: -

#nav-wrap ul, #nav-wrap li, #nav-wrap a {
margin: 0;
padding: 0;
border: none;
outline: none;
}

/* nav-wrap */
#nav-wrap {
position: relative;
font: 15px raleway-heavy, sans-serif;
text-transform: uppercase;
letter-spacing: 1.5px;
float: right;
margin-top: 32px;
margin-right: 20px;
z-index: 99999;
}

/* hide toggle button */
#nav-wrap > a.mobile-btn {
display: none;
border-radius: 3px;
}

ul#nav {
min-height: 48px;
width: auto;

/* left align the menu */
text-align: left;
}
ul#nav li {
position: relative;
list-style: none;
height: 48px;
display: inline-block;
}

/* Links */
ul#nav li a {

/* 8px padding top + 8px padding bottom + 32px line-height = 48px */

display: inline-block;
padding: 8px 11px;
line-height: 32px;
text-decoration: none;
text-align: left;
color: #ffffff;

-webkit-transition: color .2s ease-in-out;
-moz-transition: color .2s ease-in-out;
-o-transition: color .2s ease-in-out;
-ms-transition: color .2s ease-in-out;
transition: color .2s ease-in-out;
}

ul#nav li a:active { background-color: transparent !important; }
ul#nav li.current a { color: #00b2ee; }

/* adds down arrow */
ul#nav span:after {
width: 0;
height: 0px;
border: 4px solid transparent;
border-bottom: none;
border-top-color: #00b2ee;
content: '';
vertical-align: middle;
display: inline-block;
position: relative;
right: 5px;
}

/* Sub Menu
----------------------------------------------------- */
ul#nav ul {
position: absolute;
top: 100%;
left: 0;
background: #c7c7c7;
min-width: 100%; 


border-radius: 5px 5px 7px 7px;

/* for transition effects */
opacity: 0;
filter: alpha(opacity=0);

-webkit-transition: opacity .25s ease .1s;
-moz-transition: opacity .25s ease .1s;
-o-transition: opacity .25s ease .1s;
-ms-transition: opacity .25s ease .1s;
transition: opacity .25s ease .1s;
}

/* Third level sub menu
ul#nav ul ul {
position: absolute;
top: 0;
left: 100%;

border-radius: 0 3px 3px 3px;
}
*/

ul#nav ul li {
padding: 0;
display: block;
text-align: left;

/* for transition effects */
height: 0;
overflow: hidden;

-webkit-transition: height .25s ease .1s;
-moz-transition: height .25s ease .1s;
-o-transition: height .25s ease .1s;
-ms-transition: height .25s ease .1s;
transition: height .25s ease .1s;
}

/*On Hover */
ul#nav li:hover > ul { 
opacity: 1; filter: alpha(opacity=100);
}
ul#nav li:hover > ul li {
height: 42px;
overflow: visible;
border-bottom: 1px solid #26272C;
color: #ffffff;
}
ul#nav li:hover > ul li:last-child  { border: none; }

/* Sub Menu Anchor links */
ul#nav ul li a {
padding: 6px 15px;
margin: 0;
white-space: nowrap;
font-size: 13px;
}

如果您希望在将鼠标悬停在下拉文本链接上时使用某种不同的颜色,则需要在css中单独使用此代码: -

ul#nav li:hover > a {color:your color name or color code}
相关问题