I am just learning to code and was looking to understand and recreate this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_topnav.
EDIT:
I wanted it to work without using the stylesheet from FontAwesome.
The problem is that it works when I run the code snippet here, but not on my computer. It loses format and does this: picture. Does anyone know what I do wrong?
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className = " responsive";
} else {
x.className = "topnav";
}
}
body {
margin: 0;
font-family: ;
width: ;
padding: ;
}
.topnav {
overflow: hidden;
background-color: yellow;
}
.topnav a {
float: left;
display: block;
color: red;
text-align: ;
padding: 14px;
text-decoration: none;
font-size: ;
}
.topnav a:hover {
background-color: orange;
color: white;
}
.topnav a.active {
background-color: turquoise;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
<!DOCTYPE html>
<html lang="nb">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel='stylesheet' href="./css/style.css">
</head>
<header>
</header>
<body>
<div id="myTopnav">
<a href="index.html" >Hjem</a>
<a href="tjenester.html">Tjenester</a>
<a href="kontakt.html">Kontakt</a>
<a href="om-oss.html">Om oss</a>
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</div>
<div>
<h2>Welcome</h2>
<p>Hi</p>
</div>
<script src="./js/javascript.js"></script>
</body>
</html>
Can someone also explain exactly what this does:
<a href="javascript:void(0);" onclick="myFunction()"> <i ></i></a>
CodePudding user response:
I think you are missing FontAwesome. Try adding this to your header
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">