CSS: drop-down causes page to jump up - html

When user clicks on my drop-down menu, it jumps the page back to the very top (like a page reload).
See this jsFiddle with stripped down code.
I know that if I remove the # in href="#", it should work, but that is not good practice.
How do I make it so it doesn't jump the page to top?
HTML:
<div class="nav">
<ul>
<li>
Drop
<ul class="nav-user nav-li-cont">
<li> Hello
</li>
<li> World
</li>
</ul>
</li>
</ul>
</div>
CSS:
div.nav {
display: inline-block;
margin-left:50px;
}
div.nav ul {
padding: 0;
margin: 0;
list-style-type: none;
position: relative;
}
div.nav ul li {
float:left;
}
div.nav ul li a {
text-decoration: none;
}
div.nav ul li ul {
display:none;
}
div.nav ul li:hover ul {
display: list-item;
position: absolute;
}
div.nav ul li:hover ul li {
float:none;
}
div.nav ul li ul li:hover {
float:none;
}
div.nav ul li ul li a {
text-decoration: none;
cursor: pointer;
float:left;
width:100%;
}
.nav-li-cont {
border-radius: 4px;
float: left !important;
padding: 10px !important;
}

That's because there's no js/jquery assigned to that link. And the browser will assume that this is a "link for a new page", and actually if you check the URL of the website, it's propably changed to this after you click on that button (you can't see this on jsfiddle): example.com/currentPage/#.
If you change this line
Drop
to this instead
Drop
it will not jump to the top anymore. The void operator is often used merely to obtain the undefined primitive value (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).
http://jsfiddle.net/3837z3b0/3/
Update
If you have multiple links using href="#" you can either exchange the links as I mentioned above to this href="javascript:void(0)" or you can add a class called noclick for example for every link that has href="#" and add the following:
$(".noClick").attr('href','javascript:void(0)');
http://jsfiddle.net/3837z3b0/4/

Related

Why isnt my entire dropdown list displaying?

I'm trying to figure out with my drop-down list within my nav is not displaying. I
am also trying to understand how to i would render the drop-down list as a class and how it would be specified in the CSS to not get it confused with any of my of unordered lists. Can someone please help and possibly add a class to the dropdown list so i know how to display it?
Here is my code in Jfiddle:
https://jsfiddle.net/CheckLife/rzxxb2kb/4/
In your css you have:
/*Dropdown Nav */
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
position: absolute;
}
The problem here is that you're setting each individual "li" element to display none, so you're hiding each individual list item. If you show/hide the whole unordered list, then your elements will appear. Additionally, you probably want to remove position:absolute so that they stack vertically
/*Dropdown Nav */
ul li ul {
display: none;
}
ul li:hover ul {
display: block;
}
EDIT:
In order to address the issue of the list pushing all content down, I recommend not using an ul. Instead you could put each a tag in a div and do the following:
HTML:
<li onmouseover="newText()">Players
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
CSS:
.dropdown-content {
display: none;
position: absolute;
// The below was copied from your other css
background-color: #3b63d3;
width: 90px;
text-align: center;
border-right: 1px groove #141e38;
}
li:hover .dropdown-content {
display: block;
}
https://jsfiddle.net/rzxxb2kb/5/
Its the position: absolute; on ul li:hover ul li change it to position: relative;
/*Dropdown Nav */
ul li ul li{
display: none;
}
ul li:hover ul li {
display: block;
position: relative;
}
.clear {
clear: both;
}
W3Schools shows how to create Nav bars with drop down functionality
https://www.w3schools.com/css/css_dropdowns.asp

Navigation bar with drop down not working

