Submenu with distance, on hover is closed - html

I have a submenu, the problem is my submenu must have more distance and should be visible on hover, so if i'm out of li the submenu goes closed.
Demonstration code on jsfiddle.
HTML code is:
<ul class="menu">
<li> submenu
<ul>
<li>sub item 1
</li>
<li>sub item 2
</li>
<li>sub item 3
</li>
<li>sub item 4
</li>
<li>sub item 5
</li>
<li>sub item 6
</li>
<li>sub item 7
</li>
</ul>
</li>
</ul>
CSS is:
a {
text-decoration: none;
}
ul.menu {
}
ul.menu li {
list-style-type: none;
position: relative;
float: left;
display: inline;
margin: 0px 0px 1px 1px;
padding: 0px 0px 0px 0px;
height: 33px;
line-height: 33px;
}
ul.menu li a {
display: block;
color: #5b615b;
padding: 0px 7px 0px 7px;
background-color: #e1e1e1;
text-transform: uppercase;
}
ul.menu li a:hover {
color: #000000;
background-color: #e1e1e1;
}
ul.menu li ul {
display: none;
font-size: 10px;
width: 100%;
position: absolute;
top: 37px;
left: 0px;
margin: 0px;
padding: 0px;
}
ul.menu li:hover > ul {
display: block;
}
ul.menu li:hover > a {
color: #000000;
background-color: #e1e1e1;
}
ul.menu li:hover > ul {
display: block;
}
ul.menu li ul li {
position: relative;
float: none;
display: block;
height: 22px;
line-height: 22px;
border-bottom: 1px solid #ffffff;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
ul.menu li.active ul li a {
color: #5b615b;
background-color: #e1e1e1;
}

Change the top on ul.menu li ul to 33px instead of 37, and add a padding-top of 1px. Updated Fiddle here.
The padding ensures that the hover effect remains active between the elements (padding is part of the element so also has hover).

ul.menu li ul {
display: none;
font-size: 10px;
width: 100%;
position: absolute;
top: 34px;
left: 0px;
margin: 0px;
padding: 0px;
}
change top:37; to top:34; If you want to maintain the whitespace, you can add a 3px white border to the bottom of the top li.
jsfiddle showing this method http://jsfiddle.net/N23AM/4/
visualy identical, just allows the hover to be maintained by the border rather than losing focusing to the background.

The proper solutions would be to add padding-top:20px;(your desired value would be there of course) put in ul li:hover ul selector.
Another, maybe strange solution, that I could think of is to add in your ul submenu a new div that looks like this:
<div style="height:20px; background:transparent; width:100%;"></div>

Related

Make centered text start from the same horizontal position as non-centered text

I have a ul and h2 element. The container that both elements are in has padding-left: 4%.
I want the h2 and ul elements to both start from the same horizontal position so that they're directly below each other. The problem is that as the contents of the li elements are centered, the first element is not directly below the h2 - there's a gap:
If I add margin-left: -3% to the first li element, it pushes it to the left and removes the gap:
But they're not exactly aligned and I'd have to fiddle around with the margin to make it exact. Is there an alternative way to make them directly start from the same point?
#menu {
background: #bb1919;
padding: 0 4%;
}
#menu h1 {
margin: 0;
font-weight: normal;
padding-top: 10px;
}
#menu ul {
list-style-type: none;
padding: 0;
display: flex;
align-items: center;
margin: 0;
}
#menu li {
display: inline-block;
border-bottom: 4px solid transparent;
cursor: pointer;
text-align: center;
}
#menu li:first-of-type {
/* margin-left: -2%; */
}
#menu li:hover {
border-bottom: 4px solid #fff;
}
#menu li:first-of-type p {
border-left: none;
}
#menu li p {
border-left: 1px solid #d0d0d06b;
padding: 0 14px;
margin: 14px 0 8px 0;
}
<div id="menu">
<h1>NEWS</h1>
<ul>
<li>
<p>Home</p>
</li>
<li>
<p>Coronavirus</p>
</li>
<li>
<p>UK</p>
</li>
<li>
<p>World</p>
</li>
<li>
<p>Business</p>
</li>
<li>
<p>Politics</p>
</li>
</ul>
</div>
JsFiddle: https://jsfiddle.net/naq0mb48/
Use :first-of-type rules for li and the p inside li to set margin-left and padding-left to 0 and text-align to left.
ADDITION after comment:
You can add margin-left: -14px; text-indent: 14px; to the #menu li:first-of-type rule to achieve the desired underline left of the left-aligned first menu entry on hover.
#menu {
background: #bb1919;
padding: 0 4%;
}
#menu h1 {
margin: 0;
font-weight: normal;
padding-top: 10px;
}
#menu ul {
list-style-type: none;
padding: 0;
display: flex;
align-items: center;
margin: 0;
}
#menu li {
display: inline-block;
border-bottom: 4px solid transparent;
cursor: pointer;
text-align: center;
}
#menu li:hover {
border-bottom: 4px solid #fff;
}
#menu li p {
border-left: 1px solid #d0d0d06b;
padding: 0 14px;
margin: 14px 0 8px 0;
}
#menu li:first-of-type {
text-align: left;
padding-left: 0px;
margin-left: -14px;
text-indent: 14px;
}
#menu li:first-of-type p {
border-left: none;
padding-left: 0px;
}
<div id="menu">
<h1>NEWS</h1>
<ul>
<li>
<p>Home</p>
</li>
<li>
<p>Coronavirus</p>
</li>
<li>
<p>UK</p>
</li>
<li>
<p>World</p>
</li>
<li>
<p>Business</p>
</li>
<li>
<p>Politics</p>
</li>
</ul>
</div>

