One Style Selector To Specify Both Hover,active Within A Specific Class
I am trying to figure out how to write a style for the following: I only want to style a:hover and a:active for the anchor tags inside .menu, not .menu-switch. The below appears to
Solution 1:
Your code actually has one other problem: the first set of CSS rules are not being grouped correctly. You need to repeat the #main-nav ul li
portion for every selector in the list; otherwise, the rules will apply to other links in the document, not just the ones in #main-nav ul li
.
In the same vein, to group your last two rules you need to group their selectors in their entirety.
Also, in case you weren't aware of it, you probably want to follow the LoVe-HAte mnemonic in arranging your link pseudo-classes, unless you have a good reason to sort :visited
after :hover
and :active
(I've also taken the time to arrange your last two selectors in the same order).
/*Apply the following styles to anchor tags within any ul within #main-nav*/#main-navullia:link,
#main-navullia:visited,
#main-navullia:hover,
#main-navullia:active {
color: #fff;
text-decoration: none;
padding: 10px;
display: block;
border-radius: 4px;
}
/*Apply the following styles to anchor tags within .menu only within #main-nav*/#main-nav.menua:hover,
#main-nav.menua:active {
background-color: #ccc;
}
<divid="main-nav"><ulclass="menu-switch"><li><aclass="js-menu-toggle"href="#">OPEN ME</a></li></ul><ulclass="menu"><li><ahref="#">Menu item 1</a></li><li><ahref="#">Menu item 2</a></li></ul></div>
Solution 2:
#main-navullia:link,
#main-navullia:hover,
#main-navullia:active,
#main-navullia:visited {
color: #000;
text-decoration: none;
padding: 10px;
display: block;
border-radius: 4px;
border: 1px solid orange;
}
/*Apply the following styles to anchor tags within .menu only within #main-nav*/#main-navula:active {
background-color: #ccc;
color: #fff;
}
#main-navula:hover {
background-color: #ccc;
color: #fff;
}
<divid="main-nav"><ulclass="menu-switch"><li><aclass="js-menu-toggle"href="#">OPEN ME</a></li></ul><ulclass="menu"><li><ahref="#">Menu item 1</a></li><li><ahref="#">Menu item 2</a></li></ul></div>
Post a Comment for "One Style Selector To Specify Both Hover,active Within A Specific Class"