CSS :last-child AND :hover on an a link in a ul

夙愿已清 提交于 2021-01-07 03:26:33

问题


I'm styling the last child of a navigation menu which I seem to be able to do fine with the following code:

.aston-menu-light ul > li:last-child {
    border:2px solid blue;
    border-radius: 50px;
    padding:0 20px 0 20px;
} 

.aston-menu-light ul > li > ul > li:last-child {
    border:none !important;
    padding:0 !important;
} 

.aston-menu-light ul > li:last-child:hover {
    background-color:#ffff;
    -webkit-transition: all .5s;
    -o-transition: all .5s;
    transition: all .5s;
} 

The trouble comes when I try and target the <a> on the last child on hover. I'm using this:

.aston-menu-light ul > li > a:last-child:hover {
    color:red !important;
} 

But it seems to style all of the <a> tags and not just the last child. I've tried variations such as: ul > li a but I can't seem to get it o work correctly.

I have a Codepen here: https://codepen.io/shaun-taylor/pen/LXdGGN

The main goal being for this one is just to turn the last link on the top level only red when you hover on it I guess - Thnk you for reading!


回答1:


you should change

.aston-menu-light ul > li > a:last-child:hover {
    color:red !important;
}

to

.aston-menu-light>ul>li:last-child > a:hover {
    color:red !important;
} 

/* CSS Document */


a {
	color: black;
}

nav {
	margin: 50px 0;

}

nav ul {
	padding: 0;
  margin: 0;
	list-style: none;
	position: relative;
	}
	
nav ul li {
	display:inline-block;
}

nav a {
	display:block;
	padding:0 10px;	
	color:#black;
	font-size:20px;
	line-height: 60px;
	text-decoration:none;
}


/* Hide Dropdowns by Default */
nav ul ul {
	display: none;
	position: absolute; 
	top: 60px; /* the height of the main nav */
}
	
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
	display:inherit;
}
	
/* Fisrt Tier Dropdown */
nav ul ul li {
	width:170px;
	float:none;
	display:list-item;
	position: relative;
}

/* Second, Third and more Tiers	*/
nav ul ul ul li {
	position: relative;
	top:-60px; 
	left:170px;
}

	
/* Change this in order to change the Dropdown symbol */
li > a:after { content:  ' +'; }
li > a:only-child:after { content: ''; }

.aston-menu-light ul > li:last-child {
	border:2px solid blue;
	border-radius: 50px;
	padding:0 20px 0 20px;
} 

.aston-menu-light ul > li > ul > li:last-child {
	border:none !important;
	padding:0 !important;
} 

.aston-menu-light ul > li:last-child:hover {
	background-color:#ffff;
	-webkit-transition: all .5s;
	-o-transition: all .5s;
	transition: all .5s;
} 

.aston-menu-light>ul>li:last-child > a:hover {
	color:red !important;
} 
<nav class="aston-menu-light">
    <ul>
        <li><a href="#">link</a>
          <!-- First Tier Drop Down -->
          <ul>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
          </ul>
        </li>
        <li><a href="#">link</a>
          <!-- First Tier Drop Down -->
          <ul>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
          </ul>        
        </li>
        <li><a href="#">link</a></li>
        <li><a href="#">link</a>
          <!-- First Tier Drop Down -->
          <ul>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
          </ul>
        </li>
        <li><a href="#">link</a>
          <!-- First Tier Drop Down -->
          <ul>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
              <li><a href="#">link</a></li>
          </ul>
        </li>
        <li><a href="#">link</a></li>
        <li><a href="#">link</a></li>
    </ul>
</nav>



回答2:


Use This:

.aston-menu-light ul > li:last-child a:hover {
  color:red;
}



回答3:


You shold rewrite

.aston-menu-light ul > li > a:last-child:hover {
  color:red !important;
} 

to

.aston-menu-light ul > li:last-child > a:hover {
  color:red;
} 

What you were doing wrong is that in all the li elements the a element is always the last child! Therefore in all of them, it will turn red when you hover. What you needed was the last li element, therefore using li:last-childin the CSS.

Also, there is no need to use the !important, since this CSS selector is more specific than just

a {
  color: black;
}

It will be red anyway.



来源:https://stackoverflow.com/questions/53433146/css-last-child-and-hover-on-an-a-link-in-a-ul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!