How can I get an anchor inside these list items to fill the space of the list item?

I'm using a fairly typical nested UL setup to create a dropdown menu, however I can't get the anchorlinks inside the li to expand to their height.
The HTML
<div id="navbar-container">
<ul id="navbar">
<li>Home</li>
<li>Lessons</li>
<ul>
<li>sub item1sdfsdfsdfsdfsdf</li>
<li>sub item2</li>
<li>sub item3</li>
</ul>
</li>
<li>Custom Fitting</li>
</ul>
</div>
In the CSS I'm using display:block on the anchor tags which does make them expand to the width of the li but not the height. I have tried using padding but it does not work correctly across all browsers. #navbar is using display: table and the children lis are using display: table-cell. This is so the navbar can expand and contract to fit the screen size. I suspect display: table-cell may have something to do with the anchors not expand vertically.
Here is a JSFiddle so you can see what I'm talking about.
The CSS
#navbar-container {
min-width: 768px;
height: 32px;
position: relative;
background-color: #bb4212;
}
#navbar {
list-style-type: none;
display: table;
width: 100%;
font : 14px"Arial", sans-serif;
height: 100%;
}
#navbar li {
text-transform:uppercase;
display: table-cell;
text-align: center;
}
#navbar li a {
color: #f2f2f2;
display: block;
border-left: 1px solid #c17455;
}
#navbar > li:first-child a {
border: 0;
}
#navbar li ul {
list-style-type: none;
background-color: #f2f2f2;
position: absolute;
right: -9999px;
top: 32px;
margin-left: 1px;
-moz-box-shadow: 0px 6px 4px 0px #898989;
-webkit-box-shadow: 0px 6px 4px 0px #898989;
box-shadow: 0px 6px 4px 0px #898989;
}
#navbar li ul li:hover {
background-color: #bb4212;
}
#navbar li ul a:hover {
color: #f2f2f2;
}
#navbar li:hover {
background-color: #f2f2f2;
}
#navbar li:hover a {
color: #000;
}
#navbar li:hover ul {
right: auto;
}
#navbar li ul li {
display: block;
text-align: left;
}
#navbar li ul li a {
border: 0;
white-space:nowrap;
margin: 0 5px;
text-decoration: none;
display: block;
}
My favorite technique for filling up a parent container 100% width and height is to use absolute positioning:
parent {
position: relative; /* unless it's already positioned */
}
child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Here it is applied to your JSFiddle: http://jsfiddle.net/Kgz5p/

Issue with circular drop down menu

