Pseudo Class 'nth-child()' not working with ':hover" [duplicate] - html

This question already has an answer here:
Why doesn't nth-of-type/nth-child work on nested elements?
(1 answer)
Closed 2 years ago.
I'm trying to use the pseudo class :nth-child() with :hover but it seems to not work for me. I am trying to change the colour of one element to green when hovered over, but it highlights all of them. I also tried without the hover and none of the colours change.
ul {
background-color: white;
text-decoration: none;
padding: 0;
margin: 0 auto;
}
ul li {
margin: 0 2em;
display: inline-block;
padding: 0;
list-style: none;
}
ul li a {
text-decoration: none;
color: black;
}
a:nth-child(2){
color: green;
}
<ul>
<li>Easy</li>
<li>Medium</li>
<li>Hard</li>
<li>Insane</li>
</ul>

That's because the a isn't the 2nd child - it is an only child of it's parent li. What you are looking for is the a child of the 2nd li element. You can get that like this:
li:nth-child(2) a{ color: green; }
Then for the hover, either of these work with the code in your question. It depends on what you want to target with the hover:
// When the <a> in the second li is hovered, change it's colour
li:nth-child(2) a:hover{ color: green; }
/* OR */
/* When the second li is hovered, change the colour of the <a> it contains */
li:nth-child(2):hover a{ color: green; }
Working Example (using different colours to show it working):
ul {
background-color: white;
text-decoration: none;
padding: 0;
margin: 0 auto;
}
ul li {
margin: 0 2em;
display: inline-block;
padding: 0;
list-style: none;
}
ul li a {
text-decoration: none;
color: black;
}
/* change colour of 2nd link */
li:nth-child(2) a{
color: blue;
}
/* change colour of 2nd link on hover */
li:nth-child(2):hover a{
color: green;
}
/* change colour of 3rd link on hover */
li:nth-child(3) a:hover{
color: red;
}
<ul>
<li>Easy</li>
<li>Medium</li>
<li>Hard</li>
<li>Insane</li>
</ul>
Reference: Mozilla MDN Docs for nth-child

I hope this code may help you. And learn more about pseudo-classes and elements using this link.Click here to learn about CSS Pseudo-classes
li:nth-child(2):hover a{
color: red;
}

Related

why are my buttons not on top of each other when I have already used display: block?

i want 2 of my buttons to be stacked (on top of each other). however, after using display: block, the buttons are still side by side. here are my html & css code!
does anyone know why? i had done my research and display:block is the way to code my buttons to make them on top of each other. would appreciate any help!
thank you in advance for everyone that have helped me! i'm really grateful. :)
.homepage_viewcurrentproject
{
font-family : Times New Roman;
font-size : 28px;
color : #FFFFFF;
color : rgb(255, 255, 255);
}
nav#button
{
float: left;
padding: 15px;
width: 150px;
height:350px;
margin-top: 15px;
margin-left: 65px;
text-align: center;
}
nav#button ul li
{
margin: 0; /*Setting margins and padding to 0 to remove browser default settings*/
padding: 0;
list-style-type: none; /*remove the bullets*/
color: white;
}
nav#button li a
{
display: block; /*to allow changes of- width,height, padding and margin around the link*/
width: 350px; /* length of button */
padding: 5px 10px; /*top and bottom:5px left and right:10px*/
background-color: #25374C;
text-decoration: none; /* remove the underline of hyperlink */
margin:10px;
padding: 30px 40px;
background: #25374C;
border-radius: 18px;
}
nav#button a:link{
color: white;
text-decoration: none;
}
nav#button a:visited{
color: white;
}
nav#button li a:hover, /*add a style to an element when you mouse over it*/
button li a:focus /*add a style to an element that has keyboard input focus*/
{
background-color: #2c425c;
color: #CCFFC5;
}
nav#button li a:active /*add a style to an element that is activated*/
{
background-color: #25374C;
}
<nav id="button" class="homepage_viewcurrentproject">
<ul>
<li>
Check out my Python Study
</li>
<li>
View latest project
</li>
</ul>
</nav>
enter image description here
You can use this method:
nav ul {
display: flex;
flex-direction: column;
}

