Apply Hover on the Nested Class Navigation - css

I have a mock up sample code below that displays a nested sub menu on my project
.navigation {
.cmp-navigation {
.cmp-navigation__group {
.cmp-navigation__item--level-0 {
display: inline-block;
padding: 10px;
.cmp-navigation__item-link {
color: red;
}
.cmp-navigation__group {
position: absolute;
display: block;
.cmp-navigation__item--level-1 {
.cmp-navigation__item-link {
color: yellow;
}
.cmp-navigation__group {
left: 100%;
position: absolute;
top: 0px;
.cmp-navigation__item--level-2 {
.cmp-navigation__item-link {
color: orange;
}
}
}
}
}
}
}
}
}
.navigation {
.cmp-navigation {
.cmp-navigation__group {
.cmp-navigation__item--level-0 {
display: inline-block;
padding: 10px;
.cmp-navigation__item-link {
color: red;
}
.cmp-navigation__group {
position: absolute;
display: block;
.cmp-navigation__item--level-1 {
.cmp-navigation__item-link {
color: yellow;
}
.cmp-navigation__group {
left: 100%;
position: absolute;
top: 0px;
.cmp-navigation__item--level-2 {
.cmp-navigation__item-link {
color: orange;
}
}
}
}
}
}
}
}
}
.cmp-navigation__item--level-1 {
display: none;
}
<div class="navigation aem-GridColumn aem-GridColumn--default--12">
<nav class="cmp-navigation" role="navigation" itemscope itemtype="http://schema.org/SiteNavigationElement">
<ul class="cmp-navigation__group">
<li class="cmp-navigation__item cmp-navigation__item--level-0">
Home
</li>
<li class="cmp-navigation__item cmp-navigation__item--level-0">
Equipments
<ul class="cmp-navigation__group">
<li class="cmp-navigation__item cmp-navigation__item--level-1">
Trucks
</li>
<li class="cmp-navigation__item cmp-navigation__item--level-1">
Woods
</li>
</ul>
</li>
</ul>
</nav>
</div>
And the output is something like this
The code above works fine but my problem is how to apply the hover on sub menu. My target for this nested view is to apply the hide and show on hover. Thanks for the help

First you need to hide the submenu items using display: none. Then you need to add :hover to the parent of the submenu to change from display: none to display: block. Here's an example.
nav ul li ul li {
display: none;
}
nav ul li:hover ul li {
display: block;
}
<div class="navigation aem-GridColumn aem-GridColumn--default--12">
<nav class="cmp-navigation" role="navigation" itemscope itemtype="http://schema.org/SiteNavigationElement">
<ul class="cmp-navigation__group">
<li class="cmp-navigation__item cmp-navigation__item--level-0">
Home
</li>
<li class="cmp-navigation__item cmp-navigation__item--level-0">
Equipments
<ul class="cmp-navigation__group">
<li class="cmp-navigation__item cmp-navigation__item--level-1">
Trucks
</li>
<li class="cmp-navigation__item cmp-navigation__item--level-1">
Woods
</li>
</ul>
</li>
</ul>
</nav>
</div>

Related

How can I change the hover to onclick in css?