I am writing an article for my blog on the menu.
I am trying to create a Circular drop down menu. It works fine when there are two or more Submenus on all browsers. But if there is only one sub-menu to a parent menu it is displayed way below its intended position.
Here is working copy of the following code in jsfiddle: http://jsfiddle.net/avdhut/LDmM8/5/
I know the problem is in the #menucontainer ul li:hover > ul selector,
its with margin. But if I remove that margin-top menus having 2 or more sub-menus go up and are displayed way above their intended position.
Please help as I cannot find a suitable solution on this issue.
Following is the HTML code:
<div id="menucontainer">
<ul id="hmenu">
<li>Home
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
<li>Test 3</li>
</ul>
</li>
<li>About
<ul>
<li>Test 1</li>
<li>Test 2</li>
</ul>
</li>
<li>Contact Us
<ul>
<li>Test 1</li>
</ul>
</li>
<li>Info
<ul>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</li>
</ul>
</div>
Following is the CSS code:
#menucontainer {
margin: 0px;
padding: 0px;
width: 100%;
height: 90px;
}
/* Following selectors will define the color and the border radius for the menu*/
#menucontainer ul, #menucontainer ul li ul {
list-style: none;
margin: 0px;
padding: 0px;
}
/* Following selector will define the style for individual menu or li tags. The following style gives them a look when the menus are not selected.
note that the position proerty is set to relative and dispaly is inline.
*/
#menucontainer ul li {
list-style: none;
margin: 10px;
padding: 0px;
background-color:#2CDF7B;
position: relative;
display: inline;
text-align: center;
/*Setting the height and width for the menu*/
width: 90px;
cursor:pointer;
height: 90px;
/*Setting the border-radius for the menu
various prefix are added so it is supported in older version of
browsers. For IE supported from IE-9+
IMPORTANT: to keep the border radius height and width same to get circle.
*/
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
-khtml-border-radius: 90px;
border-radius: 90px;
/*Adding the box shadow to the menu*/
box-shadow: 0 0 10px black;
-moz-box-shadow: 0 0 10px black;
-webkit-box-shadow: 0 0 10px black;
float: left;
}
/* Style for the anchor tag defined in the menu.
*/
#menucontainer ul li a {
margin: 10px;
padding: 10px;
background-color:transparent;
display: block;
text-decoration: none;
text-align: center;
font-family:"Times New Roman", "Arabic";
font-size: 18px;
color: #E3E3C0;
}
/* The hover effect is defined using the below selectors
For the opacity is used to create a semi-transperant look as a hover effect
*/
#menucontainer ul li:hover {
list-style: none;
margin: 10px;
padding: 0px;
opacity: 0.7;
background-color:#2CDF7B;
position: relative;
display: block;
width: 90px;
text-align: center;
cursor:pointer;
height: 90px;
/*Setting the border-radius for the menu
various prefix are added so it is supported in older version of
browsers. For IE supported from IE-9+
IMPORTANT: to keep the border radius height and width same to get circle.
*/
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
-khtml-border-radius: 90px;
border-radius: 90px;
-moz-box-shadow: inset 0 0 10px black;
box-shadow: inset 0 0 10px black;
-webkit-box-shadow: inset 0 0 10px black;
}
#menucontainer ul li:hover a {
margin: 10px;
padding: 10px;
background-color:transparent;
display: block;
text-decoration: none;
text-align: center;
font-family:"Times New Roman", "Arabic";
font-size: 18px;
color: white;
}
/*******************************************************/
/*******************************************************/
/*Sub menu*/
/*******************************************************/
#menucontainer ul li > ul {
display : none;
overflow: hidden;
position: relative;
border-bottom: solid 1px;
border: 0px;
height: 0px;
width: 100%;
}
#menucontainer ul li:hover > ul {
z-index: 10;
float: none;
left: -15px;
margin: 40px 10px 10px 10px;
height: auto;
padding: 0px 5px 0px 0px;
display:block;
width: 100px;
}
#menucontainer ul li:hover > ul li, #menucontainer ul li a:hover > ul li {
list-style: none;
margin: 10px;
padding: 0px;
background-color:#2CDF00;
position: relative;
display: inline;
text-align: center;
/*Setting the height and width for the menu*/
width: 90px;
cursor:pointer;
height: 90px;
/*Setting the border-radius for the menu
various prefix are added so it is supported in older version of
browsers. For IE supported from IE-9+
IMPORTANT: to keep the border radius height and width same to get circle.
*/
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
-khtml-border-radius: 90px;
border-radius: 90px;
/*Adding the box shadow to the menu*/
box-shadow: 0 0 10px black;
-moz-box-shadow: 0 0 10px black;
-webkit-box-shadow: 0 0 10px black;
float: left;
}
/*******************************************************/
#menucontainer ul li:hover > ul li:after, #menucontainer ul li a:hover > ul li:after {
content:" ";
border: solid transparent;
margin: 0px;
padding: 0px;
position: absolute;
pointer-events: none;
border-color: rgba(44, 223, 123, 0);
border-top-color: #2CDF00;
top: 98%;
left: 50%;
margin-left: -10px;
border-width:10px;
}
#menucontainer ul li:hover > ul li:before, #menucontainer ul li a:hover > ul li:before {
content:" ";
border: solid transparent;
margin: 0px;
padding: 0px;
position: absolute;
pointer-events: none;
border-color: rgba(44, 223, 123, 0);
border-bottom-color: #2CDF00;
bottom: 98%;
left: 50%;
margin-left: -10px;
border-width:10px;
}
/*******************************************************/
Just some changes to css and you are done:-
#menucontainer ul li > ul {
display : none;
overflow: hidden;
position: absolute; //implement absolute
border-bottom: solid 1px;
border: 0px;
height: 0px;
width: 100%;
top:90px; //add top
}
#menucontainer ul li:hover > ul {
z-index: 10;
float: none;
left: -15px;
margin: 5px; //change it to this
height: auto;
padding: 0px 5px 0px 0px;
display:block;
width: 100px;
}
Working fiddle:- http://jsfiddle.net/LDmM8/7/
It has nothing to do with the fact that you only have 1 item in that dropdown menu. You can try and add more and see it's still happening. The real reason is that the "Contact Us" button is taking two rows instead of one like the other buttons.
The problem is solved by setting a fixed height for the anchors.
jsFiddle Demo
#menucontainer ul li a {
height:33px;
}

