This is a side-navbar file which I made by watching a youtube video and I tweaked some things in it and its working fine but there's a little problem.
Whenever I hover on the profile info box which is on bottom it starts to dance. Why?
let arrow = document.querySelectorAll(".arrow");
for (var i = 0; i < arrow.length; i++) {
arrow[i].addEventListener("click", (e) => {
let arrowParent = e.target.parentElement.parentElement; //selecting main parent of arrow
arrowParent.classList.toggle("showMenu");
});
}
let sidebar = document.querySelector(".sidebar");
let sidebarBtn = document.querySelector(".bx-menu");
console.log(sidebarBtn);
sidebarBtn.addEventListener("click", () => {
sidebar.classList.toggle("close");
});
.sidebar {
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 260px;
z-index: 100;
transition: all 0.5s ease;
backdrop-filter: blur(25px);
box-shadow: 10px 0 15px rgba(0, 0, 0, 0.1);
}
.sidebar.close {
width: 78px;
}
.sidebar .logo-details {
height: 60px;
width: 100%;
display: flex;
align-items: center;
}
.sidebar .logo-details i {
font-size: 30px;
color: #444444;
height: 50px;
min-width: 78px;
text-align: center;
line-height: 50px;
}
.sidebar .logo-details .logo_name {
font-size: 22px;
color: #444444;
font-weight: 600;
transition: 0.3s ease;
transition-delay: 0.1s;
}
.sidebar.close .logo-details .logo_name {
transition-delay: 0s;
opacity: 0;
pointer-events: none;
}
.sidebar .nav-links {
height: 100%;
padding: 30px 0 150px 0;
overflow: auto;
}
.sidebar.close .nav-links {
overflow: visible;
}
.sidebar .nav-links::-webkit-scrollbar {
display: none;
}
.sidebar .nav-links li {
position: relative;
list-style: none;
transition: all 0.4s ease;
}
.sidebar .nav-links li:hover {
backdrop-filter: blur(40px);
box-shadow: 10px 0 15px rgba(0, 0, 0, 0.1);
}
.sidebar .nav-links li .iocn-link {
display: flex;
align-items: center;
justify-content: space-between;
}
.sidebar.close .nav-links li .iocn-link {
display: block
}
.sidebar .nav-links li i {
height: 50px;
min-width: 78px;
text-align: center;
line-height: 50px;
color: #444444;
font-size: 20px;
cursor: pointer;
transition: all 0.3s ease;
}
.sidebar .nav-links li.showMenu i.arrow {
transform: rotate(-180deg);
}
.sidebar.close .nav-links i.arrow {
display: none;
}
.sidebar .nav-links li a {
display: flex;
align-items: center;
text-decoration: none;
}
.sidebar .nav-links li a .link_name {
font-size: 18px;
font-weight: 400;
color: #444444;
transition: all 0.4s ease;
}
.sidebar.close .nav-links li a .link_name {
opacity: 0;
pointer-events: none;
}
.sidebar .nav-links li .sub-menu {
padding: 6px 6px 14px 80px;
margin-top: -10px;
display: none;
backdrop-filter: blur(25px);
box-shadow: 10px 0 15px rgba(0, 0, 0, 0.1);
}
.sidebar .nav-links li.showMenu .sub-menu {
display: block;
}
.sidebar .nav-links li .sub-menu a {
color: #444444;
font-size: 15px;
padding: 5px 0;
white-space: nowrap;
opacity: 0.6;
transition: all 0.3s ease;
}
.sidebar .nav-links li .sub-menu a:hover {
opacity: 1;
}
.sidebar.close .nav-links li .sub-menu {
position: absolute;
left: 100%;
top: -10px;
margin-top: 0;
padding: 10px 20px;
border-radius: 0 6px 6px 0;
opacity: 0;
display: block;
pointer-events: none;
transition: 0s;
}
.sidebar.close .nav-links li:hover .sub-menu {
top: 0;
opacity: 1;
pointer-events: auto;
transition: all 0.4s ease;
}
.sidebar .nav-links li .sub-menu .link_name {
display: none;
}
.sidebar.close .nav-links li .sub-menu .link_name {
font-size: 18px;
opacity: 1;
display: block;
}
.sidebar .nav-links li .sub-menu.blank {
opacity: 1;
pointer-events: auto;
padding: 3px 20px 6px 16px;
opacity: 0;
pointer-events: none;
}
.sidebar .nav-links li:hover .sub-menu.blank {
top: 50%;
transform: translateY(-50%);
}
.sidebar .profile-details {
position: fixed;
bottom: 0;
width: 260px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 0;
transition: all 0.5s ease;
backdrop-filter: blur(60px);
box-shadow: 10px 0 15px rgba(0, 0, 0, 0.1);
}
.sidebar.close .profile-details {
background: none;
}
.sidebar.close .profile-details {
width: 78px;
}
.sidebar .profile-details .profile-content {
display: flex;
align-items: center;
}
.sidebar .profile-details img {
height: 40px;
width: 40px;
object-fit: cover;
border-radius: 16px;
margin: 0 14px 0 12px;
transition: all 0.5s ease;
backdrop-filter: blur(70px);
box-shadow: 10px 0 15px rgba(0, 0, 0, 0.1);
}
.sidebar.close .profile-details img {
padding: 10px;
}
.sidebar .profile-details .profile_name,
.sidebar .profile-details .job {
color: #444444;
font-size: 18px;
font-weight: 500;
white-space: nowrap;
}
.sidebar.close .profile-details i,
.sidebar.close .profile-details .profile_name,
.sidebar.close .profile-details .job {
display: none;
}
.sidebar .profile-details .job {
font-size: 12px;
}
.home-section {
position: relative;
height: 100vh;
left: 260px;
width: calc(100% - 260px);
transition: all 0.5s ease;
background: brown;
opacity: 0.2;
}
.sidebar.close~.home-section {
left: 78px;
width: calc(100% - 78px);
}
.home-section .home-content {
height: 60px;
display: flex;
align-items: center;
}
.home-section .home-content .bx-menu,
.home-section .home-content .text {
color: #11101d;
font-size: 35px;
}
.home-section .home-content .bx-menu {
margin: 0 15px;
cursor: pointer;
}
.home-section .home-content .text {
font-size: 26px;
font-weight: 600;
}
#media (max-width: 420px) {
.sidebar.close .nav-links li .sub-menu {
display: none;
}
}
<div class="sidebar close">
<div class="logo-details">
<i class='bx bx-library'></i>
<span class="logo_name">E-Library</span>
</div>
<ul class="nav-links">
<li>
<a href="#">
<i class='bx bx-grid-alt'></i>
<span class="link_name">Dashboard</span>
</a>
<ul class="sub-menu blank">
<li><a class="link_name" href="#">Category</a></li>
</ul>
</li>
<li>
<div class="iocn-link">
<a href="#">
<i class='bx bx-collection'></i>
<span class="link_name">Category</span>
</a>
<i class='bx bxs-chevron-down arrow'></i>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Category</a></li>
<li>HTML & CSS</li>
<li>JavaScript</li>
<li>PHP & MySQL</li>
</ul>
</li>
<li>
<div class="iocn-link">
<a href="#">
<i class='bx bx-book-alt'></i>
<span class="link_name">Posts</span>
</a>
<i class='bx bxs-chevron-down arrow'></i>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Posts</a></li>
<li>Web Design</li>
<li>Login Form</li>
<li>Card Design</li>
</ul>
</li>
<li>
<a href="#">
<i class='bx bx-pie-chart-alt-2'></i>
<span class="link_name">Analytics</span>
</a>
<ul class="sub-menu blank">
<li><a class="link_name" href="#">Analytics</a></li>
</ul>
</li>
<li>
<a href="#">
<i class='bx bx-line-chart'></i>
<span class="link_name">Chart</span>
</a>
<ul class="sub-menu blank">
<li><a class="link_name" href="#">Chart</a></li>
</ul>
</li>
<li>
<div class="iocn-link">
<a href="#">
<i class='bx bx-plug'></i>
<span class="link_name">Plugins</span>
</a>
<i class='bx bxs-chevron-down arrow'></i>
</div>
<ul class="sub-menu">
<li><a class="link_name" href="#">Plugins</a></li>
<li>UI Face</li>
<li>Pigments</li>
<li>Box Icons</li>
</ul>
</li>
<li>
<a href="#">
<i class='bx bx-compass'></i>
<span class="link_name">Explore</span>
</a>
<ul class="sub-menu blank">
<li><a class="link_name" href="#">Explore</a></li>
</ul>
</li>
<li>
<a href="#">
<i class='bx bx-history'></i>
<span class="link_name">History</span>
</a>
<ul class="sub-menu blank">
<li><a class="link_name" href="#">History</a></li>
</ul>
</li>
<li>
<a href="#">
<i class='bx bx-cog'></i>
<span class="link_name">Settings</span>
</a>
<ul class="sub-menu blank">
<li><a class="link_name" href="#">Settings</a></li>
</ul>
</li>
<li>
<div class="profile-details">
<div class="profile-content">
<img src="image/profile.jpg" alt="profileImg">
</div>
<div class="name-job">
<div class="profile_name">Sam</div>
<div class="job">Student</div>
</div>
<i class='bx bx-log-out'></i>
</div>
</li>
</ul>
</div>
<section class="home-section">
<div class="home-content">
<i class='bx bx-menu'></i>
</div>
</section>
It seems like fixed position is creating problems with the backdrop-filter. Try to replace:
.sidebar .nav-links li:hover{
backdrop-filter: blur(40px);
box-shadow: 10px 0 15px rgba(0,0,0,0.1);
}
with:
.sidebar .nav-links li:hover:not(.sidebar .nav-links li:last-child){
backdrop-filter: blur(40px);
box-shadow: 10px 0 15px rgba(0,0,0,0.1);}
}
I've created a simple dropdown menu that scales and fades in whenever I hover over the parent list item, "Services". Everything works as it should except for when I hover over the child element (dropdown menu) which also makes it appear. I understand that simply hiding it with opacity will not prevent a user from hovering over it but it allows for the 0.4s transition to take effect. If I were to hide it with display:none or visibility:hidden, the transition becomes instant. I'm considering using jQuery to solve this but was wondering if there is a solution based purely in CSS.
JSFiddle
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
</nav>
It's not that you are hovering over the child element, it's that your list elements are so tall from the height:60px that when you hover under the words, it's still going over the li element.
If you are targeting supporting browsers you can use pointer-events to prevent the submenu from causing a hover mouse event
Give your element pointer-events:none to disable
.services-list-container {
pointer-events:none;
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
Then to make sure you can still enter the submenu, reset pointer-events while your .hover elements are hovered
.hover:hover .services-list-container {
pointer-events:all;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
Demo
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
pointer-events:none;
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
pointer-events:all;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
</nav>
A combination of visibility:hidden; with the opacity changes helps you to keep that animation while still preventing it from triggering outside of the intended menu area:
https://jsfiddle.net/4wg4sbqy/
CSS:
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav > ul > li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165,175,185,1);
}
nav > ul > li > a {
padding: 0 20px;
height: 100%;
line-height: 60px;
}
nav > ul > li:hover > a {
color: rgba(105,115,125,1);
}
nav > ul > li:first-of-type {
margin-right: auto;
}
.services-list-container {
position: absolute;
width: 200px;
top: 100%;
height: auto;
opacity: 0;
visibility: hidden;
transform: rotateX(-20deg) scale(0.9,0.9);
transform-origin: 0 0;
transition: transform 0.4s ease, opacity 0.4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
margin-top: 30px;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
padding: 20px;
display: block;
background-color: rgba(235,240,245,1);
}
.hover:hover .services-list-container {
visibility: visible;
transform: rotateX(0deg) scale(1,1);
opacity: 1;
transition: transform 0.4s ease, opacity 0.4s ease;
}
HTML:
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
</nav>
Please check if this is what you want. Submenu is hidden until the hover takes place.
* {
margin: 0;
padding: 0;
font-family: Montserrat;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
nav {
background-color: white;
height: 60px;
display: flex;
justify-content: center;
align-items: center;
}
nav>ul>li {
display: inline-block;
position: relative;
height: 60px;
cursor: pointer;
}
nav a {
color: rgba(165, 175, 185, 1);
}
nav>ul>li>a {
padding: 0 20px;
line-height: 60px;
}
nav>ul>li:hover>a {
color: rgba(105, 115, 125, 1);
}
nav>ul>li:hover .services-list-container {
display: inline-block;
}
nav>ul>li:first-of-type {
margin-right: auto;
}
.services-list-container {
visibility: hidden;
z-index: -1;
position: absolute;
width: 200px;
top: 100%;
opacity: 0;
transition: opacity .4s ease;
}
.services-list-container ul {
background-color: white;
box-shadow: 0 6px 16px rgba(40, 40, 90, 0.15);
position: relative;
}
.services-list-container a {
padding: 20px;
display: block;
}
.services-list-container a:hover {
background-color: rgba(235, 240, 245, 1);
}
.hover:hover .services-list-container {
z-index: 0;
visibility: visible;
opacity: 1;
}
<nav>
<ul>
<li>
[Logo]
</li>
<li>
About
</li>
<li class="hover">
Services
<div class="services-list-container">
<ul>
<li>
Information
</li>
<li>
Technology
</li>
<li>
Training
</li>
<li>
Project Support
</li>
</ul>
</div>
</li>
<li>
News
</li>
<li>
Jobs
</li>
<li>
Contact
</li>
</ul>
I have a ul li list, I am trying to align li to center of the ul.
I have attached my HTML code and CSS.
Important: On li hover I have added some animation (margin-top).
Expected output: Align li to the center of ul without affecting li,
#content-right {
border-top: 3px solid #f1f1f1;
border-bottom: 3px solid #f1f1f1;
width: 100%;
border: 1px solid green;
float: left;
}
#content-right {
float: left;
height: 159px;
}
#partner-icons {
margin: 0 0 0 20px;
padding: 0;
text-align: center;
}
#partner-icons li {
list-style: none;
float: left;
margin: 10px;
text-align: center;
width: 14%;
text-transform: uppercase;
font-size: 0.8em;
height: 50px;
font-weight: 700;
display: inline-block;
}
#partner-icons li figure {
color: #e90e0e;
}
#partner-icons li img {
width: 90%;
margin-bottom: 10px;
}
#partner-icons li figure {
transition: all 0.2s ease;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
}
#partner-icons li:hover figure {
margin-top: 5px;
}
<div id="content-right">
<ul id="partner-icons" class="cf">
<li id="first">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
ABCDEF
</li>
<li id="second">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
GHJIDDK
</li>
<li id="fifth">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
FDSNOISD
</li>
<li id="sixth">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
GDSFDSDSD
</li>
</ul>
</div>
I tried my self to align it to center, I changed the CSS of li (float:none), It's working fine but on hover animation is not working.
Please, can anyone help to resolve this.
Remove float:left and add vertical-align: top; to li, by default inline-block elements are aligned to baseline that's why removing margin affects all li elements.
#content-right {
border-top: 3px solid #f1f1f1;
border-bottom: 3px solid #f1f1f1;
width: 100%;
border: 1px solid green;
float: left;
}
#content-right {
float: left;
height: 159px;
}
#partner-icons {
margin: 0 0 0 20px;
padding: 0;
text-align: center;
}
#partner-icons li {
list-style: none;
/*float: left;*/
margin: 10px;
text-align: center;
width: 14%;
text-transform: uppercase;
font-size: 0.8em;
height: 50px;
font-weight: 700;
display: inline-block;
vertical-align: top;
}
#partner-icons li figure {
color: #e90e0e;
}
#partner-icons li img {
width: 90%;
margin-bottom: 10px;
}
#partner-icons li figure {
transition: all 0.2s ease;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
}
#partner-icons li:hover figure {
margin-top: 5px;
}
<div id="content-right">
<ul id="partner-icons" class="cf">
<li id="first">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
Offers
</li>
<li id="second">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
Fantasy Football
</li>
<li id="fifth">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
Find a Job
</li>
<li id="sixth">
<figure>
<img src="http://icons.iconarchive.com/icons/paomedia/small-n-flat/256/cat-icon.png">
</figure>
Buy Sell
</li>
</ul>
</div>
#menu {
width: 100%;
margin: 10px auto;
}
#menu ul,
#menu li {
margin: 0 auto;
padding: 0 0;
list-style: none
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
}
#menu li a {
border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
color: #444;
background-color: #e5e5e5;
}
#menu input {
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 30px;
opacity: 0;
}
#menu label {
font-size: 20px;
display: none;
width: 35px;
height: 20px;
line-height: 20px;
text-align: center;
color: #77778b
}
#menu label span {
position: absolute;
left: 35px;
}
#menu ul.menus {
height: auto;
overflow: hidden;
width: 180px;
border-top: 1px solid #e5e5e5;
border-left: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
position: absolute;
z-index: 99;
display: none;
left: -1px;
}
#menu ul.menus li {
display: block;
width: 100%;
}
#menu ul.menus a {
color: #77778b;
}
#menu li:hover ul.menus {
display: block
}
<nav id='menu'>
<input type='checkbox' />
<label><i class='icon-reorder' /><span>Menu</span>
</label>
<ul>
<li><a href='/'>Home</a>
</li>
<li><a class='dropdown' href='#'>Menu1</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
</ul>
</li>
<li><a class='dropdown' href='#'>Menu2</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
<li><a href='#'>Submenu3</a>
</li>
</ul>
</li>
</ul>
</nav>
How can I make the Dropdowns Fade in with only CSS? A bit noobish question but still trying to understand CSS Any one have any idea how to make it possible plz suggest it.
Thanks !
One possible solution is to use opacity: 0 instead of display: none and using transition on hover:
#menu {
width: 100%;
margin: 10px auto;
}
#menu ul,
#menu li {
margin: 0 auto;
padding: 0 0;
list-style: none
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
}
#menu li a {
border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
color: #444;
background-color: #e5e5e5;
}
#menu input {
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 30px;
opacity: 0;
}
#menu label {
font-size: 20px;
display: block;
width: 35px;
height: 20px;
line-height: 20px;
text-align: center;
color: #77778b;
visibility: hidden;
}
#menu label span {
position: absolute;
left: 35px;
}
#menu ul.menus {
height: auto;
overflow: hidden;
width: 180px;
border-top: 1px solid #e5e5e5;
border-left: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
position: absolute;
z-index: 99;
display: block;
left: -1px;
opacity: 0;
}
#menu ul.menus li {
display: block;
width: 100%;
}
#menu ul.menus a {
color: #77778b;
}
#menu li:hover ul.menus {
opacity: 1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
<nav id='menu'>
<input type='checkbox' />
<label><i class='icon-reorder' /><span>Menu</span>
</label>
<ul>
<li><a href='/'>Home</a>
</li>
<li><a class='dropdown' href='#'>Menu1</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
</ul>
</li>
<li><a class='dropdown' href='#'>Menu2</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
<li><a href='#'>Submenu3</a>
</li>
</ul>
</li>
</ul>
</nav>
Also you can use transition same way to animate on mouse out:
#menu {
width: 100%;
margin: 10px auto;
}
#menu ul,
#menu li {
margin: 0 auto;
padding: 0 0;
list-style: none
}
#menu ul {
width: 100%;
}
#menu li {
float: left;
display: inline;
position: relative;
}
#menu a {
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
}
#menu li a {
border-right: 1px solid #e5e5e5;
}
#menu li a:hover {
color: #444;
background-color: #e5e5e5;
}
#menu input {
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 30px;
opacity: 0;
}
#menu label {
font-size: 20px;
display: block;
width: 35px;
height: 20px;
line-height: 20px;
text-align: center;
color: #77778b;
visibility: hidden;
}
#menu label span {
position: absolute;
left: 35px;
}
#menu ul.menus {
height: auto;
overflow: hidden;
width: 180px;
border-top: 1px solid #e5e5e5;
border-left: 1px solid #e5e5e5;
border-bottom: 1px solid #e5e5e5;
position: absolute;
z-index: 99;
display: block;
left: -1px;
opacity: 0;
-webkit-transition: opacity 0.5s ease-in-out;
-moz-transition: opacity 0.5s ease-in-out;
-ms-transition: opacity 0.5s ease-in-out;
-o-transition: opacity 0.5s ease-in-out;
transition: opacity 0.5s ease-in-out;
}
#menu ul.menus li {
display: block;
width: 100%;
}
#menu ul.menus a {
color: #77778b;
}
#menu li:hover ul.menus {
opacity: 1;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
<nav id='menu'>
<input type='checkbox' />
<label><i class='icon-reorder' /><span>Menu</span>
</label>
<ul>
<li><a href='/'>Home</a>
</li>
<li><a class='dropdown' href='#'>Menu1</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
</ul>
</li>
<li><a class='dropdown' href='#'>Menu2</a>
<ul class='menus'>
<li><a href='#'>Submenu1</a>
</li>
<li><a href='#'>Submenu2</a>
</li>
<li><a href='#'>Submenu3</a>
</li>
</ul>
</li>
</ul>
</nav>
How can I change my main navigation bar so that when I go to the "About", "From Internet", "By Me" dropdown lists and then hover over a second layer option both the active top link and active second link remain the same color:
Like here this example here, when you go to hyper link 4 in the last sub-option both active options are green at the same time! How can I do that in mine?
body {
background: #DCDCD8;
}
h2 {
text-align: center;
color: #CCC;
}
a {
display: block;
text-decoration: none;
width: 100%;
height: 100%;
color: #999;
}
/* NAVIGATION */
.navigation {
list-style: none;
padding: 0;
width: 250px;
height: 40px;
margin: 0;
background: #F5F5F0;
position: relative;
z-index: 100;
display: inline-block;
vertical-align: top;
left: 5px;
top: 0px;
}
.navigation,
.navigation a.main {
border-radius: 4px;
}
.navigation:hover,
.navigation:hover a.main {
border-radius: 4px 4px 0 0;
}
.navigation a.main {
height: 40px;
font: bold 15px/40px arial, sans-serif;
text-align: center;
text-decoration: none;
color: #FFF;
transition: 0.2s ease-in-out;
position: relative;
z-index: 100;
display: inline-block;
}
.navigation li {
width: 250px;
height: 40px;
background: #F7F7F7;
font: normal 14px/40px arial, sans-serif !important;
color: #999;
text-align: center;
margin: 0;
transform-origin: 50% 0%;
transform: perspective(350px) rotateX(-90deg);
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.05);
}
.navigation li:nth-child(even) {
background: #F5F5F5;
}
.navigation li:nth-child(odd) {
background: #EFEFEF;
}
.navigation li.n1 {
transition: 0.2s linear 0.8s;
}
.navigation li.n2 {
transition: 0.2s linear 0.6s;
}
.navigation li.n3 {
transition: 0.2s linear 0.4s;
}
.navigation li.n4 {
transition: 0.2s linear 0.2s;
}
.navigation li.n5 {
border-radius: 0px 0px 4px 4px;
transition: 0.2s linear 0s;
}
.navigation:hover li {
transform: perspective(350px) rotateX(0deg);
transition: 0.2s linear 0s;
}
.navigation:hover .n2 {
transition-delay: 0.2s;
}
.navigation:hover .n3 {
transition-delay: 0.4s;
}
.navigation:hover .n4 {
transition-delay: 0.6s;
}
.navigation:hover .n5 {
transition-delay: 0.8s;
}
#option:hover,
#blue:hover {
color: white;
background-color: #19A3FF;
}
#option:active {
color: white;
background-color: #1c5f8d;
}
#blue:active {
color: white;
background-color: #1c5f8d;
}
#option {
color: black;
}
#blue {
color: black;
}
.navigation ul {
display: none;
position: relative;
top: -40px;
left: 250px;
list-style-type: none;
margin: 0;
padding: 0;
}
.navigation li:hover ul {
display: block;
}
#nav {
min-width: 1020px;
}
<div style="background:#F5F5F0;">
<div>
<div style="float:left; margin-top:0; left:20px;">
<img class="buzz" src="community-activism.png" width="80" height="80" />
</div>
<h1 style="color:black; opacity:40;" class="shrink"><strong> Social Activism</strong></h1>
</div>
<br />
<div id="nav">
<div id="nav_wrapper" style="margin:0 auto; display:inline-block;">
</div>
<div>
<ul class="navigation">
<a class="main" href="#url" style="color:black;" id="blue">Home</a>
</ul>
<ul class="navigation">
<a class="main" href="#url" style="color:black;" id="blue">About ▼</a>
<li class="n1" id="selection">
Poems ►
<ul>
<li class="n6">
Poem From Internet
</li>
<li class="n7">
Poem By Me
</li>
</ul>
</li>
<li class="n2" id="selection">
Informational Media
</li>
<li class="n3" id="selection">
Visual/Video
</li>
<li class="n4" id="selection">
Photo Essay
</li>
<li class="n5" id="selection">
Me
</li>
</ul>
<ul class="navigation">
<a class="main" href="#url" style="color:black;" id="blue">From Internet ▼ </a>
<li class="n1" id="selection">
Poem
</li>
<li class="n2" id="selection">
Informational Media
</li>
<li class="n3" id="selection">
Visual/Video
</li>
<li class="n4" id="selection">
Photo Essay
</li>
</ul>
<ul class="navigation">
<a class="main" href="#url" style="color:black;" id="blue">By Me ▼</a>
<li class="n1" id="selection">
Poem
</li>
<li class="n2" id="selection">
Informational Media
</li>
<li class="n3" id="selection">
Visual/Video
</li>
<li class="n4" id="selection">
Photo Essay
</li>
</ul>
</div>
</div>
</div>
Your nested list needs to be changed slightly. The only valid element directly inside an unordered list <ul> is the list item element <li> and not <a>
This example is basic and made purely to demonstrate a method to colour the backgrounds.
The Background CSS
The background is applied to the list items child <a> element.
Give a background color to the direct child of the hovered list item (this will apply to all top level list items):
li:hover > a {
background: pink;
}
Give a background to the list items link that is being hovered:
li a:hover {
background: purple;
}
That will give you this (purple item is hovered):
Complete Example
* {
margin: 0;
padding: 0;
}
body {
font-family: arial, sans-serif;
}
a {
text-decoration: none;
color: #000;
font-weight: bold;
display: block;
padding: 5px 10px;
}
ul {
list-style: none;
}
ul li {
display: inline-block;
vertical-align: top;
}
ul li .layer li,
ul li:hover .layer .layer li {
display: none;
}
ul li:hover .layer li,
ul li .layer li:hover .layer li {
display: block;
}
li:hover > a {
background: pink;
}
li a:hover {
background: purple;
}
<ul>
<li>Home</li>
<li>Hover Me
<ul class="layer">
<li>Option</li>
<li>Option</li>
<li>Option</li>
<li>Hover Me
<ul class="layer">
<li>Option</li>
<li>Option</li>
<li>Option</li>
</ul>
</li>
</ul>
</li>
</ul>