Im fairly new to building a html/css website, but Ive built a template with a Nav with hover effects. It works fine when viewing on desktop or tablet. On mobile I have a hamburger menu icon which when clicked on displays a menu as a vertical list. The top level Menu names display ok, but when hovering over them to show the sub menu items, I cant get the code right to so the space between each Top level menu name is expanded to then fit the sub menu vertical list between. It seems to overlay over the top level menus (see the screenshot Ive attached).Mobile view screencap
I’ve been unable to work this out so any help pointers would be gratefully received.
Ive included the HTML and CSS below so apologies for the length.
index.html
<body>
<!-- Header Section Start -->
<section class="header">
<!-- Navigation Bar Start -->
<nav>
<!-- Website Logo Image Inside Navigation Bar -->
<a href="index.html"><img src="images/logo/world-war-2-logo.webp" alt="World-War-2.org Logo" title="World-War-2.org"> </a>
<!-- Menu Links in Navigation Bar -->
<div class="nav-menu-links" id="navmenulinks">
<!-- Responsive Menu Open Button in Navigation Bar -->
<img class="nav-menu open" src="images/icons/menu/close.webp" onclick="hidemenu()" alt="World-War-2.org menu opened">
<ul>
<li><a class="main-menu-item" href="index.html">Home</a></li>
<li><a class="main-menu-item" href="#">The Road to War</a></li>
<li><a class="main-menu-item-with-submenu" href="#">Eastern Europe</a>
<div class="sub-menu-1">
<ul>
<li><a class="sub-menu-1-item" href="#">Invasion of Poland</a></li>
<li><a class="sub-menu-1-item" href="#">Winter War</a></li>
<li><a class="sub-menu-1-item" href="#">Barbarossa</a></li>
<li><a class="sub-menu-1-item" href="#">Typhoon</a></li>
</ul>
</div>
</li>
<li><a class="main-menu-item-with-submenu" href="#">Western Europe</a>
<div class="sub-menu-1">
<ul>
<li><a class="sub-menu-1-item" href="#">Phoney War</a></li>
<li><a class="sub-menu-1-item" href="#">Denmark and Norway</a></li>
<li><a class="sub-menu-1-item" href="#">Low Countries</a></li>
<li><a class="sub-menu-1-item" href="#">Battle of France</a></li>
</ul>
</div>
</li>
<li><a class="main-menu-item-with-submenu" href="#">Italy and the Balkans</a>
<div class="sub-menu-1">
<ul>
<li><a class="sub-menu-1-item" href="#">Greece Attacked</a></li>
<li><a class="sub-menu-1-item" href="#">Invasion of Yugloslavia</a></li>
<li><a class="sub-menu-1-item" href="#">Greece Overcome</a></li>
</ul>
</div>
</li>
<li><a class="main-menu-item-with-submenu" href="#">North Africa</a>
<div class="sub-menu-1">
<ul>
<li><a class="sub-menu-1-item" href="#">Italy Advances</a></li>
<li><a class="sub-menu-1-item" href="#">British Reposte</a></li>
<li><a class="sub-menu-1-item" href="#">Enter Rommel</a></li>
</ul>
</div>
</li>
<li><a class="main-menu-item-with-submenu" href="#">Battle of the Atlantic</a>
<div class="sub-menu-1">
<ul>
<li><a class="sub-menu-1-item" href="#">Bottled Up</a></li>
<li><a class="sub-menu-1-item" href="#">1st Happy Time</a></li>
<li><a class="sub-menu-1-item" href="#">2nd Happy Time</a></li>
</ul>
</div>
</li>
</ul>
</div>
<!-- Responsive Menu Close Button in Navigation Bar -->
<img class="nav-menu close" src="images/icons/menu/menu.webp" onclick="showmenu()" alt="World-War-2.org menu closed">
</nav>
<!-- Navigation Bar End -->
CSS
* {
margin: 0;
padding: 0;
font-family: "Alumni Sans", sans-serif;
}
/* Header Section */
.header {
min-height: 100vh;
width: 100%;
background-image: linear-gradient(rgba(0,0,0,0.25),#999393),url(images/hero-photo/a-world-at-war-cover-d-1040-l85.webp);
background-position: center;
background-size: cover;
position: relative;
}
/* Navigation Bar */
nav {
display: flex;
padding: 1% 3%;
justify-content: space-between;
align-items: center;
}
/* Website Logo Image Inside Navigation Bar */
nav img{
width: 175px;
}
.nav-menu-links{
flex: 1;
text-align: right;
}
.nav-menu-links ul li{
list-style: none;
display: inline-block;
padding: 8px 12px;
position:relative;
}
.nav-menu-links ul li a {
color: #fff;
text-decoration: none;
font-family: "Amatic SC", sans-serif;
font-size: 20px;
font-style: normal;
font-weight: 700;
}
.nav-menu-links ul li::after{
content: '';
width: 0%;
height: 2px;
background: #f44336;
display: block;
margin: auto;
transition: 0.5s;
}
.nav-menu-links ul li:hover::after{
width: 100%;
}
/* Hide Sub Menu being displayed until Hovered on */
.sub-menu-1{
display: none;
}
/* Display Sub Menu when Hovered on */
.nav-menu-links ul li:hover .sub-menu-1 {
display: block;
position: absolute;
margin-top: 5px;
margin-left: -15px;
width: fit-content;
}
/* Sub Menu1 UL */
.nav-menu-links ul li:hover .sub-menu-1 ul {
margin: 10px;
background: rgba(0,0,0,0.25);
text-align: left;
}
.nav-menu-links ul li:hover .sub-menu-1 ul li {
padding: 10px;
transition: 0.5s;
border-bottom: 1px dashed #fff;
width: fit-content;
white-space: nowrap;
position: relative
}
.nav-menu-links ul li:hover .sub-menu-1 ul li:last-child {
border-bottom: none;
}
.text-box {
width: 90%;
color: #fff;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
text-align: center;
}
.text-box h1 {
font-size: 75px;
font-family: "Amatic SC", sans-serif;
font-weight: 700;
}
.text-box p {
margin: 10px 0 40px;
font-size: 40px;
color: #fff;
font-family: "Amatic SC", sans-serif;
font-weight: 400;
}
.hero-buttons a{
font-size: 20px;
font-family: "Amatic SC", sans-serif;
font-weight: 700;
}
.hero-button1{
width: 150px;
display: inline-block;
text-decoration: none;
color: #fff;
border: 1px solid #fff;
padding: 12px 0px;
margin: 0 3px;
font-size: 13px;
background: transparent;
position: relative;
cursor: pointer;
}
.hero-button1:hover {
border: 1px solid #fff;
background: #f44336;
transition: 0.5s;
}
.hero-button2 {
width: 150px;
display: inline-block;
text-decoration: none;
color: #fff;
border: 1px solid #fff;
padding: 12px 0px;
margin: 0 3px;
font-size: 13px;
background: #f44336;
position: relative;
cursor: pointer;
}
.hero-button2:hover {
border: 1px solid #f44336;
background: #f44336;
transition: 0.5s;
}
.hero-button3 {
width: 150px;
display: inline-block;
text-decoration: none;
color: #fff;
border: 1px solid #fff;
padding: 12px 0px;
margin: 0 3px;
font-size: 13px;
background: transparent;
position: relative;
cursor: pointer;
}
.hero-button3:hover {
border: 1px solid #fff;
background: #f44336;
transition: 0.5s;
}
.quote-text-box {
width: 90%;
color: #fff;
position: absolute;
top: 85%;
left: 50%;
transform: translate(-50%,-50%);
text-align: center;
}
.quote-text-box h1 {
font-size: 25px;
font-family: "Amatic SC", sans-serif;
font-weight: 700;
}
.quote-text-box p {
margin: 10px 0 10px;
font-size: 20px;
color: #fff;
font-family: "Amatic SC", sans-serif;
font-weight: 400;
}
.introduction{
background: #fff;
width: auto;
padding: 5px 5px 5px 5px;
}
.introduction h1 {
font-size: 20px;
font-family: "Amatic SC", sans-serif;
font-weight: 700;
text-align: center;
text-decoration: underline;
}
.introduction p {
font-size: 17px;
color: #000000;
font-family: "Alumni Sans", sans-serif;
font-weight: 400;
text-align: justify;
padding: 5px 5px 5px 5px;
line-height: 24px;
}
.introduction p::first-letter {
font-size: 25px;
font-weight: 400;
}
.bold {
font-weight: 700;
font-size: 17px;
font-family: "Alumni Sans", sans-serif;
}
.red {
font-weight: 400;
color: #f44336;
font-size: 17px;
font-family: "Alumni Sans", sans-serif;
}
.flags {
width: auto;
padding: 5px 0px 5px 0px;
}
.flags-row {
align-items: center;
justify-content: center;
display: flex;
flex-wrap: wrap;
padding: 5px 0px 5px 0px;
}
.flags-column {
box-sizing: border-box;
padding: 0px 5px 0px 5px;
}
.flags-column img {
border: 1px solid #808080;
border-radius: 5px;
}
.medals {
width: auto;
padding: 5px 5px 5px 5px;
}
.medals-row {
display: flex;
align-items: center;
justify-content: space-around;
}
.medals-column{
box-sizing: border-box;
}
.copyright {
background: #808080;
width: auto;
padding: 5px 5px 5px 5px;
margin: auto;
}
.copyright p {
font-size: 17px;
color: #fff;
font-family: "Alumni Sans", sans-serif;
font-weight: 400;
text-align: center;
}
/* Viewport of 700px Max */
@media(max-width: 700px) {
/* Website Logo Image Inside Navigation Bar */
nav img {
width: 125px;
}
.text-box h1 {
font-size: 40px;
}
.text-box p {
font-size: 25px;
}
.nav-menu-links {
position: absolute;
background: rgba(0,0,0,0.75);
height: 100vh;
width: 100%;
top: 0;
right: -100%;
text-align: left;
z-index: 2;
transition: 1s;
white-space: nowrap;
background-color: red;
}
.nav-menu-links ul {
padding: 30px;
}
.nav-menu-links ul li{
display: flex;
}
.nav-menu {
margin: 10px;
cursor: pointer;
width: auto;
}
/* Start Sub Menu Expands down */
.main-menu-item {
}
.main-menu-item {
}
.main-menu-item-with-submenu {
}
.sub-menu-1 {
height: auto;
}
.sub-menu-1-item {
list-style-type: none;
}
/* End Sub Menu Expands down */
.hero-button1 {
width: 100px;
}
.hero-button2 {
width: 100px;
}
.hero-button3 {
width: 100px;
}
.flags-column img {
width: 48px;
height: 29px;
}
.medals-column img{
width: 45px;
height: 88px;
}
}
/* Viewport greater than 701px, but less that 1024px */
@media(min-width: 701px) {
.text-box h1 {
font-size: 55px;
}
.text-box p {
font-size: 32px;
}
/* Hamburger and X buttons hide */
.nav-menu {
display: none;
}
.hero-button1 {
width: 125px;
}
.hero-button2 {
width: 125px;
}
.hero-button3 {
width: 125px;
}
.flags-column img {
width: 60px;
height: 36px;
}
.medals-column img {
width: 80px;
height: 156px;
}
}
/* Viewport greater than 1024px */
@media(min-width: 1024px) {
.text-box h1 {
font-size: 75px;
}
.text-box p {
font-size: 40px;
}
/* Hamburger and X buttons hide */
.nav-menu {
display: none;
}
.hero-button1 {
width: 150px;
}
.hero-button2 {
width: 150px;
}
.hero-button3 {
width: 150px;
}
.flags-column img {
width: 80px;
height: 48px;
}
.medals-column img {
width: 120px;
height: 234px;
}
}
When i hover over a top level menu, I would like the space between each of these to expand and be filled by the sub menu. (hope this makes sense).
Garreth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.