I am trying to make a left nav menu for my web page, which is to look something like the following image:
Here is the jsfiddle of the problem I am facing. http://jsfiddle.net/rzr4Z/
My html looks like the following
<ul id="nav">
<li>First<img src="images/navbar-icons/first.png" />
</li>
<li>Second<img src="images/navbar-icons/second.png" />
</li>
<li>Third<img src="images/navbar-icons/third.png" /> <img
src="images/navbar-icons/third.png" />
<ul id="side-menu">
<li><img src="images/navbar-icons/submenu-img.png" alt="sub menu image" />
</li>
<li>Sub-menu item 1</li>
<li>Sub-menu item 2</li>
<li>Sub-menu item 1</li>
</ul></li>
<li>Fourth<img src="images/navbar-icons/fourth.png" />
</li>
</ul>
CSS:
ul#nav {
list-style-type: none;
margin: 10px 0;
padding: 0;
text-align: center;
}
ul#nav li {
font-size: 250%;
line-height: 25px;
padding: 7px 0;
margin: 0;
}
ul#nav li a {
display: block;
}
ul#nav li a:link,ul#nav li a:visited {
color: #333333;
text-decoration: none;
}
ul#nav li a:hover,ul#nav li a:active {
color: #a61607;
text-decoration: none;
}
ul#nav li img {
display: none;
}
ul#nav a:hover#first_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav a:hover#second_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav a:hover#third_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav a:hover#fourth_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav li ul#side-menu {
display: none;
position: absolute;
}
ul#nav li:hover ul#side-menu {
font-size: 30%;
list-style-type: none;
line-height: 2px;
color: #a61607;
text-decoration: none;
display: block;
position: absolute;
top: 310px;
left: 250px;
}
ul#nav li:hover ul#side-menu li {
float: none;
}
ul#nav li:hover ul#side-menu li img {
position: absolute;
top: 400px;
left: 0px;
}
Instead of doing something like,
ul#nav a:hover+img {
display: block;
position: relative;
top: -25px;
}
I had to use different ID's for each of the li elements in the css because each of the rollover image has a different size and width so that I could adjust them individually for their correponding rollover positions. Is using different ID's for each of the menu items the right way to get the desired effect?
The main problem however is when I hover the images for each of the menu items, I start getting a fluttering effect, and the hover effect doesn't look easy to the eyes with that kind of fluttering.
The images for the sub-menu (the sub-menu items do appear, but the img doesn't appear) also doesn't appear when hovering over #third-page menu item.
Any suggestions on how to resolve this fluttering, and getting the correct effects for this menu?
Using different IDs is fine in your case. As for the fluttering problem, I was able to eliminate it by changing this part of your CSS:
ul#nav a:hover#first_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav a:hover#second_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav a:hover#third_id+img {
display: block;
position: relative;
top: -25px;
}
ul#nav a:hover#fourth_id+img {
display: block;
position: relative;
top: -25px;
}
to this:
ul#nav li:hover #first_id+img {
display: block;
}
ul#nav li:hover #second_id+img {
display: block;
}
ul#nav li:hover #third_id+img {
display: block;
}
ul#nav li:hover #fourth_id+img {
display: block;
}
What was happening was, when you hover over the third link, the size of the image would push the link so that it was centered within the li. And if your mouse happened to be hovering to the left or right of the link once the image appeared, you weren't hovering over the link anymore, so it would disappear, then immediately reappear because now you are again hovering over the link... etc.
Detecting the :hover over the li itself fixes this so that the image appears if you are hovering over the li itself, regardless of if you are hovering over the link, image, or any space around them.
Hope this helps.
The trick to fixing your issue is two things:
1) Make the LI items fixed-width
2) Make the "hover" effect occur on the LI, not on the A
The reason you are experiencing "flicker" is exactly what's been described above. When you are applying the :hover effect to the A tag, and the tags aren't fixed in size, you wind up in some positions where your mouse causes the :hover, the :hover moves the object that caused it out from under the mouse - and that causes the effect to end, putting the object back under the mouse - which causes it again. Endless cause'n'effect which gives you flicker.
By putting the :hover effect on the LI instead, you cause the :hover to be raised whether the mouse is over the A OR the IMG - so when the IMG tag pops visible and everything shifts size, your mouse is still hovering over the LI that contains both elements - and voila - no flicker.
There is another issue caused by your approach, however, due to the size of the images. Because your images are shunting things downward due to their height (big images) - when you get to the bottom of the image, and roll over the next LI - everything will shift out from under your mouse and cause a similar issue. This, however, will generally not cause flicker per-se.
Please see the following update to your jsFiddle.
As you will see, the "height" issue occurs on the "Third" entry. This, too, is solvable by explicitly setting height / width and using images of an exact size - OR (more effectively) by using background: url(...); CSS rules rather than IMG tags - because they are far easier to clip / resize.
If you need an example of some other CSS-based rollover menus with sub-menus, please take a look at this jsFiddle, which demonstrates a more modular approach to dropdown menus and such. It could be modified to suit your needs pretty easily with the use of background graphics.
This problem happens when we do not specify following css attributes:
min-width, max-width, and
min-height, max-height
And carefully using the following css attributes can help:
#parent_id:hover
{
//use borders to find space required for correct width/height
}
#parent_id>#child_id:hover
{
//use borders to find space required for correct width/height
}
#parent_id:hover, #child_id:hover
{
//use borders to find space required for correct width/height
}
So make sure that you add these.
Remember this always:
Never do css without borders, always make sure that you add borders to almost all nodes,
this helps to clearly understand how much space is being occupied and it is more easy to workout errors in css like this
ul#nav {
list-style-type: none;
margin: 0;
padding: 0;
text-align: center;
position:absolute;
height:300px;
width:200px;
max-height:300px;
max-width:200px;
border:5px solid red;
overflow:hidden;
}
ul#nav li {
border:1px solid green;
font-size: 250%;
line-height: 25px;
max-height:200px;
padding: 7px 0;
margin-top: 10px;
max-height:50px;
}
ul#nav li a {
border:1px solid black;
}
ul#nav li a:link,ul#nav li a:visited {
color: #333333;
text-decoration: none;
}
ul#nav li a:hover,ul#nav li a:active {
max-height:50px;
color: #a61607;
text-decoration: none;
}
ul#nav li img {
display: none;
}
ul#nav a:hover#first_id+img {
display: block;
}
ul#nav a:hover#second_id+img {
display: block;
}
ul#nav a:hover#third_id+img {
display: block;
}
ul#nav a:hover#fourth_id+img {
display: block;
}
ul#nav li ul#side-menu {
display: none;
}
ul#nav li:hover ul#side-menu {
font-size: 30%;
list-style-type: none;
line-height: 2px;
color: #a61607;
text-decoration: none;
display: block;
position: absolute;
top: 310px;
left: 250px;
}
ul#nav li:hover ul#side-menu li {
float: none;
}
ul#nav li:hover ul#side-menu li img {
position: absolute;
width:48px;
height:48px;
top: 400px;
left: 0px;
}
Now it is not fluttering
Your code:
ul#nav {
list-style-type: none;
margin: 0;
padding: 0;
text-align: center;
position:absolute;
max-height:300px;
max-width:200px;
border:5px solid red;
overflow:hidden;
}
My code:
ul#nav {
list-style-type: none;
margin: 0;
padding: 0;
text-align: center;
position:absolute;
height:300px;
width:200px;
max-height:300px;
max-width:200px;
border:5px solid red;
overflow:hidden;
}
Carefully look at my last answer, you have missed significant attributes like height:300px; and width:200px;
Simply specifying max,min-prefixed attributes is not enough, you should add specific height and width settings, only then fluttering disappears.
You can either do:
ul#nav li a{display: block;}
or
ul#nav li{text-align: left;}
so mouseleave doesn't fire on mouseenter
Related
This is a frequently addressed problem in SO, but almost 5 of the threads here didn't offer any help. The dropdown menu items are not staying open unless I hover over the initial item many times. I can't locate the problem in a specific class, any help would be appreciated.
Here is the CSS Code:
nav ul {
list-style: none;
}
nav ul li {
display: block;
float: right;
}
nav ul ul {
display: none;
position: relative;
margin-top: 60px;
}
nav ul li:hover > ul {
display: block;
position: absolute;
margin-left: -30px;
}
nav ul ul li {
width: 170px;
float: none;
display: block;
}
#navigation_bar a {
display: block;
float: right;
padding: 5px;
font-size: 25px;
color: black;
}
#navigation_bar a:hover {
border-radius: 20px;
color: #FFFFFF;
transition: color 0.2s;
}
div nav {
display: block;
}
And here is the corresponding HTML code:
<div id="navigation_bar">
<nav>
<ul>
<li>شركتنا</li>
<li>الخدمات</li>
<li>المركز الإعلامي
<ul>
<li>آخر الأخبار</li>
<li>معرض الصور</li>
</ul>
</li>
<li>التحميلات</li>
<li>اتصل بنا</li>
</ul>
</nav>
</div>
It's because there's a bit of a gap between the parent nav and the child dropdown. When you try to hover on the child dropdown and your cursor passes over this gap, you lose the li:hover state hence the dropdown hides.
nav ul ul {
...
margin-top: 43px;
...
}
https://jsfiddle.net/mq29ac39/1/
Now compare this one with the original margin-top: 63px
https://jsfiddle.net/mq29ac39/2/
I added background a color to give a better visual idea about the gap
I've been using the following HTML and CSS code for a nav menu on the left for the better part of a year without problems - except that not the display: block functionality doesn't work on ul li a. The text gets pushed down a line, with only the before arrows remaining in place, no matter what I do.
On ul li it's no problem, but it would be more practical to have the link itself extend throughout a block.
Anyone an idea as to the solution?
HTML:
<div class="navmenu_left_wrapper">
<nav>
<div class="navmenu_left">
<ul>
<li>Front page</li>
<li>Introduction</li>
</ul>
</div>
</nav>
</div>
CSS:
.navmenu_left_wrapper {
padding-bottom: 1px;
background-color: #DDD;
text-align: left;
overflow: visible;
width: 145px;
text-align: left;
}
.navmenu_left {
margin-bottom: 10px;
margin-left: 0px;
margin-right: 0px;
font-size: 12px;
font-family: 'oswald-regular', 'Times New Roman';
border: 1px dotted #000;
}
.navmenu_left ul {
margin: 0;
padding: 0;
display: block;
}
.navmenu_left ul li:before {
content: "\00BB \0020";
padding-right: 2px;
}
.navmenu_left ul li {
position: relative;
margin: 0;
padding-left: 4px;
list-style: none;
background: #F2F2F2;
text-align: left;
text-decoration: none;
height: 18px;
}
.navmenu_left ul li:hover {
background: #CCCCFF;
}
.navmenu_left ul li a {
color: #000;
width: 135px;
display: block; /* <----- Doesn't work. Text to next line, underneath "before" arrow. -------- */
}
I think you want display: inline-block as others have stated in the comments. The problem is your a is too wide at 135px and is overflowing the container. The whitespace will cause it to wrap by default.
You can either reduce the width of the a or add white-space: nowrap; to the .navmenu_left ul li CSS
white-space property - https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
fiddle - http://jsfiddle.net/msj5wcgw/3/
You can even set it to show ellipsis if it overflows via overflow: ellipsis
If you want the >> to be clickable, you can add this hack to .navmenu_left ul li a:
padding-left: 1rem;
margin-left: -1rem;
Problem solved. It was a combination of two settings:
.navmenu_left ul li a {
color: #000;
width: 128px; /* ">>" symbol is not part of the overall width of the <li> element. */
display: inline-block; /* that explains also why `inline-block` is needed and not just `block`.
}
This is what it should look like:
There is a tab-menu and a dropdown area. This should always have the same position (but different content and the respective tab choosen) as in the picture. Meaning it should always be as wide as the tabs(-menu).
But I can not figure out how:
to get this responsive
how to have the dropdown area stay where it is
how to style the subitems (in the dropdown area)
Here is what I got so far (sorry for the huge css it is not cleaned yet!), the menu starts at line 1559.
http://jsfiddle.net/pxpHw/
How do I do this properly?
THANKS!
code:
// css
nav {
cursor: default;
}
a {
text-decoration: none;
color: #000000;
}
#menu ul {
margin: 0px;
padding: 0px;
left: 0;
list-style-type: none;
background:green;
z-index: 100;
max-width: 60em;
}
#menu li {
float: left;
width: 20%;
text-align: center;
}
#menu li ul {
display: none;
/*display: block;*/
padding-top: 3px;
}
#menu li:hover ul {
display: block;
}
#menu li ul li {
background-color: #2F2D49;
border-bottom: 1px solid #FFFFFF;
width: 100%;
max-width: 60em;
min-height: 30em;
position: absolute;
}
#menu li ul li a {
color: #FFFFFF;
}
#menu li ul li:hover {
background-color: #232323;
}
Use media queries to get the responsive design
Check the following links
http://mobile.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/
http://mediaqueri.es/
Thanks
AB
This is what I was looking for: (Columns and Layout tab)
http://codecanyon.net/item/css3-full-responsive-dropdown-menu/full_screen_preview/4528828
I found a guide to make drop-down menus, and it said that when you would stopped hovering on the main menu item, the drop-down menus would stay fixed. However, my menu just disappears, making it impossible to press the items!
As you can see, it's the Music menu bit that has the drop-down (or in this case, "drop-right") menu.
Fiddle here:
http://jsfiddle.net/Gb2aS/
Code here:
HTML:
<!DOCTYPE HTML>
<html>
<head>
<link href="stylesheet.css" rel="stylesheet" type="text/css">
<title>Home</title>
</head>
<body>
<div ID="menubox">
<ul>
<li>Home</li>
<li>Music
<ul>
<li>My music</li>
<li>The Joao Gilberto project</li>
</ul></li>
<li>Github</li>
<li>Pictures</li>
</ul>
</div>
<div ID="circle">
<p ID="title"> A <br> r <br> n <br> s <br> t <br> e <br> i <br> n </p>
</div>
</body>
</HTML>
CSS:
#menubox
{
width: 8%;
height: 30%;
border: 10% solid #C7D93D;
border-radius: 5%;
position: fixed;
margin-top: 12%;
margin-left: 18%;
font-family: Ubuntu, Lucida console, Futura;
list-style: none;
float: left;
}
#menubox ul li a
{
text-align: left;
font-size: 200%;
color: #FFF0A5;
}
#menubox ul li
{
color: #468966;
font-family: Ubuntu, Lucida console, Futura;
float: left;
margin-right: 10px;
position: relative;
}
#menubox ul
{
color: #468966;
font-family: Ubuntu, Lucida console, Futura;
}
#menubox ul ul
{
position: absolute;
left: -9999px;
list-style: none;
}
#menubox ul ul li
{
float: left;
margin-left: 40%;
position: relative;
font-size: 60%;
text-align: left;
}
#menubox ul ul a
{
white-space: nowrap;
}
#menubox ul li:hover a
{
text-decoration: none;
color: #FFB03B;
}
#menubox ul li:hover ul
{
left: 0;
}
#menubox ul li:hover ul a
{
text-decoration: none;
color: #FFB03B;
}
#menubox ul li:hover ul li a:hover
{
color: #FFB03B;
}
div p
{
color: #FFF0A5;
text-align: center;
position: relative;
vertical-align: middle;
display: inline-block;
margin-top: 300px;
line-height: 60px;
}
#circle
{
border-radius: 100%;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
background-color: #B64926;
width: 500px;
height: 500px;
display: block;
position: fixed;
margin-top: 9%;
margin-left: 52%;
text-align: center;
}
#title
{
text-color: #FFF0A5;
font-size: 350%;
display: inline;
text-align: center;
}
body
{
height: 100%;
width: 100%;
background-color: #468966;
font-family: Ubuntu, Lucida console, Futura;
}
.link
{
text-color: #FFF0A5;
text-decoration: none;
text-align: left;
}
The problem is because your sub list is offset, so there is deadspace that the cursor has to pass through from the main menu item, and the submenu. This will fix your issue:
#menubox ul li:hover ul
{
left: 0;
top: 0;
z-index:100;
}
As Daniel Gimenez was explaining above, the reason why the submenu stays visible is because it is a child of the main ul item, and so if you keep your cursor over the submenu the browser counts that as maintaining your cursor over the original menu item as well, and the :hover css persists.
It works pretty well for dropdown/popout menus because even if the child object is physically displayed outside of its parent, it is still "inside" the parent from a code-point-of-view. But if there is any physical gap between the two and the mouse crosses over that gap, the :hover rule is deactivated and the submenu disappears.
Your css was a lot to pour through. So I just boiled it down to the basics. I believe your issue has to do with a gap between your main link and submenu.
Explanation of CSS
* Anchors are block inline-block type's and have the exact width of the parent li and ul.
* Submenus are inside li's. So when li's are hovered over they are visible. The submenu is visible because it is a child of the li.
* Because the anchors are 100% and stretch the li, the abut the submenu, so when moving the mouse over, there is no gap, and so the submenu remains visible.
jsFiddle
#menubox {
position: absolute;
left: 100px;
top: 100px;
}
#menubox ul {
display:inline-block;
padding-left:0;
}
#menubox > ul {
width: 100px;
}
#menubox > ul ul {
position:absolute;
display: none;
width: 200px;
}
#menubox li {
list-style-type:none;
display:block;
}
#menubox li:hover {
background:red;
}
#menubox a {
display:inline-block;
width:100%;
}
#menubox ul li:hover ul {
display: inline-block;
background: orange;
}
I've added some padding to the list that pops up, essentially creating a block around it. While your mouse is on that block, it won't dissapear.
http://jsfiddle.net/Gb2aS/5/
#menubox ul ul
{
position: absolute;
left: -9999px;
padding: 100px;
list-style: none;
}
there is however the issue of the drawn circle being placed overtop of the list, but I'll leave that to you.
I DO however like Daniel's solution better. Giving the links their own class is a much better way of dealing with it. You're better off to look at his solution and adapt it to what you want.
i have this menu:
<div class="nav">
<ul>
<li>HOME</li>
<li>AMORTECIMENTO</li>
</ul>
</div>
the normal apearance:
and this is the apearance when users hover the menu:
So, i dont know how to setup the css, assuming i have biggest names on menu like: "AMORTECIMENTO"
Any tips??
i have tried this, but on small names, the menu is cutting the background...
.nav ul li a{
display:block;
font-size:15px;
color:#000;
padding:5px 7px;
background:transparent;
text-decoration:none;
}
.nav ul li:hover{
background:url(../imagens/bola_fundo_menu.png) center no-repeat;
}
My intention is, when user hover the menu item, on the <li> background, apear the basketball, and on <a> tag, the background is going to #FFF but i have small and big names on menu, so i can't set width of <li> and <a> tags... i think
Here is a quick example using pseudo-elements: http://codepen.io/anon/pen/iwerJ
Using the exact HTML you originally posted, with CSS like this:
.nav {
background: #CCC;
font-family: Helvetica, Arial, sans-serif;
line-height: 48px;
margin: 50px auto 0;
width: 90%;
}
.nav ul:after {
clear: both;
content: '';
display: block;
}
.nav ul li {
float: left;
font-size: 14px;
list-style: none;
padding: 0 10px;
position: relative;
}
.nav ul li:hover:after {
/* Replace background with image */
background: #abc123;
/* Optionally remove radius */
border-radius: 30px;
content: '';
display: block;
position: absolute;
top: 50%; left: 50%;
margin-top: -30px;
margin-left: -30px;
height: 60px; width: 60px;
}
.nav a {
color: #333;
display: inline-block;
line-height: 180%;
padding: 0 4px;
position: relative;
text-decoration: none;
z-index: 1;
}
.nav li:hover a { background: #FFF; }
Just set a hover background. Eg:
div.nav li:hover{
background-image: url('basketball.jpg');
}
EDIT:
You've got a lot more issues than just a background image...
You need to vertically center your nav text set a min-width for the
nav cells so that the left and right of the ball aren't cut off
set a solid white background for the anchor tag so the text is actually visible on hover
set a z-index for the anchor tags that's greater
than the center image so that they are all clickable (right now you
can't click the link to the right of the center)
Good luck. I can't write all of that code out for you, but that should send you in the right direction.
on your nav class all you have to do is write your css like this:
.nav ul li:hover
{
background-image:url('yourimage.jpg');
}
To fix the cut-off images, you can simply put a min-width on your .nav li elements. Make sure the value is at least as wide as your background images.
You'll probably also want to add text-align: center.