When a user hovers over my horizontal menu, how can I keep the sub-menu open until another menu item is hovered over? Here is the source code:
HTML
<ul id="nav">
<li>Home</li>
<li>my Links
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</li>
</ul>
CSS
#nav {
width:100%;
margin:0;
padding:12px 0 4px 0;
height:30px;
position:relative;
}
#nav li {
float:left;
list-style:none;
display:inline
}
#nav a {
text-decoration:none;
}
#nav li ul li a {
margin:0
}
#nav .active a, #nav li:hover>a {
background:#ac2024;
color:#fff;
}
#nav li a:hover, #nav ul a:hover {
background:#ac2024;
color:#fff
}
#nav ul {
position:absolute;
left:0;
height:27px;
width:100%;
display:none;
}
#nav li:hover>ul {
display: inline;
}
#nav ul a {
width:100%
}
#nav:after {
content:".";
display:inline;
clear:both;
visibility:hidden;
height:0
}
#nav {
display:inline-block
}
If this is not possible with CSS, how can it be done with jQuery? Thanks.
Based on your updated question, modified answer below.
You'll need to add a small amount of JS as mentioned in my comment, please see here for demo
JSFiddle (updated)
Updated HTML:
<ul id="nav">
<li>Home</li>
<li><a id="hover1" href="#">my Links</a>
<ul id="visible1">
<li>Link 1</li>
<li>Link 2</li>
</ul>
</li>
<li><a id="hover2" href="#">Our Links</a>
<ul id="visible2">
<li>Link 3</li>
<li>Link 4</li>
</ul>
</li>
</ul>
Updated CSS:
#nav {
width:100%;
margin:0;
padding:12px 0 4px 0;
height:30px;
position:relative;
}
#nav li {
float:left;
list-style:none;
display:inline
}
#nav a {
text-decoration:none;
}
#nav li ul li a {
margin:0
}
#nav .active a, #nav li:hover>a {
background:#ac2024;
color:#fff;
}
#nav li a:hover, #nav ul a:hover {
background:#ac2024;
color:#fff
}
#nav ul {
position:absolute;
left:0;
height:27px;
width:100%;
display:none;
}
#nav li:hover>ul {
display: inline;
}
#nav ul a {
width:100%
}
#nav:after {
content:".";
display:inline;
clear:both;
visibility:hidden;
height:0
}
#nav {
display:inline-block
}
.result_hover {
display:block !important;
}
Updated JQuery:
$("#hover1").hover(
function () {
$("#visible1").addClass("result_hover");
$("#visible2").removeClass("result_hover");
}
);
$("#hover2").hover(
function () {
$("#visible2").addClass("result_hover");
$("#visible1").removeClass("result_hover");
}
);
I don't think you could achieve that only with CSS.
The solution that I thing about from the top of my head is to add class 'hover-class' to the li item via jQuery, and while hovering on new menu item adding 'hover-class' to the new item and removing it from the old one. You should keep track in JS what's the last item that has been hovered and manipulat view based on this data.
But maybe someone will suprise me and give solution only in CSS :) Hope it helps.
Related
How i make the child lists expand and collapse by click ?
.tree-list li > ul{
display: none;
}
See the Pen Pure CSS Fullscreen Overlay Menu .
You can use it by jquery/javascript or you can simply use css for hover.
for jquery just write onclick ul display:block along with this css.
<nav id="dropdown">
<ul>
<li>Menu
<ul>
<li> 1</li>
<li> 2</li>
<li> 3</li>
</ul>
</nav>
In CSS
#dropdown
{
margin-top:15px
}
#dropdown ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
#dropdown ul a
{
display:block;
color:#333;
text-decoration:none;
font-weight:700;
font-size:12px;
line-height:32px;
padding:0 15px;
font-family:"HelveticaNeue","Helvetica Neue",Helvetica,Arial,sans-serif
}
#dropdown ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
#dropdown ul li:hover
{
background:#f6f6f6
}
#dropdown ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff;
padding:0
}
#dropdown ul ul li
{
float:none;
width:200px
}
#dropdown ul ul a
{
line-height:120%;
padding:10px 15px
}
#dropdown ul ul ul
{
top:0;
left:100%
}
#dropdown ul li:hover > ul
{
display:block
}
Background color (red) misses out after I am hovering drop down items.
What should I do to keep the background color of the menu item I am in?
<style>
nav ul
{
list-style-type:none;
padding:0px;
text-align:center;
background-color:grey;
}
nav ul li
{
display:inline-block;
}
nav ul a
{
display:block;
padding:20px;
padding-left:50px;
padding-right:50px;
text-decoration:none;
color:black;
text-transform:uppercase;
font-family:arial;
font-size:20px;
}
nav ul a:hover
{
background:red;
}
nav ul ul {
display: none;
}
nav ul li:hover ul{
display:block;
position:absolute;
}
nav ul ul li
{
display:block;
border-bottom:solid 1px black;
}
</style>
</head>
<body>
<nav>
<ul>
<li>one</li>
<li>two
<ul>
<li>Photoshop</li>
<li>Illustrator</li>
<li>Web Design</li>
</ul>
</li>
<li>three
<ul>
<li>ay</li>
<li>bee</li>
</ul>
</li>
<li>four</li>
</ul>
</nav>
</body>
This is because the parent li is not attracting the :hover, so a hover on the child submenu doesn't keep it active. As such, you need to move the style change to the li:hover instead of the a child, as the submenu is an sibling of the a and not direct child.
Change:
nav ul a:hover {
background:red;
}
To:
nav ul li:hover {
background:red;
}
Demo Fiddle
Do you guys have any idea how to remove the right border on my drop down menu? I tried putting right-border: none, right-border: hidden, and right-border: 0px but nothing!
HTML :
<section class="menu">
<ul>
<li><a class="active" href="#"> PORTFOLIO </a>
<ul>
<li> illustrations </li>
<li> portraits </li>
<li> environments </li>
<li> life drawings </li>
</ul>
</li>
<li> STORE
<ul>
<li> society6 </li>
<li> redbubble </li>
</ul>
</li>
<li> CONTACT </li>
<li> ABOUT </li>
</ul>
</section>
CSS :
.menu {
height:29px;
width:100%;
/*background:orange;*/
}
.menu ul {
width:auto;
list-style-type:none;
font-family:"calibri", "arial";
}
.menu ul li {
position:relative;
display:inline;
float:left;
width:auto;
border-right: 2px solid purple;
margin-left:10px;
line-height:12px;
}
.menu ul li a {
display:block;
padding:3px;
color:#854288;
text-decoration:none;
font-size:20px;
font-weight:strong;
padding-right:25px;
}
.menu ul li a:hover, .active {
color:#788d35
}
.menu ul li ul {
display:none;
}
.menu ul li:hover > ul {
display:block;
position:absolute;
top:23px;
float:left;
padding-left:20px;
text-align:left;
margin-left: -30px;
}
.menu ul li ul li {
position:relative;
min-width:135px;
max-width:1350px;
width:100%;
}
.menu ul li ul li a {
padding: 3px;
margin-left: 1px;
border-right: hidden; /* <---- DOES NOT WORK */
}
This removes border from the main menu (after the last item About) :
.menu ul li:last-child{ border:none; }
JSFiddle
If you also want to remove border from the nested lis, you should add border:none to .menu ul li ul li :
JSFiddle
try this
#right_border_less{
border:solid;
border-right:none;
}
I've made an horizontal menu that all menu items take 100% of the nav. To do this, I use display:table in 'lu', and display:table-cell in 'li'. In this way the menu is horizontal and extends all manu items to 100% width. The problem is the submenu is also horizontally but I want the submenu items vertically.
<script type="text/javascript">
$('body').ready(function() {
$('.dropdown').hover(function() {
$(this).find('.sub_navigation').slideToggle();
});
});
</script>
ul#navigation {
float:left;
display:table;
margin:0;
padding:0;
width:100%;
font-size:15px;
background-color:#FFF;
}
ul#navigation li {
display: table-cell;
margin:0;
padding:14px 3px 14px 3px;
text-align:center;
}
ul.sub_navigation {
position:absolute;
display:none;
margin:0;
padding:0;
background-color:#FFF;
opacity:0.8;
list-style-type:none;
}
ul.sub_navigation li {
clear:both;
}
ul#navigation li a,
ul#navigation li a:active,
ul#navigation li a:visited {
color:#000000;
text-decoration:none;
display:block;
}
ul#navigation li a:hover {
color:#478961;
}
<ul id="navigation">
<li class="dropdown">
Item 1
<ul class="sub_navigation">
<li>Sub item 1</li>
<li>Sub item 2</li>
</ul>
</li>
<li>Item 2</li>
</ul>
What can I add to sub_navigation styles to show subenu items vertically?
Thanks
Victor
Since the elements are set to clear:both, simply add float:left to the li elements in the subnavigation.
ul.sub_navigation li {
float:left;
clear:both;
}
Doing so without floats if that's what you want:
ul.sub_navigation li a {
display: inline-block;
}
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
I am trying to create a sub menu that floats out to the right. However, I have hit a brick wall and can only get it to float inline.
http://jsfiddle.net/jspring/yD9N4/
You can see here (although it is a bit wider than normal) that the sub menu just displays inside the parent list item. If someone could help me to get it floating out to the right that would be great!
<ul class="cl-menu">
<li>Link 1
<ul>
<li>Sub Link 1
</li>
<li>Sub Link 2
</li>
</ul>
</li>
<li>Link 2
<ul>
<li>Sub Link 1
</li>
<li>Sub Link 2
</li>
</ul>
</li>
</ul>
.cl-menu{
list-style:none outside none;
display:inline-table;
position:relative;
width:100%;
}
.cl-menu li{
list-style:none outside none;
border-bottom:1px solid #cccccc;
padding:5px 1px;
text-align:center;
}
.cl-menu > li:hover{
/*background-color:#303030;*/
background-color:#66819C;
color:#FFF;
font-weight:bold;
text-decoration:underline;
opacity:1;
}
.cl-menu li ul{
display:none;
}
.cl-menu li:hover ul{
display:block;
opacity:0.8;
background-color:#FFF;
margin-top:4px;
font-weight:normal !important;
}
.cl-menu li ul li{
border-bottom:1px solid #cccccc !important;
border-top:none !important;
border-left:none !important;
border-right:none !important;
}
.cl-menu li ul li a{
color:#000;
text-decoration:none;
}
.cl-menu li ul li a:hover{
color:#390;
text-decoration:underline;
}
.cl-menu ul:after{
content:"";
clear:both;
display:block;
}
Use position:absolute property for submenu. Use right css property for positioning the submenu properly.
.cl-menu li ul
{
display:none;
position:absolute;
right:20%;
}