I have tried to change the hover to click
and search online already
but it still does not work
how can i change the code to onclick
so i can click and show the drop down menu?
The pattern
$* {
box-sizing: border-box;
}
ul {
padding: 0;
}
ul a {
display: inline-block;
text-decoration: none;
color: #ffefc6;
.menu {
max-width: 300px;
margin: 0 auto;
list-style-type: none;
background-color:#333;
font-weight:bold;
The effect
$.drop-down {
position: relative;
}
.submenu {
position: relative;
opacity: 0;
width: 100%;
z-index: 8;
transition: opacity 0.5s ease;
}
.submenu-item {
display: block;
height: 0px;
overflow: hidden;
transition: height 0.5s ease;
}
.drop-down:hover .submenu {
opacity: 1;
}
.drop-down:hover .submenu-item {
overflow: visible;
height: 30px;
}
About the html code
<ul class="menu">
<li class="itembox drop-down">
<a class="item" href="#">Intro</a>
<div class="submenu">
<a class="submenu-item" href="#">Seting </a>
<a class="submenu-item" href="#">Rule </a>
<a class="submenu-item" href="#">Mode </a>
<a class="submenu-item" href="#">Type </a>
</div>
</li>
You have to use JavaScript, css can't listen to click events.
function toggle(id) {
var elt = document.getElementById(id);
elt.style.display = elt.style.display=='block' ? 'none' : 'block';
}
.submenu {
display: none;
}
<ul>
<li onclick="toggle('submenu-1')">
Option 1
<ul id="submenu-1" class="submenu">
<li>Sub-option 1.1</li>
<li>Sub-option 1.2</li>
</ul>
</li>
<li onclick="toggle('submenu-2')">
Option 2
<ul id="submenu-2" class="submenu">
<li>Sub-option 2.1</li>
<li>Sub-option 2.2</li>
</ul>
</li>
</ul>

Hovering over a menu item to highlight background of subnav

I'm trying to make it so when you hover over one of the categories in the header nav "CALLS | CHATS | MORE" the subnav below shows and the ul that is associated with the header nav changes its background color i know i can just add a class using jQuery but could i not do this with a sibling selector or something? i just think my HTML is layed out incorrectly at the moment for it to work TIA. this is what i currently have.
<header class="header">
<ul class="header__nav">
<li class="header__navItem header__navItem--calls">Calls</li>
<li class="header__navItem header__navItem--chats">Chats</li>
<li class="header__navItem header__navItem--more">More</li>
</ul>
<nav class="navigation">
<div class="navigation__wrapper cf">
<ul class="navItems navItems--calls">
<li class="navItems__item"></li>
<li class="navItems__item"></li>
<li class="navItems__item"></li>
<li class="navItems__item"></li>
<div class="bgHover"></div>
</ul>
</div>
</nav>
</header>
.header {
height: 5.8rem;
background: rgba(25, 25, 25, 0.9);
position: absolute;
width: 100%;
z-index: 999;
&__nav {
display: inline-block;
text-align: center;
font-size: 0;
left: 50%;
position: absolute;
transform: translateX(-50%);
padding: 0;
margin: 0;
}
&__navItem {
color: $white;
display: inline-block;
#include font-size(18);
padding: 0 4rem;
line-height: 5.8rem;
letter-spacing: 0.1rem;
position: relative;
cursor: default;
font-weight: 400;
text-transform: uppercase;
font-family: $lato;
&--calls {
&:hover ~ .navigation__wrapper .navItems .bgHover {
max-height: 35rem;
}
&:hover {
color: red;
}
}
}
I'm presuming you want the submenu to appear below the header nav. If so, then move your sub nav into the li tag for the corresponding header item, like so:
<li class="header__navItem header__navItem--calls">Calls
<ul class="navItems navItems--calls">
<li class="navItems__item">First</li>
<li class="navItems__item">Second</li>
<li class="navItems__item">Third</li>
<li class="navItems__item">Fourth</li>
</ul>
</li>

Background color won't take up whole element

I have a drop down menu button that turns grey when hovered over. However, the grey does not expand throughout the whole button, but stops at the where I set the padding.
HTML:
#dropdown {
list-style: none;
display: none;
position: absolute;
background-color: #fff;
padding: 10px;
z-index: 99999;
width: 230%;
}
#dropdown li:hover,
#dropdown li:active {
background-color: #e3e3e3;
}
#dropdown.active, #menu:target #dropdown {
display: block;
}
<ul class="main-nav js--main-nav">
<li id="menu">
Help▾
<ul id="dropdown">
<li>Give</li>
<li>Get Help</li>
<li>Get Involved</li>
</ul>
</li>
</ul>
What I want is for the grey to take up all of the drop-down menu when hovered over, as opposed to just stopping at the padding.
EDIT: I should have clarified, when I meant I want it to take up "all" of the drop down menu, I meant just the "li" when I hover over that.
Is this what you were looking for?
#dropdown {
list-style: none;
display: none;
position: absolute;
background-color: #fff;
padding: 10px;
z-index: 99999;
width: 230%;
}
#dropdown:hover {
background-color: #eee;
}
#dropdown li:hover,
#dropdown li:active {
background-color: #e3e3e3;
}
#dropdown.active, #menu:target #dropdown {
display: block;
}
<ul class="main-nav js--main-nav">
<li id="menu">
Help▾
<ul id="dropdown">
<li>Give</li>
<li>Get Help</li>
<li>Get Involved</li>
</ul>
</li>
</ul>

Side bar hyperlink are not hidden by height css

