Jamie Belcher
Jamie Belcher

Reputation: 123

Changing certain on:hover colours depending on pseudo class

I have a special on-hover attribute that basically underlines whatever I hover over in a pretty looking way.

Right now the colour of the underline has been set to white, I was wondering if there would be a way to change that colour for different items?

My Navigation Bar

I have no idea what to do, I've tried changing it inline but due to it being:

.navbar li:before {
    content: "";
    position: absolute;
    z-index: -1;
    left: 50%;
    right: 50%;
    bottom: 0;
    background: white;
    height: 4px;
    -webkit-transition-property: left, right;
    transition-property: left, right;
    -webkit-transition-duration: 0.3s;
    transition-duration: 0.3s;
    -webkit-transition-timing-function: ease-out;
    transition-timing-function: ease-out;
}

I'm not sure it'll work inline due to this

EDIT ---

I should've mentioned that if you click the "contracts " tab, a new list appears and that I'm trying to change the colours of each of their underlines somehow, silly me!

Upvotes: 0

Views: 57

Answers (3)

Jamie Belcher
Jamie Belcher

Reputation: 123

Big thanks to Georgios for this! You got me on the right track, I created an additional selector so that it picks only the contractor ones!

This is a fiddle of the new one:

I simple had to add the:

.additional

tag in Georgios' answer

.navbar li:nth-child(1):before {
    background-color:red;
}

so it now looks like this:

.navbar .additional li:nth-child(3):before {
    background-color: blue;
}

New Nav Bar

Upvotes: 1

geeksal
geeksal

Reputation: 5016

Try this : nth-child() selector will solve you problem

	$('.navbar ul li:nth-child(2) a').click(function() {
		$(".additional").slideToggle()
	});
body {
    background-color: #fff;
    margin: 0;
    font-family: Arial;
    color: #333;
}

header {
    background-color: #333;
    position: fixed;
    top: 0;
    width: 100%;
}

.navbar {
    display: block;
    text-align: center;
   
}

.navbar ul {
    list-style-type: none;
    padding: 0;
    margin: 0;

}

 .navbar li {
    display: inline-block;
    vertical-align: middle;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    box-shadow: 0 0 1px rgba(0, 0, 0, 0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -moz-osx-font-smoothing: grayscale;
    position: relative;

}
.navbar li:before {
    content: "";
    position: absolute;
    z-index: -1;
    left: 50%;
    right: 50%;
    bottom: 0;
    background: white;
    height: 4px;
    -webkit-transition-property: left, right;
    transition-property: left, right;
    -webkit-transition-duration: 0.3s;
    transition-duration: 0.3s;
    -webkit-transition-timing-function: ease-out;
    transition-timing-function: ease-out;
}
.navbar li:hover:before, navbar li:focus:before, .navbar li:active:before {
    left: 0;
    right: 0;
}

.navbar li a {
    padding: 25px;
    display: block;
    height: 100%;
    color: white;
    text-decoration: none;
}

.title {
    height: 80px;
    padding: 2px;
    background-color: #fff;
}

.container {
    margin-top: 150px;
    padding-top: 50px;
}

.home {
    margin-top: 10px;
    text-align: center;
    padding: 40px !important;
}
.wrap {
    width: 100%;
    margin: 0 auto;
}
.left_col {
    float: left;
    width: 50%;
}
.right_col {
    float: right;
   width: 50%;
}
.right_col img {
    width: 80%;
    margin-top: 50px;
    border: 2px solid black;
    border-radius: 5px;
}
.left_col img {
    width: 80%;
    margin-top: 50px;
    border: 2px solid black;
    border-radius: 5px;
}

.aditional {
    position:absolute;
    top:100%;
    width:100%;
    background:#000;
    display:none;
}
.aditional li {
     color:#fff;
  }
  
  .navbar li:nth-child(2):before {
    background: blue;
    }

 .navbar li:nth-child(3):before {
    background: green;
    }
 .navbar li:nth-child(4):before {
    background: red;
    }
     .navbar li:nth-child(5):before {
    background: yellow;
    }
<header>
    <div class="navbar">
        <ul>
            <li style="float: left"><a href="#home">Home</a></li>
            <li><a href="#contact">Contracts</a></li>
            <li><a href="#about">About Us</a></li>
            <li><a href="#other">Other</a></li>
            <li> <a href="#release">Release Notes</a></li>
            <li> <a target="_blank" href="http://www.phpartnership.com">Pinnacle Health Partnership</a></li>
        </ul>
        <ul class="aditional">
				<li><a href="#green">So this would have a Green underline</a></li>
               <li><a href="#blue">So this would have a Blue underline</a></li>
                <li><a href="#red">So this would have a Red underline</a></li>
                <li><a href="#orange">So this would have a Orange underline</a></li>
               </ul>
    </div>
</header>

Upvotes: 2

Georgios Dimitriadis
Georgios Dimitriadis

Reputation: 685

Use the nth-child(n) selector, like this:

// Override the first element in the menu to be red
.navbar li:nth-child(1):before {
    background-color:red;
}

Upvotes: 5

Related Questions