Just starting out with web development and stack overflow, so please bear with me if I struggle to explain the issue.
I came across some JavaScript to make my website responsive on small screens (mobiles). However, I am having trouble centering my top nav bar. Using text-align:center is causing the button (which only appears on small screens) to overlap the nav bar. How can I address this? Thanks in advance!
Here is the code I have so far:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>TVG</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<style>
ul.topnav {
list-style-type: none;
margin: 0 auto;
margin-left: 600px;
padding: 0;
overflow: hidden;
}
ul.topnav li {float: left;}
ul.topnav li a {
display: inline-block;
color: grey;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.topnav li a:hover {background-color: #555;}
ul.topnav li.icon {display: none;}
@media screen and (max-width:680px) {
ul.topnav {
margin: 0 auto;
}
ul.topnav li:not(:first-child) {display: none;}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
@media screen and (max-width:680px) {
ul.topnav.responsive {position: relative;}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
#logodiv {
float:left;
padding-top:16px;
padding-left:45px;
padding-right:7px;
padding-bottom:3px;
margin-left:100px;
}
.break{
clear:both;
padding-top:10px;
padding-left:7px;
}
</style>
<body>
<div id="logodiv">
<img src="images/bbclogo.png" />
</div>
<ul class="topnav mainMenu" id="myTopnav">
<li><a href="#home">Home</a></li>
<li><a href="#news">About</a></li>
<li><a href="#contact">Menu</a></li>
<li><a href="#about">Contact</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</li>
</ul>
<div class="break"> </div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>