simple way of making selected navigation page link change colour?

I know this is a silly question but for some reason I'm always having a problem with changing the background colour of a selected navigation item, I looked this up so many times and I tried doing the selected classes for a button but for some reason it doesn't work for me, can someone point out what am I doing wrong?
html:
<div id="nav">
<ul>
<li><a class="selected" href="?page=home">Home</a></li>
<li>Gallery</li>
<li>About</li>
<li>Contact</li>
<li>Shop</li>
</ul>
</div>
css:
#nav {
list-style: none;
width: 100%;
text-align: center;
background-color: #000;
height:52px;
background-color: #000; opacity: 0.7; filter: alpha(opacity=50);
}
#nav ul {
list-style-type: none;
margin: 0px;
padding: 10px;
padding-left: 650px;
}
#nav li {
margin: 0px;
display: inline-block;
}
#nav li a {
padding: 10px;
text-decoration: none;
font-family: 'Quicksand';
font-size: 25px;
color: #fff;
background-color: #000;
}
#nav li a:hover {
color: black;
background-color: #999;
background-color: #666;
color: white;
}
li.selected a { background-color: blue;
}
You've two issues here.
You are using li.selected a but you are assigning your class to a tag so the selector should be li a.selected
Specificity. You have #nav in all the selectors, since ID selectors are more specific, you need to add it to your .active selector as well. So it should be #nav li a.selected
Demo
Suggestion :
I would recommend you to use class instead of id. Keep ID's for JavaScript selectors, as it can access your DOM faster, but for CSS, stick to classes as much as you can, else you will end up with long specific selectors and even !important.
So you should have something like <div id="nav" class="nav"> and use .nav in CSS instead of #nav

How do I make my navigation link have a box around it upon hover?

Sorry for the odd title, didn't really know what to call it.
I'm new to designing, as I generally stick to back-end but a friend suggested I give it a go. What I'm trying to do is get this
to look like this
My current css for it is as follows.
#main-nav {
margin-top:70px;
}
#main-nav ul {
list-style-type: none;
}
#main-nav li {
display:inline-block;
position:relative;
}
#main-nav a {
text-transform: uppercase;
text-decoration: none;
color: #FFFFFF;
}
#main-nav a:hover {
background-color: #2c3e50;
}
I have no clue what I'm doing when it comes to designing and it may be an easy solution but I don't have the slightest idea.
Thanks in advance.
You need to add padding and border to your CSS:
padding: 10px;
border: 1px solid #333;
The complete CSS you might need is:
#main-nav a {
text-transform: uppercase;
text-decoration: none;
color: #FFFFFF;
padding: 10px;
display: block;
}
#main-nav a:hover {
background-color: #2c3e50;
border: 1px solid #333;
}
Edited as question has changed. As others have said, you want to add padding to your a:hover.
#main-nav a:hover {
background-color: #2c3e50;
padding: 10px;
}
Your a element needs display: block as well. You're adding a background to the a element, and by default that element is inline, meaning the height and width of the bounding box are determined by the contents. You will also need to add some padding.
#main-nav {
background: #000077;
}
#main-nav ul {
list-style-type: none;
}
#main-nav li {
display: inline-block;
}
#main-nav a {
padding: 15px;
display: block;
color: #FFFFFF;
}
#main-nav a:hover {
background-color: #2c3e50;
}
<ul id="main-nav">
<li>What?</li>
</ul>
Note that display: block is necessary, since otherwise the layout of the button will be dictated by the text contained in it rather than the width and height computed with the padding. If you add padding without modifying the display property, you will notice that the padding has an effect, but the ul and li size still according to the text rather than the computed width and height.
Check out the snippet below to see what I mean:
#main-nav {
background: #000077;
}
#main-nav ul {
list-style-type: none;
}
#main-nav li {
display: inline-block;
}
#main-nav a {
padding: 15px;
color: #FFFFFF;
}
#main-nav a:hover {
background-color: #2c3e50;
}
<ul id="main-nav">
<li>What?</li>
</ul>

How to remove an underline on a pseudo-element?

