First of all I'm new in HTML-CSS. While making this project I am facing a problem in the navbar menu. I was making a dropdown menu. After making the dropdown; whenever I hover on the menu (Panel), it moves to the left. So, I need help. I'm putting my code below. I have added a picture below too. Thanks in advance.
body, html{
height: 100%;
margin: 0;
padding:0;
color: black;
font-family: Arial, Helvetica, sans-serif;
}
a {
color: black;
text-decoration: none;
}
body {
background: url("background0.jpg") no-repeat;
background-size: auto 100%;
background-repeat: no-repeat;
background-position: right top;
}
#background {
position: absolute;
top: 100%;
}
.logo{
float:left;
margin-top: 15px;
margin-left: 50px;
}
nav{
padding: 15px 0px;
width: 100%;
}
ul{
padding: 0;
position: relative;
list-style-type: none;
float: right;
font-family: Arial, Helvetica, sans-serif;
font-weight: 550;
}
ul li {
display: inline-block;
padding: 5px 5px;
color: black;
margin: 0px 0px 0px 1px;
}
.home{
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.home:hover{
background-color: #cce0ff;
}
.events{
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.events:hover{
background-color: #cce0ff;
}
.activities{
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.activities:hover{
background-color: #cce0ff;
}
.contact{
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.contact:hover{
background-color: #cce0ff;
}
.panel{
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.panel:hover{
background-color: #cce0ff;
}
ul li ul {
background: whitesmoke;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: relative;
transition: all 0.5s ease;
margin-top: 2rem;
left: 0;
display: none;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
border-radius: 10px;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel = "icon" type = "image/png" href = "###">
<link rel="stylesheet" href="###.css">
</head>
<body>
<div class="navbar">
<div class="logo">
<a href="index.html">
<img src="logo1.png">
<img src="logo2.gif">
</a>
</div>
<nav>
<ul>
<li><a href="index.html" class="home">Home</a></li>
<li><a href="#" class="events">News & Events</a></li>
<li><a href="#" class="activities">Activities</a></li>
<li><a href="#" class="panel">Panel</a>
<ul class="dropdown">
<li><a href="#">2021</a></li>
<li><a href="#">2020</a></li>
</ul>
</li>
<li><a href="#" class="contact">Contact Us</a></li>
</ul>
</nav>
</div>
<body>
</html>
ul li {
/* add this line for child absolute positioning */
position: relative;
}
ul li ul {
/* change position */
position: absolute;
/* remove margin-top */
/* add top (same as li height so that there is no space between Panel and the ul to preserve hover) */
top: 1.75rem;
}
body, html {
height: 100%;
margin: 0;
padding:0;
color: black;
font-family: Arial, Helvetica, sans-serif;
}
a {
color: black;
text-decoration: none;
}
body {
background: url("background0.jpg") no-repeat;
background-size: auto 100%;
background-repeat: no-repeat;
background-position: right top;
}
#background {
position: absolute;
top: 100%;
}
.logo {
float:left;
margin-top: 15px;
margin-left: 50px;
}
nav {
padding: 15px 0px;
width: 100%;
}
ul {
padding: 0;
position: relative;
list-style-type: none;
float: right;
font-family: Arial, Helvetica, sans-serif;
font-weight: 550;
}
ul li {
display: inline-block;
padding: 5px 5px;
color: black;
margin: 0px 0px 0px 1px;
position: relative;
}
.home {
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.home:hover {
background-color: #cce0ff;
}
.events {
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.events:hover {
background-color: #cce0ff;
}
.activities {
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.activities:hover {
background-color: #cce0ff;
}
.contact {
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.contact:hover {
background-color: #cce0ff;
}
.panel {
padding: 4px 15px;
text-align: center;
cursor: pointer;
outline: none;
background-color: transparent;
border: none;
border-radius: 18px;
}
.panel:hover {
background-color: #cce0ff;
}
ul li ul {
background: whitesmoke;
visibility: hidden;
opacity: 0;
min-width: 5rem;
position: absolute;
transition: all 0.5s ease;
/* margin-top: 2rem; */
left: 0;
top: 1.75rem;
display: none;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
border-radius: 10px;
}
ul li:hover > ul,
ul li ul:hover {
visibility: visible;
opacity: 1;
display: block;
}
ul li ul li {
clear: both;
width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel = "icon" type = "image/png" href = "###">
<link rel="stylesheet" href="###.css">
</head>
<body>
<div class="navbar">
<div class="logo">
<a href="index.html">
<img src="logo1.png">
<img src="logo2.gif">
</a>
</div>
<nav>
<ul>
<li><a href="index.html" class="home">Home</a></li>
<li><a href="#" class="events">News & Events</a></li>
<li><a href="#" class="activities">Activities</a></li>
<li><a href="#" class="panel">Panel</a>
<ul class="dropdown">
<li><a href="#">2021</a></li>
<li><a href="#">2020</a></li>
</ul>
</li>
<li><a href="#" class="contact">Contact Us</a></li>
</ul>
</nav>
</div>
<body>
</html>