How can I stop my submenu from disappearing while hovering between spaced submenu items?

I have a CSS menu with a submenu, and each submenu list item has a 1px space between them. The problem mostly occurs in IE, but unless you move your cursor very quickly, the submenu will disappear while hovering between the two submenu items (due to the 1px space). How do I stop this from happening while keeping the 1px spaces?
<nav id="menu" class="sixteen columns">
<ul id="nav">
<li>Home
<ul>
<li>drop 1</li>
<li>drop 1</li>
<li>drop 1</li>
</ul>
</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
</nav>
#menu {
position: relative;
z-index: 999;
}
ul#nav {
margin: 0;
padding: 1px 0;
width: auto;
}
/* start first level nav items */
ul#nav li {
float: left;
padding: 4px;
list-style: none;
list-style-image: none;
}
ul#nav li a {
font-size: 13px;
text-decoration: none;
display: block;
text-align: left;
padding: 3px 9px;
white-space: nowrap;
text-transform: uppercase;
color: #000;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
ul#nav li.current a, ul#nav li a:hover {
color: #fff;
background: black;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
ul#nav li ul {
position: absolute;
width: 10em;
left: -999em;
margin:0px;
padding:0px;
}
ul#nav li ul li {
padding: 0;
margin: 1px 0;
}
ul#nav li ul li a,
ul#nav li.current ul li a {
width: 200px;
background-color: #000;
line-height: 26px;
color: #fff;
}
ul#nav li ul li a:hover,
ul#nav li.current ul li a:hover {
background-color: #FA891C;
color: #fff;
}
Get rid of the 1px margin and add a 1px spacer that is part of the hover element.
ul#nav li ul li {
padding: 0;
margin: 1px 0; /* change to margin:0px;*/
}
And...
ul#nav li.current ul li a {
border-bottom: 2px solid transparent; /*Add this*/
padding: 0px; /*And this*/
width: 200px;
background-color: #000;
line-height: 26px;
color: #fff;
}
and add this css
.subMenu1
{
margin-left:10px; /* adjust as needed */
}
and wrap your submenu items in a div like this:
<li><div class="subMenu1">drop 1</div></li>
http://jsfiddle.net/BZkfy/
You can use the jQuery hoverIntent plugin to add a delay that will allow users to keep focus on your menus.
http://cherne.net/brian/resources/jquery.hoverIntent.html
E.G.
$('.main-nav > ul > li').hoverIntent({
timeout: 300,
over: function () {
$(this).addClass('hover')
},
timeout: 300,
out: function () {
$(this).removeClass('hover')
}
});

