Issue
I have implemented a CSS hamburger icon, and I'm encountering an issue where the navigation items are not displaying when I hover over the button. The icon animation works correctly, but the nav is not visible.
I attempted to resolve this by using the following code:
:is(header:hover, header:focus-within) nav {
display: block;}
While the above modification partially solved the issue, it causes the navigation to be displayed when hovering over the entire header. I need assistance in making the navigation visible only when hovering over the button. How can I achieve this properly?
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--HEADER-BGCOLOR: #333;
--HEADER-COLOR: whitesmoke;
}
/*FONT STYLES*/
html {
font-size: 1.5rem;
font-family: 'Roboto', sans-serif;
}
body {
min-height: 100vh;
display: flex;
flex-flow: column nowrap;
}
header {
background-color: var(--HEADER-BGCOLOR);
color: var(--HEADER-COLOR);
}
.header-title-line {
padding: 0.25rem 0.5rem;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
}
/*BUTTON RESET*/
.menu-button {
background-color: transparent;
border: none;
width: 48px;
height: 48px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
/*HAMBURGER ICON*/
.menu-icon,
.menu-icon::before,
.menu-icon::after {
background-color: var(--HEADER-COLOR);
width: 40px;
height: 5px;
border-radius: 5px;
position: absolute;
transition: all 0.5s;
}
.menu-icon::before,
.menu-icon::after {
content: "";
}
.menu-icon::before {
transform: translate(-20px, -12px);
}
.menu-icon::after {
transform: translate(-20px, 12px);
}
/*HAMBURGER ICON*/
:is(.menu-button:hover,
.menu-button:focus-within) .menu-icon {
background-color: transparent;
transform: rotate(720deg);
}
:is(.menu-button:hover,
.menu-button:focus-within) .menu-icon::before {
transform: translateX(-20px) rotate(45deg);
}
:is(.menu-button:hover,
.menu-button:focus-within) .menu-icon::after {
transform: translateX(-20px) rotate(-45deg);
}
:is(.menu-button:hover,
.menu-button:focus-within) nav {
display: block;
}
nav {
display: none;
background-color: var(--HEADER-BGCOLOR);
transform-origin: top center;
animation: showMenu 0.5s ease-in-out forwards;
}
@keyframes showMenu {
0% {
transform: scaleY(0);
}
80% {
transform: scaleY(1.2);
}
100% {
transform: scaleY(1);
}
}
nav ul {
list-style-type: none;
display: flex;
flex-flow: column nowrap;
}
nav li {
padding: 0.5rem;
border-top: 1px solid var(--HEADER-COLOR);
}
nav a {
display: block;
text-align: center;
width: 80%;
margin: auto;
}
nav a:any-link {
color: var(--HEADER-COLOR);
font-weight: bold;
text-decoration: none;
}
nav a:hover,
nav a:focus {
transform: scale(1.2);
transition: all 0.3s;
}
<header>
<section class="header-title-line">
<h1>PreguiƧa Co.</h1>
<button class="menu-button">
<div class="menu-icon"></div>
</button>
</section>
<nav>
<ul>
<li><a href="Home.html">Home</a></li>
<li><a href="#">Item1</a></li>
<li><a href="#">Item2</a></li>
<li><a href="#">Item3</a></li>
</ul>
</nav>
</header>
Solution
You might use header:has(.menu-button:hover, .menu-button:focus-within) nav
instead:
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
/*RESET*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
:root {
--HEADER-BGCOLOR: #333;
--HEADER-COLOR: whitesmoke;
}
/*FONT STYLES*/
html {
font-size: 1.5rem;
font-family: 'Roboto', sans-serif;
}
body {
min-height: 100vh;
display: flex;
flex-flow: column nowrap;
}
header {
background-color: var(--HEADER-BGCOLOR);
color: var(--HEADER-COLOR);
}
.header-title-line {
padding: 0.25rem 0.5rem;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
}
/*BUTTON RESET*/
.menu-button {
background-color: transparent;
border: none;
width: 48px;
height: 48px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
/*HAMBURGER ICON*/
.menu-icon,
.menu-icon::before,
.menu-icon::after {
background-color: var(--HEADER-COLOR);
width: 40px;
height: 5px;
border-radius: 5px;
position: absolute;
transition: all 0.5s;
}
.menu-icon::before,
.menu-icon::after {
content: "";
}
.menu-icon::before {
transform: translate(-20px, -12px);
}
.menu-icon::after {
transform: translate(-20px, 12px);
}
/*HAMBURGER ICON*/
:is(.menu-button:hover, .menu-button:focus-within) .menu-icon {
background-color: transparent;
transform: rotate(720deg);
}
:is(.menu-button:hover, .menu-button:focus-within) .menu-icon::before {
transform: translateX(-20px) rotate(45deg);
}
:is(.menu-button:hover, .menu-button:focus-within) .menu-icon::after {
transform: translateX(-20px) rotate(-45deg);
}
header:has(.menu-button:hover, .menu-button:focus-within) nav {
display: block;
}
nav {
display: none;
background-color: var(--HEADER-BGCOLOR);
transform-origin: top center;
animation: showMenu 0.5s ease-in-out forwards;
}
@keyframes showMenu {
0% {
transform: scaleY(0);
}
80% {
transform: scaleY(1.2);
}
100% {
transform: scaleY(1);
}
}
nav ul {
list-style-type: none;
display: flex;
flex-flow: column nowrap;
}
nav li{
padding: 0.5rem;
border-top: 1px solid var(--HEADER-COLOR);
}
nav a {
display: block;
text-align: center;
width: 80%;
margin: auto;
}
nav a:any-link {
color: var(--HEADER-COLOR);
font-weight: bold;
text-decoration: none;
}
nav a:hover,
nav a:focus {
transform: scale(1.2);
transition: all 0.3s;
}
<header>
<section class="header-title-line">
<h1>PreguiƧa Co.</h1>
<button class="menu-button">
<div class="menu-icon"></div>
</button>
</section>
<nav>
<ul>
<li><a href="Home.html">Home</a></li>
<li><a href="#">Item1</a></li>
<li><a href="#">Item2</a></li>
<li><a href="#">Item3</a></li>
</ul>
</nav>
</header>
Answered By - Kosh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.