Navbar fade out issue - html

This is really a matter of me messing around in the CSS, and ending up unknowingly deleting or overriding something. When I hover over the nav link which drops down a list, it fades in. However, it doesn't fade out when it disappears after moving the mouse away.
Link to fiddle
<nav class="bg">
<ul class="width nav">
<li><span></span></li>
<li>Link</li>
<li><span></span></li>
<li>DropLink
<ul class="drop dr2">
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</li>
<li><span></span></li>
</ul>
</nav>
CSS (sorry for the mess on this one. In the full CSS file, it's all combined to save on space):
*{margin:0;padding:0;font-size:small;font-family:Roboto;vertical-align:middle;}
.drop,nav{border:1px solid #BBB;}
body>.main,nav>ul{font-size:0;}
.width{margin:0 auto;width:84%;}
.width,.bg{min-width:1000px;}
.bg,.drop{background:linear-gradient(#444,#000,#444);}
nav{border-left:none;border-right:none;}
nav>ul:after{content:"";}
nav a{padding:8px;}
nav a:hover, nav a:active{color:#BBB;}
.drop{z-index:2;left:-9999px;top:-9999px;opacity:0;border-top:none;}
.dr1{transform:translateX(-28.5%);width:230%;}
.dr2{transform:translateX(-27%);width:216%;}
.drop li{white-space:nowrap;display:block;}
.nav li:hover .drop{left:0;top:100%;opacity:1;transition:opacity .5s,top 0s,left 0s;}
nav span{border-left:1px solid #000;border-right:1px solid #555;padding:7px 0;}
nav{font-weight:bold;}
nav>ul{text-align:justify;}
nav>ul,nav>ul:after{width:100%;}
a{text-decoration:none;}
a{color:#FFF;}
.nav li:hover .drop{text-align:center;}
nav a,nav li,nav>ul:after{display:inline-block;}
nav li,nav>ul{position:relative;}
.drop{position:absolute;}
While I'm asking, I might as well kill two birds with one stone and ask something else. If you zoom in on the bar where the borders are, you'll see that they don't actually have the same height as the navbar itself. If I increase the padding in the relevant area in my CSS, it becomes one pixel too long. If I don't, it's one pixel too short. It is never 100% the height of the bar. I asked this question not too recently, but after changing the entire navbar code, I've been unable to replicate the solution.

Your problem is that you are only setting the left and top rules on hover, but when you aren't hovering the dropdown completely leaves the view, so although the opacity if fading, you can't see it.
What you need to do is change two group selectors to have the same position whether hovering or not.
E.G.
.nav li:hover .drop {
left:0;
top:100%;
opacity:1;
transition:opacity .5s, top 0s, left 0s;
}
Should become two selectors:
.nav li .drop {
left: 0;
top: 100%;
pointer-events: none;
opacity: 0;
transition: opacity .5s, top 0s, left 0s;
}
.nav li:hover .drop {
pointer-events: auto;
opacity: 1;
}
In addition, your text-align property should be set even when not hovering, otherwise the text will shift left.
E.G.
Delete:
.nav li:hover .drop {
text-align:center;
}
Add:
.nav li .drop {
text-align:center;
}
If you make these corrections, you should get: jsfiddle.net/4PLbd/28
Note:
I don't want to be nitpicky, but your code is styled terrible and makes fixing things difficult, always try to make code readable. I did not clean up a lot of code here, although it should be.

You should probably consider re coding this, half of your CSS was unecessary and also had to be cleaned up. I also took out code that was not needed, I can show you how to do it better, for instance the spaces with span elements, not necessary. Also, good code structure is important, as it will help you find and locate problems. I cleaned this up as best as I could. Again, this gets your first question done.
Here is just a few CSS changes that I made. Check the Jsfiddle
.drop {
opacity: 0;
transition: all .5s ease;
}
ul.nav li:hover .drop {
opacity: 1;
transition: all .5s ease;
}

Related

Creating CSS3 dropdown menus on a horizontal navbar with a fade-in effect

I've been trying to get this right for a couple of days now, and so far, I haven't really made progress on getting the dropdown working at all, though partly because the guides I've read all make use of symbols in the CSS that I'm unfamiliar with, such as the tilde and 'greather than' symbols. In any case, here is the base code that I have:
<nav class="bg">
<ul class="width">
<li>Link</li>
<li>
<ul>
<li>Link</li>
<li>Link</li>
</ul>
</li>
</ul>
</nav>
CSS:
.width{margin:0 auto;min-width:1000px;width:84%;}
nav ul{width:100%;text-align:justify;font-size:0;position:relative;}
nav ul:after{content:"";width:100%;display:inline-block;}
nav li{list-style:none;display:inline-block;}
nav a{display:inline-block;padding:10px;}
I think the next step is to put display:none; on the inner <ul> elements to hide them by default, but the next part is where I get lost. How do you use CSS to make one object do something when something else happens to another object? In this case, how would you use the CSS to make the dropdown list appear when the relevant link is hovered?
Here is a Demo
First of all you need to understand how CSS Selectors are working.
For the way to add a Dropdown, you go for the hover on the parent element and wrap that around the sub-menu. Than if the hover event is fired everything inside will be able to select true :hover
Like:
.nav li:hover .sub-menu {
left: 0;
top: 100%;
}
I also added the Fade-In effect. There for you work with css-transition.
But be careful, if your Sub-menu will be on/off with display:block and :none it wont work i guess.
U may play around with that in that fiddle to fiddle out what you can do and how your changes effect the output.
.nav .sub-menu {
position: absolute;
left: -1000px;
top: -1000px;
opacity: 0;
-o-transition: opacity .25s;
-ms-transition: opacity .25s;
-moz-transition: opacity .25s;
-webkit-transition: opacity .25s;
transition: opacity .5s;
}
.nav li:hover .sub-menu {
left: 0;
top: 100%;
opacity: 1;
}
You can even create a dropdown without using any selectors. But it is important to know about selectors. Go through this link to know more about selectors.
You can have a look at Pure CSS Dropdown Menu to check how to create dropdown menu with css.
You have yourself correctly pointed out the problem and that is :
How do you use CSS to make one object do something when something else
happens to another object?
I think this simplest demo, would shed some light on your above question.
Specifically have a deeper look at below CSS in the demo.
#circle:hover .popup{
display : block;
}
How do you use CSS to make one object do something when something else happens to another object? In this case, how would you use the CSS to make the dropdown list appear when the relevant link is hovered?
li > a ul { display: none; }
li > a:hover ul { display: block; }
You can also play with transitions to add fade-in effect.

using :hover with css to make a div fade in or out

I've got two divs, div 1, and underneath it is hidden div 2. When I hover over div 1, I want it to hide, and show div 2. Then, once I mouse off of the area (now div 2), div 1 is displayed again.
Here is the code:
<a href="javascript://" class="hoverable">
<div class="normal" style="background:#666;">Hover over me!</div>
<div class="hover" style="background:#888;">Now you see me!</div>
</a>
and here is the css:
<style>
.hoverable {
cursor:default;
color:#000;
text-decoration:none;
}
.hoverable .hover {
display:none;
}
.hoverable:hover .normal {
display:none;
}
.hoverable:hover .hover {
display:block;
}
</style>
My only problem with this is that is is very quick, cut and dry, and not very "fancy". I'd like to add something simple like a fade effect.
I've gotten this working, without the fade effect, here:
http://jsfiddle.net/pBDGW/
If anyone knows how to make those two divs transition with a fade-out, please let me know!
You can use CSS transition with opacity like this:
.hoverable {
cursor:default;
color:#000;
text-decoration:none;
position: relative;
display: block;
}
.hoverable .hover {
opacity:0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.hoverable .hover,
.hoverable .normal{
transition: opacity .5s;
-o-transition: opacity .5s;
-ms-transition: opacity .5s;
-moz-transition: opacity .5s;
-webkit-transition: opacity .5s;
}
.hoverable:hover .normal {
opacity:0;
}
.hoverable:hover .hover {
opacity:1;
}
You can see the jsfiddle here: http://jsfiddle.net/pBDGW/12/
Some explanation:
The transition applied to both div are the main code that make them fade in & out. You can read more about it here: http://css3.bradshawenterprises.com/transitions/
Since you want the first div to fade out, and the second div to fade in, there will be a moment when both div have to be visible partially, hence position: absolute and some positioning on the second div (to make it overlap with the first div).
You are wrapping an anchor (<a>) around both div, which is actually not encouraged, so I have to give it display: block; . A better approach (HTML-wise) is to wrap both div inside another div (still use the same class hoverable), and use 2 different anchors inside each div.
EDIT: this approach http://jsfiddle.net/pBDGW/14/ works too. Here you only fade out the first div, while the second div is always visible but is hidden under the first div when not hovering. It is shorter css, but I don't recommend this approach though because I sometimes have issues with getting the first div to go on top on different browsers.
You can use jQuery, it has functions fadeIn and fadeOut and also its easy to hide() and show() on events mouseOver and mouseLeave.
You can see fiddle here.
$(document).ready(function(){$(".hover").hide();
$(".normal").mouseover(function(){
$(".normal").fadeOut(0);
$(".hover").fadeIn(1000);
});
$(".hover").mouseout(function(){
$(".normal").fadeIn(1000);
$(".hover").fadeOut(0);
});});
You can use transitions:
ADD THIS TO YOUR :HOVER
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
This adds the effect of fade IN/OUT
Addition: this will only work if you have property set for :hover, for example if you want to change the color, or background or what ever..

CSS3 list transition: position of bullets

I have created a menu with li elements. When li:hover, i would like to approximate list-style bullets to the text, and there would be a color changing too. (Both of them with transition, so background-image is unfit!)
I have already tried so many different ways, with relative positioning, and different margin settings, but none of them works properly. Is there any solution?
(BTW, sorry for my poor english!)
Give this a whirl:
ul {
list-style:none;
margin:0;
padding:0;
}
ul li:before {
content: "\2022";
opacity:0;
padding:0 5px 0 10px;
margin:0;
transition:opacity 1s;
}
ul li:hover:before {
opacity:1;
}
As suggested by #FK32 - we can use the :before pseudo-class to simulate a bullet point, by using the unicode character \2022\. We then initially set it's opacity to 0 and when the user hovers on the list item we change the opacity to 1, by apply a transition:opacity 1s so that we fade it in and out.
I removed any margin or padding that a user agent / custom stylesheet may have applied, and then adding padding to the pseudo so that you can more accurately space your list item's content from your bullet.

Setting Other Child's Property On Hover Under Same Parent With Pure CSS (HTML List)

I'm trying to make a CSS menu with submenus. When I move the mouse over a child element of list, I want the submenu to appear inside that child if there is any.
As it is avaliable to see above, sub menu appears but I don't want that to happen when I move the cursor over the areas that I've marked with circles. These are padding areas.
Here's my markup:
<ul>
<li>Home</li>
<li>About Us
<ul>
<li>Our Mission</li>
<li>Our Vision</li>
</ul>
</li>
</ul>
And this is the CSS:
a { color: #000; text-decoration: none; }
a:hover { color: #A0A0A0; }
ul > li { float: left; padding: 0px 30px; list-style-type: none;
margin: 0px 5px; background-color: #CACACA; }
ul > li:hover > ul { display: inline; }
ul > li > ul { position: fixed; display: none; }
Now there is no problem with this code. As you can see in this fiddle, it works as I wanted, the submenu appears when I move the mouse over the "About Us" menu.
However, my problem here is that there are blank spaces(padding) inside menu items and the submenu appears even if I hover over these spaces. But I want the submenu to appear only when I hover the <a> element inside the <li>.
To accomplish this, I tried that code:
ul > li > a:hover > ul { display: inline; }
instead of the existing:
ul > li:hover > ul { display: inline; }
but it didn't work, I wasn't expecting since the <a> and <ul> are the children of the very same <li> element.
I'm creating a template for a CMS, so I don't want to get involved with jQuery implementations too much, like setting ids or classes for list elements. I want to make an implementation with pure CSS.
And I will be using seperating images between list elements, so I must use padding instead of margin.
If I was using margin, it would be easy since the width of the list element would be same with the <a> element's width.
But here, after all these exceptions, I need some help. Any ideas are highly appreciated.
Thanks in advance.
This is possible, but only-just, and with some fairly severe caveats; first the CSS to hide/show the sub-menu:
ul > li > ul {
position: fixed;
opacity: 0;
-moz-transition: opacity 1s linear;
-ms-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
}
ul > li a:hover + ul,
ul > li:hover a + ul:hover{
opacity: 1;
-moz-transition: opacity 1s linear;
-ms-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
transition: opacity 1s linear;
}
JS Fiddle demo.
Now, the caveats:
The display property won't animate (it can't, there's no interim states between none and inline-block (or between any other valid values for the property), so the sub-menu always has to be 'visible'/'present' on the page (albeit with an opacity of 0 hiding it in most browsers).
The transitions are necessary, because that's the only way that the cursor would have time to move from the a link to the ul without it immediately hiding; the transition is, basically, a time-delay to allow the second of the selectors, ul > li:hover a + ul:hover to match.
cross-browser compatibility is likely to be a problem, since transitions aren't supported in Internet Explorer < 10, mind you nor is opacity until version 9.
The major problem, though, is selector specificity. It's going to be rather difficult to write the selectors for arbitrary-depth menus using this technique. So, while it's possible, I certainly wouldn't recommend using this approach, as it seems far too prone to failure.
Perhaps an easier way to do this would be to use the CSS peer selector +. Because the sub-menu <ul> is not a direct child of the <a> tag, you can't use >. Instead use the peer selector + to choose all subsequent peers of a particular type.
In other words: ul li a:hover + ul { display: inline; }
And it has good browser support.

How do I remove the space between links?

<nav id="main_nav">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Forum</li>
<li>Contact Us</li>
</ul>
</nav>
#main_nav{
background:green;
}
#main_nav li{
display:inline-block;
list-style:none;
padding:10px;
font-weight:bold;
}
#main_nav li a{
text-decoration: none;
color:white;
padding:10px;
}
#main_nav li{
-webkit-transition: opacity .5s, background .5s, color .5s;
-moz-transition: opacity .5s, background .5s, color .5s;
-o-transition: opacity .5s, background .5s, color .5s;
}
#main_nav li:hover{
color:red;
background:rgba(0, 0, 200,.5);
}
This is my navigation bar, but I have a problem. Between each link is a small space. When I hover over them quickly, it doesn't look nice. How do I remove it?
A few ways:
Remove the white space between the list items ( jsFiddle example)
Use comments (<!-- -->) to occupy the white space gap (jsFiddle example)
Close the list item tags on the next line (jsFiddle example)
Note that there's also a fourth option where you can set the font-size to zero on the #main_nav element, and then set the font-size on the anchors to something larger, but there have been issues with some earlier builds of Android with this technique. jsFiddle example.
it happens when you use display:inline-block; for list element.
you should use float:left or play with margins/paddings
Arrange all the list in single line will remove the space...
<li>Home</li><li>About Us</li><li>Forum</li><li>Contact Us</li>
Updated fiddle : http://jsfiddle.net/RYh7U/78/
See this fiddle: http://jsfiddle.net/zteA7/
It is because of inline-block creating space.
Make the <li> element floated and use the CSS :after selector to insert a clearer block element to make the nav block the height that of the menu. (no html required).
You also don't need to specify the container heights which should mean line-breaks will work
#main_nav li{
display:inline-block; /* the new web designer disease ?? */
list-style:none;
padding:10px;
font-weight:bold;
margin:0;
}
Just rewrite your html this way
<ul>
<li>Home</li><li>About Us</li><li>Forum</li><li>Contact Us</li>
</ul>
That's because of the property display: inline-block;, which causes the li "blocks" to still behave as inline elements, and therefore build spaces between them. I've had this problem a couple of times and I know two (not so beatiful) solutions:
Make the li elements float: left, but be careful to not mess up the rest of your design.
Write space-filling html comments between the li elements, so no "space" is recognized between them by the browser.
Remove any padding from your <ul> tag. It might be useful for you to use a reset.css to avoid problems like this http://meyerweb.com/eric/tools/css/reset/