Pure HTML/CSS menu with horizontal submenu. How keep parent 'a' background color?

I am building a pure HTML/CSS menu with a horizontal submenu.
The problem is that when you are hovering on the submenu the background color from the hover of the main-menu item is gone.
Is it possible to keep that background color of the main menu item when you are hovering on the submenu?
Here is what I got so far:
http://jsfiddle.net/YKEkB/1/
As you can see when you hover over "this is the one" you get the submenu. But when you are in the submenu the background color of "this is the one" changes back.
Is there any solution with pure HTML/CSS?
This is all the code:
<div class="wrapper">
<div class="menu-holder">
<ul class="menu">
<li>item 1
</li>
<li><a class="test" href="#">This is the one</a>
<ul class="submenu">
<li>Submenu item 1
</li>
<li>Submenu item 2
</li>
</ul>
</li>
<li><a href="#" >Menu item 3</a>
</li>
<li><a href="#" >Last item</a>
</li>
</ul>
</div>
</div>
css:
.wrapper {
width:900px;
height:200px;
background:grey;
}
.menu-holder {
padding: 50px 0 0 0;
}
.menu-holder ul {
margin: 0 0 0px 25px;
padding: 0;
list-style-type: none;
}
.menu-holder ul li {
position: relative;
float: left;
padding: 0px 10px 0 10px;
margin: 0px 0 0 0px;
border-left: 1px dotted white;
line-height: 0px;
}
.menu-holder ul li a {
font-family: arial, sans-serif;
font-size: 12px;
font-style: bold;
display: block;
color: white;
text-decoration: none;
padding: 15px 10px 15px 10px;
-webkit-border-radius: 5px 5px 0px 0px;
border-radius: 5px 5px 0px 0px;
}
.menu-holder ul li a:hover + ul {
display: block;
}
.menu-holder ul li a:hover {
display: block;
background-color: #025179;
}
.menu-holder ul li .submenu {
display: none;
position: absolute;
top: 100%;
left: 0px;
right: auto;
margin: -5px 0 0px 0px;
padding: 5px 10px 5px 10px;
white-space: nowrap;
}
.menu-holder ul li .submenu li {
position: static;
float: left;
display: inline;
padding: 15px 10px 15px 10px;
background-color: #025179;
}
.menu-holder ul li .submenu li a {
display: inline;
margin: 0 0px 0 0px;
padding: 0px 10px 0px 10px;
-webkit-border-radius: 0;
border-radius: 0;
}
.menu-holder ul li .submenu li:first-of-type {
-webkit-border-radius: 0px 0px 0px 5px;
border-radius: 0px 0px 0px 5px;
}
.menu-holder ul li .submenu li:last-of-type {
-webkit-border-radius: 0px 5px 5px 0px;
border-radius: 0px 5px 5px 0px;
}
.menu-holder ul li .submenu:hover {
display: block;
}
.menu-holder ul li .submenu:hover .test {
display: block;
background-color: #025179;
}
.menu-wrapper .menu-holder ul li:first-of-type {
border-left: none;
}
Easy: http://jsfiddle.net/YKEkB/2/
You have to change it to this (delete the a):
.menu-holder ul li:hover{
display: block;
background-color: #025179;
}
Now you will have to adjust it so it's centered vertically. Note that you should keep 0px between the menu item and the submenu.
Many more things.
You can use the pseudo element :last-child to add the dotted line in the last item (see fiddle 3):
.menu-holder ul li:last-child {
border-right: 1px dotted white;
}
If I'm right, you only wanted to round the last element in the drop down menu and the second corner of the first one:
.menu-holder ul li ul li:first-of-type {
-webkit-border-radius: 0px 5px 0px 0px;
border-radius: 0px 5px 0px 0px;
}
.menu-holder ul li .submenu li:last-of-type {
-webkit-border-radius: 0px 0px 5px 5px;
border-radius: 0px 0px 5px 5px;
}
You can (and should) access the submenues like this:
.menu-holder ul li ul {
Instead of this:
.menu-holder ul li .submenu {
Cleaned and touched a couple of things more, and here is your code working. Tell me if you find any trouble. There's still room for improvement (readability and DRY to name some), but here it goes:
Final Demo