I'm doin' a navigation bar for a website. I created it etc. but when I go to one of the sub menu's it disappears..
here's my HTML:
<ul id="menu">
<li>Welcome</li>
<li>Review
<ul>
<li>Customer Reviews</li>
<li>Leave a Review</li>
</ul>
</li>
<li>Gallery</li>
<li>Discounts
<ul>
<li>Refer us!</li>
<li>Claim discount</li>
</ul>
</li>
<li>Send me an email!
</li>
</ul>
</nav>
and my CSS:
/* nav */
nav{
text-align:center;
}
nav a:visited{
color:black;
}
nav a{
text-decoration:none;
color:black;
}
#menu {
margin:0 auto;
display: inline-block;
list-style-type:none;
padding:0;
}
#menu li {
float: left;
position: relative;
list-style-type: none;
background:white;
border:1px solid black;
margin-left:10px;
margin-top:5px;
border-radius:4px;
}
#menu li a {
font-family:helvetica;
display:block;
padding:10px 10px;
text-decoration:none;
}
#menu li a:hover {
color:orange;
}
#menu li ul {
background: none repeat scroll 0 0 #ffffff;
margin-top:6px;
margin-right:1px;
padding: 2px;
}
/*#menu, #menu ul {
margin:0 auto;
padding: 0;
}*/
#menu li {
float: left;
position: relative;
list-style-type: none;
}
#menu > li:hover > ul {
display: block;
}
#menu > li > ul {
display: none;
position: absolute;
}
#menu li a {
white-space: nowrap;
}
and a little JSFiddle for ya: http://jsfiddle.net/nv741s01/
If you hover your mouse over a menu option [that has a sub-menu] long enough and then do it, it works, but people won't be willing to wait three seconds every time they want to visit a sub menu, so how do I resolve it so that it works as soon as you go to it?
any help would be much appreciated, thanks in advance :)
It was because there was a little gap between the sub menu and the menu, here is the fixed JSFiddle:
http://jsfiddle.net/nv741s01/3/
And here is what I changed:
#menu li ul {
background: none repeat scroll 0 0 #ffffff;
margin-top: 1px;
margin-right:1px;
padding: 2px;
}
I changed the margin-top to 1px.
The margin of an element doesn't capture hover events. Use padding instead. Make these changes:
#menu li {
float: left;
position: relative;
list-style-type: none;
background:white;
padding-left:10px;
padding-top:5px;
margin:0;
}
/* add this rule */
#menu li a {
border:1px solid black;
border-radius:4px;
}
#menu li ul {
background: none repeat scroll 0 0 #ffffff;
margin-top:0px;
margin-right:1px;
padding: 2px;
}
Fiddle: http://jsfiddle.net/nv741s01/2/
You are using margin to position the submenu away from the main item. Since margin isn't part of the actual element it doesn't trigger any hover behaviours. Instead, use padding on the child ul element, since padding is actually considered part of the child's box. This will make the hover behaviours trigger consistently when moving the mouse from parent to child.
You also describe that there's a 3 second delay somewhere - that's impossible from this code, and I cannot reproduce it obviously.
Your dropdowns are disappearing because as you move your mouse cursor down, there's a gap between the parent menu item and the child menu item.
When the mouse leaves the parent li space, it no longer applies to the hover state, and so the CSS rule is ignored, leaving the child menu hidden.
If it helps, I tend to use a combination of margins and padding, to 'bump together' the parent and child menus, to help navigation.

Horizontal menu bar with horizontal sub menu does not working in IE

I use the following HTML program for creating Horizontal menu bar with horizontal sub menu.It was working fine in Fire Fox and Chrome but it doesn't work in IE.So What are the changes are need changes in this program?
<html>
<head>
<style>
/* Targeting both first and second level menus */
#nav li {
list-style:none;
float: left;
position: relative;
}
#nav li a {
display: block;
padding: 8px 12px;
text-decoration: none;
}
#nav li a:hover {
background-color:red;
color:#FFF;
opacity:1;
}
/* Targeting the first level menu */
#nav {
top:150px;
min-width:850px;
background:#fff;
opacity:0.5;
display: block;
height: 34px;
z-index: 100;
position: absolute;
}
#nav > li > a {
}
/* Targeting the second level menu */
#nav li ul {
color: #333;
display: none;
position: absolute;
width:850px;
}
#nav li ul li {
display: inline;
}
#nav li ul li a {
background: #fff;
border: none;
line-height: 34px;
margin: 0;
padding: 0 8px 0 10px;
}
#nav li ul li a:hover {
background-color:red;
color:#FFF;
opacity:1;
}
/* Third level menu */
#nav li ul li ul{
top: 0;
}
ul.child {
background-color:#FFF;
}
/* A class of current will be added via jQuery */
#nav li.current > a {
background: #f7f7f7;
float:left;
}
/* CSS fallback */
#nav li:hover > ul.child {
left:0;
top:34px;
display:inline;
position:absolute;
text-align:left;
}
#nav li:hover > ul.grandchild {
display:block;
}
</style>
</head>
<body>
<ul id="nav">
<li>Home</li>
<li>
Products
<ul class="child">
<li>Hard Drives</li>
<li>Monitors</li>
<li>Speakers
<ul class="child">
<li>10 watt</li>
<li>20 watt</li>
<li>30 watt</li>
</ul>
</li>
<li>Random Equipment</li>
</ul>
</li>
<li>
Services
<ul class="child">
<li>Repairs</li>
<li>Installations</li>
<li>Setups</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
</ul>
</body>
</html>
do you use modernizr? or have available classes for legacy IE browsers? You might want to try messing with separate styles and/or stylesheets for IE.
Or
You can switch your navigation from floated blocks to "inline" elements, which older browsers such as IE6 that don't work well with floats will recognize.
Or
If you want to stick with floats then make sure you are setting a "width" for any floated element.
Chris Coyier has a nice article on floats that contains a section labeled: "Problems with Floats"
http://css-tricks.com/all-about-floats/
Well for some reason I can't even get the fiddle site to work at all in IE8...? But one problem I noticed in your CSS, is opacity. IE8 doesn't support the opacity property. To change the opaqueness of elements in old IE, use filter:alpha(opacity=75); Note, the value 75 is a whole number from 1 to 100, not a decimal like with the opacity property. I wish I could be more help, but I can't even see what the problem is in the fiddle...

Why does my css menu have text overlapping on drop down