CSS
.nested-menu {
.list-group-item {
cursor: pointer;
}
.nested {
list-style-type: none;
}
ul.submenu {
height: 0;
}
& .expand {
ul.submenu {
list-style-type: none;
height: auto;
li {
a {
color: #FFF;
padding: 10px;
display: block;
}
}
}
}
}
HTML
<div class="nested-menu">
<a class="list-group-item" (click)="addExpandClass('pages')">
<span>
<i class="fa fa-universal-access"></i>
Account
</span>
</a>
<div class="nested" [class.expand]="showMenu === 'pages'">
<ul class="submenu">
<li>
<a [routerLink]="['account/password']" (click)="eventCalled()">
<span>Password reset</span>
</a>
</li>
<li>
<a [routerLink]="['account/user']" (click)="eventCalled()">
<span>User</span>
</a>
</li>
</ul>
</div>
</div>
before clicking it looks like this. height: 0 is not hiding the child links. How to hide them ?
You need to add overflow:hidden; to your submenu class.
ul.submenu {
height: 0;
overflow: hidden;
}
You cannot set the height for inline elements. You will have to convert that to either block element or inline-block element to be able to do it.
Set display: inline-block; or display: block;

how to overlap a list of menus over an image

When the menu "product" is clicked or mouse over, the another list of menus appear.. but the image block which is below the menu bar, moves away from the position. if i use css [ position:absolute;], then the image box remains static and the product's sub-menu overlaps the image block, which is what i wanted. but the image blocks width & height settings change drastically, thereby spoiling the alignment.
pls chk the codings in jsFiddle
.home_menu {
border: 1px solid black;
width: 98%;
height: 3.3%;
margin-right: auto;
margin-left: auto;
}
div#menuDemo {
clear: both;
//border:1px solid black;
height: 78%;
width: 100%;
position: relative;
left: 0;
top: 0;
background-color: #A55927;
/*Remove this next one in production - Used for demo purpose only*/
margin-bottom: 0.1%;
padding-top: 0.7%;
z-index: 4;
}
div#menuDemo ul {
list-style: none;
margin: 0;
padding: 0;
background-color: #A55927;
}
div#menuDemo > ul > li {
float: left;
text-align: center;
}
div#menuDemo ul li {
width: 25%;
//border: 5px solid purple;
}
div#menuDemo ul li a {
text-decoration: none;
font-weight: bolder;
text-align: center;
}
div#menuDemo > ul > li > ul {
display: none;
text-align: center;
}
div#menuDemo > ul > li:hover > ul {
display: block;
text-align: center;
}
.sub1 {
width: 100%;
//border:1px solid green;
}
.colouring {
color: black;
font-weight: bolder;
}
.colour {
//border:1px solid blue;
color: black;
text-align: center;
//width:100%;
}
.wrapper {
border: 5px solid pink;
width: 98.8%;
height: 82%;
margin-top: 1%;
z-index: 2;
}
.uniform_block {
border: 5px solid green;
width: 100%;
height: 40%;
cursor: pointer;
}
.uniform_block img {
width: 100%;
height: 100%;
}
<body>
<div class="home_menu">
<div id="menuDemo">
<ul>
<li id="homeMenu">About Us
</li>
<!-- <li >About Us</li> -->
<li>Products
<ul class="sub1">
<li> Uniforms
<ul>
<li> &nbsp
</li>
<li> Automobile Industry Uniforms
</li>
<li> Pharmaceutical Uniforms
</li>
<li> Food Industry Uniforms
</li>
<li> Government Sector Uniforms
</li>
<li> School/College Uniforms
</li>
<li> &nbsp
</li>
</ul>
</li>
<li>Shoes
<ul>
<li> &nbsp
</li>
<li> Industrial Shoes
</li>
<li> Safety & Security Shoes
</li>
<li> Executive Shoes
</li>
<li> &nbsp
</li>
</ul>
</li>
</ul>
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
<div class="wrapper">
<div class="uniform_block">
<img src=" http://t0.gstatic.com/images?q=tbn:ANd9GcSH-kRi3rkVciPcH_c6dDJJI6C1ntzwcKl9MoVQIyuKk8F7unpf" />
</div>
<div class="home_footer">
<div class="footer_contents"></div>
</div>
</div>
</body>
kindly help. My requirement is, when i mouse over the "product menu", the drop down menu should be viewed above the image block which is below the menu bar.
Add position:absolute to the css of your ul menu (in your case, the sub1 class), and remove the width:100% so it can inherit the default width of its parent. Absolute positioning will prevent your browser from trying to put your ul element after the previous element on the page.
ul.sub1 {
position:absolute;
}
http://jsfiddle.net/C2YXp/2/

Resources