I'm basically trying to add an animation to my navigation so when a user hovers over the links the text color fades in blue, and then fades back out to black after. I've read up on the transition property and watched a few tutorials on Youtube but I can't get it to work when I apply it to my own navigation bar.
Below is a link to my Codepen, if anybody could shed some light on the problem I'd be really appreciative..
Thanks
HTML:
<!-- header starts here -->
<header>
<nav>
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
<li>Service</li>
</ul>
</nav>
</header>
http://codepen.io/Clarkpen/pen/razMWB
Try here:
http://codepen.io/anon/pen/myMrRq
I included a transition on the anchor:
nav ul li a {
transition:all 400ms ease-in;
}
Note it is good practice to add the vendor prefixes:
nav ul li a {
-webkit-transition: all 400ms ease-in-out;
-moz-transition: all 1400ms ease-in-out;
-o-transition: all 400s ease-in-out;
transition: all 400ms ease-in-out;
}
EDIT
I have edited your codepen here to only add the transition to the navigation links:
http://codepen.io/anon/pen/RNZGpL
nav ul{margin:0px;padding:0px;}
nav ul li{float:left;list-style:none;}
nav ul li a{text-decoration:none; padding:15px; color:#666; text-transform:uppercase;transition:ease-in 0.5s;-webkit-transition:ease-in 0.5s;-ms-transition:ease-in 0.5s;-moz-transition:ease-in 0.5s;}
nav ul li a:hover{background:#F00; color:#FFF;}
<nav>
<ul>
<li>Home</li>
<li>About US</li>
<li>Products</li>
<li>Feedback</li>
<li>Contact</li>
</ul>
</nav>
Demo
Related
I'm trying to make a color transition but I don't know how to make the effect that the color will be added gradually without increase size.
a:hover{
background-color:#c0392b;
transition: 1000ms linear;
padding:20px;
<nav class="navigation-in-center">
<ul class="container menu group">
<li>A PROPOS</li>
<li>SERVICES</li>
<li>IDEES</li>
<li>CHEQUES CADEUX</li>
<li>CONTACT</li>
</ul>
</nav>
Your size is increasing due to padding.
Remove the padding and apply transition to the a element, not in it's hover state:
a {
transition: all 1000ms linear;
}
a:hover {
background-color: #c0392b;
}
<nav class="navigation-in-center">
<ul class="container menu group">
<li>A PROPOS</li>
<li>SERVICES</li>
<li>IDEES</li>
<li>CHEQUES CADEUX</li>
<li>CONTACT</li>
</ul>
</nav>
You should add a transition for "background-color" only.
transition: background-color 1000ms linear;
a{
display: inline-block;
padding:20px;
transition: background-color 1000ms linear;
}
a:hover{
background-color:#c0392b;
}
<nav class="navigation-in-center">
<ul class="container menu group">
<li>A PROPOS</li>
<li>SERVICES</li>
<li>IDEES</li>
<li>CHEQUES CADEUX</li>
<li>CONTACT</li>
</ul>
</nav>
I was trying to do some fading effects with Javascript but it was giving me some issues. I've managed to get my drop down menu working entirely with CSS, but I can't figure out for the life of me how to get the menu to fade in / out when hovering over it's parent for some reason.
jsfiddle
HTML
<body>
<div id="header1">
<table width="100%" height="60px" border="0">
<tr>
<td align="center"><ul id="horiznav">
<li><a href="#" class='class2'>Home</a></li>
<li><a href="#" class='class2'>Statistics</a></li>
<li><a href="#" target="_blank" class='class2'>Overview</a></li>
<li><a href="#" class='class2'>Lists</a>
<ul>
<li><a href="#" class='class2'>All Active Projects</a></li>
<li><a href="#" class='class2'>By Phase</a></li>
<li><a href="#" class='class2'>By User</a></li>
<li><a href="#" class='class2'>Completed Projects</a></li>
</ul>
</li>
<li><a href="#" class='class2'>New Project</a></li>
<li><a href="#" class='class2'>Administration</a></li>
</ul></td></tr>
</table>
</div>
</body>
CSS
#header1{
background: #0d2965;
background-color: #0d2965;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
font-weight:bold;
color:#FFF;
}
ul#horiznav, #horiznav ul{/*remove the bullets from the dropdown ul as well*/
margin:0;
padding:0;
list-style-type:none;
height:32px;
text-align:center
}
#horiznav li{
display: inline-block;
/*float: left;*//*float the li element so the menu's horizontal...*/
width:150px;/*...and set the width*/
z-index: 1;
position:relative;/*set position:relative as the start point for absolutely positioning the dropdown*/
}
#horiznav li a{
display:block;/*make the link a block element...*/
width:150px;/*...with a fixed width...*/
line-height:30px;/*...and set the line-height to vertically centre the text*/
text-align:center;/*horizontally centre the text...*/
color:white;/*...colour it white...*/
text-decoration:none;/*...and remove the default underline*/
background-color:#EA9531;/*give the link an orange background...*/
border:1px solid white/*...and a white border...*/
}
#horiznav li a:hover{
-webkit-transition:All 0.5s ease;
-moz-transition:All 0.5s ease;
-o-transition:All 0.5s ease;
color:#333333/*change the text colour on :hover*/
}
#horiznav li ul{
display:none;/*hide the dropdown*/
position:absolute;/*position it absolutely..*/
left:0;/*...align the left edge with the left edge of the parent li...*/
top:32px/*...and 32px down from the top - 30px height + 2px for the border*/
}
#horiznav li:hover ul {
display:block/*display the ul when the parent li is hovered*/
}
#horiznav li ul a{
background-color:#FFB33B/*give the dropdown a different background colour*/
}
Does anyone have any ideas or links to point me in the right direction?
Thanks in advance!
Transition cant be used on the display property.
Use visibility and opacity instead.
Use this CSS
#horiznav li ul {
-webkit-transition:visibility 0.5s ease, opacity 0.5s ease;
-moz-transition:visibility 0.5s ease, opacity 0.5s ease;
-o-transition:visibility 0.5s ease, opacity 0.5s ease;
transition:visibility 0.5s ease, opacity 0.5s ease;
visibility:hidden;
opacity:0;
position:absolute;
left:0;
top:32px;
}
#horiznav li:hover ul {
visibility:visible;
opacity:1;
}
Proof in JSFiddle
Works fine for me.
HTML:
<ul class="nav">
<ul>
<li>Home</li>
<li>A
<ul>
<li>X</li>
<li>Y</li>
<li>Z
</ul>
</li>
</ul>
</ul>
CSS:
.nav ul ul {
position:absolute;
visibility: hidden;
opacity:0;
width:170px;
margin:0;
transition:visibility 0s linear 0.3s, opacity 0.3s linear;
-webkit-transition:visibility 0s linear 0.3s, opacity 0.3s linear;
-moz-transition:visibility 0s linear 0.3s, opacity 0.3s linear;
}
.nav ul li:hover > ul {
visibility: visible;
opacity:1;
}
I want the first level to fade in, on hover on a list item of the main menu, but it just doesn't seem to work. I have spent hours on it and I'm not sure what's really wrong. Any pointers?
If you need to see the complete code: http://paperbird.in/projects/BusinessConclave/index.php
Edit: Ok, you contributed your website, so here's the solution, actually the transition does work, but z-index is causing you an issue there, so it flicks the sub menu instantly.. though it transits, use z-index: 100; for .nav ul li:hover > ul on line 153 in style.css and make sure you remove visibility properties as they are not required.
First of all, your markup is invalid, you cannot nest ul as a direct descendant to ul so nest that in an li and secondly, you cannot transit visibility property, so only use opacity and get rid of the visibility property as well. If you want, you can also use animation-timing-function property with a value of linear for a consistent fadein and fadeout effect.
Demo
.nav ul ul{
position:absolute;
opacity:0;
width:170px;
margin:0;
-webkit-transition:opacity 0.3s linear;
-moz-transition:opacity 0.3s linear;
transition:opacity 0.3s linear;
}
.nav ul li:hover > ul{
opacity:1;
}
And make sure you declare properietary properties before general properties.
This question already has answers here:
Transitions on the CSS display property
(37 answers)
Closed 8 years ago.
I can't get transition to work on a website i'm working on. The nav menu hides and shows correctly, but it just appears instantly without tranistion. The CSS is this:
.nav ul li ul li {
display:none;
visibility:hidden;
transition: all 0.5s ease;
}
.nav ul li:hover > ul
{
transition: all 0.5s ease 0s;
display:block;
visibility:visible;
}
and the HTML is
<div class="nav">
<ul>
<li>test</li>
<li>test
<ul class="sub-menu">
<li>test</li>
</ul>
</li>
</ul>
</div>
I have tested it in chrome, ffx, ie.
I actually got most of this code off another answer on this site, so i'm not sure what my problem is here.
I should have mentioned, I have tried opacity from other answers, but in a drop down menu, it won't work as the menu stays there.
The transition from display: none to display: block does not behave like you expect.
Work with opacity instead.
jsFiddle Demo
Try This CSS this will work fine
.nav ul li > ul {
opacity:0;
overflow:hidden;
transition: all 0.5s ease;
}
.nav ul li:hover > ul
{
transition: all 0.5s ease 0s;
height:auto;
opacity:1;
}
Demo Here
display property does not work with transitions.
what itay suggested is valid,and you can play with other properties as well (position ?)
to make the desired effect.
I'm trying to understand the simplest background transition possible using only HTML5 and CSS3. Searching through stackoverflow I've learned it can be easily implemented using external libraries such as jQuery but for this project I've decided not relying on any of those.
Markup
<nav>
<ul>
<li><a id="foobar" href="http://www.google.com/search?q=foobar">Foobar</a></li>
</ul>
</nav>
Styles
body {
background: url('background-default.png'), no-repeat;
}
#foobar a:hover {
background: url('background-hover.png'), no-repeat;
-webkit-transition: // TODO;
-moz-transition: // TODO;
-o-transition: // TODO;
-ms-transition: // TODO;
transition: // TODO;
}
As I mentioned in my comment, you can't transition the background-image property but you can get the sort of effect you're looking for if you're willing to add extra markup and then transition the opacity. So you'll have some markup like this:
<nav>
<ul>
<li>
<img src="no-icon.png">
<img src="yes-icon.png">
<a id="foobar" href="http://www.google.com/search?q=foobar">Foobar</a>
</li>
</ul>
</nav>
Then set the transition on the images, absolute position them (so they'll be like backgrounds), and hide one of them by default (I've left out the vendor extensions for clarity):
nav li img {
position: absolute;
transition-duration: 1.5s;
opacity: 1;
}
nav li img:first-child {
opacity: 0;
}
Then swap the opacity values on li:hover:
nav li:hover img {
opacity: 0;
}
nav li:hover img:first-child {
opacity: 1;
}
Here's a full working example. Not an ideal solution because you have to add extra markup, but it'll work.
Here's an example of the code I use to achieve this. The images are sprites which each contain normal and hover state. The trick is to add the img to both li and a, and to use opacity to change the appearance of the image. You can then use css3 transitions to make this appear smoother.
<ul id="homenav">
<li class="h"><a href="#><span>Home</span></a></li>
<li class="i"><span>Inloggen</span></li>
<li class="v"><span>Voorbeelden</span></li>
</ul>
#homenav li.h, #homenav li.h a {background-image: url('img/btn_home.gif');}
#homenav li.i, #homenav li.i a {background-image: url('img/btn_inloggen.gif');}
#homenav li.v, #homenav li.v a {background-image: url('img/btn_voorbeelden.jpg');}
#homenav li {background-position: 0 170px;}
#homenav li a {background-position: 0 0;}
#homenav li a:hover
{opacity: 0;
-webkit-transition: opacity .8s ease-in;
-moz-transition: opacity .8s ease-in;
-o-transition: opacity .8s ease-in;
transition: opacity .8s ease-in;}
#homenav a {display: block; height: 100%;}
#homenav a span {display: none;}