On Chrome and Firefox, if I apply a text-decoration:underline on a tag, by default the underline does not apply to the pseudo element.
But on IE it does, and I can't remove it.
I want the link to be underlined, but not the pseudo element.
It work if I add a span inside and put the underline on it, but I want to know if it can be made without additional markup.
a{
padding-left: 9px;
position:relative;
display:inline-block;
}
a:before{
content:'\203A\00a0';
position:absolute;
top:0;
left:0;
display: inline-block;
}
#underline{
text-decoration: none;
}
#underline:hover{
text-decoration:underline;
}
/* special for IE */
#underline:hover:before
{
text-decoration:none !important; /* does nothing ! */
}
#color{
color:green;
}
#color:hover{
color:red;
}
#color:hover:before{
color:green; /* work ! */
}
#span{
text-decoration: none;
}
#span:hover span{
text-decoration: underline;
}
underline
<br>
color
<br>
<span>with span</span>
It seems that IE don't let you override the text-decoration in the pseudoelement if it isn't set in it.
First let the pseudo-element be underlined - text-decoration: underline - and afterwards override this with textdecoration: none.
#underline:hover:before
{
text-decoration:underline;
}
#underline:hover:before
{
text-decoration:none;
}
As text-decoration: underline; can't be overridden in IE you could use border-bottom: 1px solid green; instead. This can then be overwritten on the :before by setting its border colour to the background colour (in this case white). This will only work on solid colour backgrounds though.
a {
color: green;
display: inline-block;
padding-left: 9px;
position: relative;
text-decoration: none;
}
a:before {
content: '\203A\00a0';
display: inline-block;
left: 0;
position: absolute;
top: 0;
}
a:hover {
border-bottom: 1px solid green;
}
a:hover:before {
border-bottom: 1px solid white;
}
Hover to check underline
you can add this to your css. this helped me in the IE
a {text-decoration:none;}
a:hover {text-decoration:underline;}
a:before,a:after { text-decoration:underline;}
a:before,a:after,
a:hover:before,a:hover:after {text-decoration:none;}
a:link { text-decoration: none; }
a:visited { text-decoration: none; }
a:hover { text-decoration: none; }
a:active { text-decoration: none; }

Pure CSS dropdown menu

I'm trying to fashion a 100% CSS and HTML dropdown menu like what's seen on http://phpbb.com. When you hover over the navigation links, a new div appears just below the one you hovered onto.
What I'm trying to do is make .submenu appear just below the <li> that it's nested into by using #nav li a:hover submenu {. To my knowledge this CSS selector should select the .submenu DIV when an a element is hovered over? But it doesn't work.
#nav {
list-style-type: none;
margin: -5px 0px 0px 5px;
}
#nav li {
display: inline;
}
#nav li a {
display: block;
padding: 3px;
float: left;
margin: 0px 10px 0px 10px;
text-decoration: none;
color: #fff;
font-weight: bold;
position: relative;
}
#nav li a:hover {
text-shadow: 1px 1px #333;
}
#nav li a:hover submenu {
display: block;
color: red;
}
.submenu {
position: absolute;
display: none;
}
<ul id="nav">
<li>Home
</li>
<li>
Skins
<div class="submenu">
hello :)
</div>
</li>
<li>Guides
</li>
<li>About
</li>
</ul>
Your second to last selector is looking for a "submenu" element, you should correct this to say ".submenu"
Like this:
/*#nav li a:hover submenu {*/
#nav li a:hover .submenu {
display: block;
color: red;
}
EDIT:
To get the hover to work, you also need to adjust your CSS so that the hover is applied to the list item, instead of the anchor tag:
#nav li:hover .submenu {
display: block;
color: red;
}
Are you missing a period ('.') before submenu in the selector #nav li a:hover submenu?
Try to edit this following part.
Put a . (dot) before the submenu, since its a class.
#nav li a:hover .submenu {
display: block;
color: red;
}
#nav li:hover .submenu {
display: block;
color: red;
}
You want the submenu to appear when you hover on li, not on a, simply because you do not have items with a class submenu inside the a.
Also you could consider using s for the submenus.