Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code so far is simple but I'm not sure how to overwrite the default background-color I have set for this class. the necessary html:

HTML
<!--This is the site navigation -->
<div class="navbar" id="navbar">
    <a href=""><img src="./Images/Hogwarts_Crest.png" alt="Welcome Home" id="home_image"></a>
    <ul class="navbar">
        <li class="Hufflepuff"><a href="">Browse</a></li>
        <li class="Gryffindor"><a href="">Submit</a></li>
        <li class="Ravenclaw"><a href="">Profile</a></li>
        <li class="Slytherin"><a href="">Settings</a></li>
    </ul>
</div>


and the css that goes with it:

CSS
/*general settings for the houses*/
.Hufflepuff {
	background-color: rgba(255, 255, 0, 1);
	color: Black;
}

.Gryffindor {
	background-color: rgba(255, 0, 0, 1);
	color: rgb(255, 215, 0);
}

.Ravenclaw {
	background-color: rgba(0, 0, 255, 1);
	color: rgb(205, 157, 50);
}

.Slytherin {
	/*background-color is emerald*/
	background-color: rgba(80, 200, 120, 1);
	color: rgb(211, 211, 211);
}

/*settings specifically for links in houses*/
.Hufflepuff a {
	color: Black;
}

.Gryffindor a {
	color: rgb(255, 215, 0);
}

.Ravenclaw a {
	/*text color is bronze*/
	color: rgb(205, 157, 50);
}

.Slytherin a {
	color: rgb(211, 211, 211);
}

/*hover colors*/
.Hufflepuff a:hover {
	background-color: rgba(255, 255, 0, 0.5)
	color: Black;
}

.Gryffindor a:hover {
	background-color: rgba(255, 0, 0, 0.5)
	color: rgb(255, 215, 0);
}

.Ravenclaw a:hover {
	background-color: rgba(0, 0, 255, 0.5)
	/*text color is bronze*/
	color: rgb(205, 157, 50);
}

.Slytherin a:hover {
	background-color: rgba(80, 200, 120, 0.5)
	color: rgb(211, 211, 211);
}


The idea here is that there is a horizontal navigation bar at the top of the screen. Each button has it's own background and text color. When the mouse hovers over a button, I want the alpha decreased by .5 on the background-color.

From what I can tell, the class selector is automatically considered priority over class selector + tag selector (e.g. ".Hufflepuff" takes priority over ".Hufflepuff a"), how do I overcome that?

What I have tried:

I tried rearranging the order so that the :hover selector was at the top, but it didn't help.
Posted
Updated 27-Jan-17 4:43am

The problem is that you're setting the background colour on the parent element, but modifying the background colour on the anchor element.

When you hover over the anchor, you set its background colour to a 50% opaque version of the parent's background colour. But mixing, for example, 50% opaque red from the anchor with 100% opaque red from the parent just gives you 100% opaque red.

You need to change your classes to modify the background colour of the parent on hover, rather than the background colour of the anchor:
CSS
.Hufflepuff {
    background-color: rgba(255, 255, 0, 1);
    color: Black;
}
.Hufflepuff:hover {
    background-color: rgba(255, 255, 0, 0.5)
}
.Hufflepuff a,
.Hufflepuff a:hover {
    color: Black;
}

Demo[^]
 
Share this answer
 
v3
Comments
KitsunePhoenix 27-Jan-17 12:26pm    
it looks better in terms of formatting the code but hasn't made any difference on the actual output.
Richard Deeming 27-Jan-17 12:29pm    
Sorry, I forgot to take the a out of the second selector. Try the updated version.
KitsunePhoenix 27-Jan-17 12:43pm    
okay that works. It may cause a problem farther down the line but i'll deal with that then.
Check this out: JSFiddle[^]. You have forgotten to add ; to
background-color: rgba(255, 255, 0, 0.5)
 
Share this answer
 
Comments
KitsunePhoenix 27-Jan-17 12:29pm    
thank you for pointing that out, I fixed it. still didn't solve my problem though.
Bryian Tan 27-Jan-17 12:45pm    
try overwrite it by adding !important
background-color: rgba(255, 255, 0, 0.5) !important;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900