To maintain the position of the logo, you must adjust the position
of both .topnav
and .topnav.responsive
to absolute
, as shown below:
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
html {
height: 100%;
width: 100%;
}
body {
display: block;
background-color: #000000;
}
.topnav {
display: block;
float: left;
background-color: #000000;
overflow: hidden;
position: absolute; /*updated value*/
}
.topnav a {
float: none;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #fee238;
color: white;
}
section {
background-image: url("https://i.postimg.cc/kGdygTgL/background-image.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
height: 100vh;
display: block;
width: 100%;
top: 50%;
text-align: center;
line-height: 1.2em;
position: relative;
z-index: 2;
font-weight: 500;
color: #fff;
letter-spacing: 5px;
margin: 0px auto;
}
h1 {
line-height: 85%;
text-align: center;
font-size: 40px;
font-family: zeitung;
color: #ffffff;
margin-bottom: 30px;
}
.header_nav {
margin: auto;
width: 60%;
text-align: center;
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-weight: bold;
color: #ffe238;
display: inline;
justify-content: space-evenly;
}
.logo {
display: inline-block;
}
#logo {
width: 25%;
height: auto;
border: 5px solid #000000;
background-color: #000000;
;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
margin: 0 auto;
display: inline-block;
z-index: 2;
}
@media screen and (max-width: 600px) {
.topnav a:not(.icon) {
display: none;
}
.topnav a.icon {
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: absolute; /*was relative*/
}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
display: block;
text-align: left;
}
}
@media screen and (min-width:600px) {
.topnav a {
float: left;
display: block;
}
.topnav .icon {
display: none
}
#logo {
width: 10%
}
}
a:link {
text-decoration: none;
color: #ffffff;
}
a:visited {
text-decoration: none;
color: #ffffff;
}
.current {
text-decoration: underline;
}
ul {
list-style-type: none;
display: inline-flex;
justify-content: space-evenly;
width: 90%;
}
header {
text-align: center;
position: fixed;
height: 75px;
background: #000;
width: 100%;
right: 0;
top: 0;
z-index: 9998;
-webkit-transition: all ease-in 200ms;
-moz-transition: all ease-in 200ms;
-o-transition: all ease-in 200ms;
transition: all ease-in 200ms;
}
.heading {
position: absolute;
width: 300px;
height: 200px;
z-index: 15;
top: 50%;
left: 50%;
margin: -100px 0 0 -150px;
}
<header>
<div class="topnav" id="myTopnav">
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
<a href="index.html" class="active">Home</a>
<a href="menus.html">Our Menus</a>
<a href="about.html">About</a>
</div>
<div class="logo">
<img src="https://i.postimg.cc/d0mpFZ6s/logo.png" alt="" id="logo">
</div>
</header>
<section></section>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
The reason for changing from relative
to absolute
is to prevent the width of .topnav
from affecting the logo's positioning.