I can't get my drop down menu (sub-menu) to stop overlapping the text. Below is my HTML + CSS.
<ul class="top-menu">
<li><a href="index.html" >Events </a></li>
<li>
Store
<ul>
<li><a href="index-2.html" >Imagination CD</a></li>
<li><a href="index-2.html" >Total Relax</a></li>
<li><a href="index-2.html" >Super Study</a></li>
</ul>
</li>
</li>
<li>About Us</li>
<li>Contact</li>
</ul>
.top-menu {
position: absolute;
padding: 32px 00 0 10px;
height: 85px;
}
.top-menu li{
padding:0 24px 0 0;
}
.top-menu li, .top-menu li a{
display:block;
float:left;
}
.top-menu ul, .top-menu ul a{
position: absolute;
display: none;
z-index:999px;
line-height: 30px;
top: 12px;
}
.top-menu li a{
padding:0 0 0 30px;
color:#4d3925;
font-size:25px;
line-height:26px;
text-decoration:none;
}
.top-menu a:hover, .top-menu .active {
color:#f29869;
}
.top-menu li:hover ul a, .top-menu li:hover ul {
position: abolute;
display: block;
padding: 32px 0 0 14px;
font-size: 16px;
line-height: 30px;
z-index: 999px;
}
reference menu: http://www.nicoheins.com/lux/
There are a number of issues with this dropdown. However, the one that's causing this particular problem is the attribute position:absolute; in your selector .top-menu ul, .top-menu ul a. Once you get rid of that, your links will no longer stack on top of eachother.
But you'll still need to do more work to get your menu looking fresh.
Not to plug my own work, but I've got a pretty decent dropdown (I think, at least) that you can use hosted over at Github. Check it out here. If you don't want the fade or arrows, you can just remove those, then apply your own style. The rest should work as you want it to.
Edits to fix other issues:
Again, the original question just asks to solve one of the problems I noted with this dropdown. Another is the fact that your submenu ul is pushing the parents lis to the right. You can fix this by applying this styling:
.top-menu > li {
position: relative;
}
.top-menu li ul {
position: absolute;
}
Another is the fact that your all of your lis are styled to float:left;. You only want the top level lis to be styled in this way. This is cased by this section:
.top-menu li, .top-menu li a{
display:block;
float:left;
}
Changing this to
.top-menu > li, .top-menu > li > a{
display:block;
float:left;
}
will fix that, but then you'll need to re-apply the display:block; to the submenu as.
I imagine your next concern would be regarding the huge spacing between the submenu as. This is because you're targeting them with this code:
.top-menu li:hover ul a, .top-menu li:hover ul {
position: abolute;
display: block;
padding: 32px 0 0 14px;
font-size: 16px;
z-index: 999px;
}
That padding-top is quite large! Be sure your selectors are only selecting what you want, or you'll end up getting strange behavior like this. This is particular important when you're working with something like a dropdown, which has lis inside of lis, and they need to be styled completely different from one another.
For a good overview of selectors, check out this page.
A functional version of this is here. Please note that I tried to change the original css as little as possible. I think this has restricted my ability to write the best possible dropdown that I think I can write. But this does work!
Here's a JSFiddle with rewritten code that works completely and cleanly!.

Rollover effect with non-equal images

In my website, I am trying to get the rollover effects working.
Currently, on no mouse hover, the ul li item is displayed as text but on mouse hover, it has a rollover effect to show the image.
Instead of having text in the normal mouse non-hover state, I want to have images.
That means, mouse hover and non-mouse hover are both different images, and there's no text
I wanted to ask how do I get such a rollover effect working, in contrast to what I have currently. (non-mouse hover is text which I want to to change to images as well)
Here is the jsfiddle of how I currently have rollovers: http://jsfiddle.net/PF35v/7/
You have all of the images hidden by default so when you put an image inside the a tag, it is also hidden.
ul#nav li a img { display: block; }
This will make the images in links always visible but the others hidden by default. I think that's what you're asking for.
Here's two different approaches, I'm sure there are others:
HTML-Centric
<ul id="nav">
<li>
<a href="#">
<span>My Text</span>
<img src="http://goo.gl/tYsDU"/>
<img class="hover" src="http://goo.gl/UohAz"/>
</a>
</li>
...
</ul>
#nav,
#nav li {
list-style-type: none;
margin: 0;
padding: 0;
}
#nav li a img {
display: inline;
}
#nav li a img.hover,
#nav li a span {
display: none;
}
#nav li a:hover img {
display: none;
}
#nav li a:hover img.hover {
display: inline;
}
http://jsfiddle.net/RdRcj/1
CSS-Centric
<ul id="nav">
<li>
</li>
...
</ul>
#nav,
#nav li {
list-style-type: none;
margin: 0;
padding: 0;
}
#nav li {
width: 128px;
height: 128px;
background-image: url(http://goo.gl/tYsDU);
}
#nav li a {
display: block;
width: 128px;
height: 128px;
padding: 0;
}
#nav li a:hover {
background-image: url(http://goo.gl/UohAz);
}
http://jsfiddle.net/RdRcj/
The first is probably the "best" from a flexibility standpoint; you don't have to hard-bake the dimensions in like you do the second. However, if they're unchanging, perhaps the second is preferable for your approach, it just takes targeting each li and a specifically, which can prove a little brittle.