Reputation: 33
I'm a bit of a beginner here, and doing exercises on FreeCodeCamp. While working on the Portfolio Site project, I tried adding a Javascript responsive mobile nav to the site from some code at ws3schools.
The nav is showing up fine, but clicking it does nothing. Here's my CodePen, any ideas what I did that's preventing it from popping up the nav links on click? https://codepen.io/colum1225/pen/xLwMJG
Here's the nav bar code:
<!-- HTML -->
<div class="topnav" id="myTopnav">
<h3>Brandon Ray</h3>
<a href="#contact">Contact</a>
<a href="#about">About</a>
<a href="#portfolio">Portfolio</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">☰</a>
</div>
<!-- CSS -->
.topnav {
background-color: #2C8597;
overflow: hidden;
width: 100%;
position: fixed;
z-index: 9;
}
.topnav h3 {
float: left;
color: #f2f2f2;
font-family: sans-serif;
font-size: 1.5em;
padding-left: 30px;
letter-spacing: .04em;
font-weight: 500;
}
.topnav a {
float: right;
display: block;
color: #f2f2f2;
text-align: center;
padding: 27px 30px 27px 30px;
text-decoration: none;
font-size: 1.25em;
font-family: sans-serif;
}
.topnav a:hover {
background-color: #ddd;
color: black;
transition: .5s;
}
.topnav a:active {
background-color: #ddd;
color: black;
}
.topnav .icon {
display: none;
font-size: 1.3em;
}
@media screen and (max-width: 640px) {
.topnav a {display: none;}
.topnav .icon {
float: right;
display: block;
color: white;
margin-right: 30px;
}
}
@media screen and (max-width: 640px) {
topnav.responsive {position: relative;}
topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
topnav.responsive a {
float: none;
display: block;
text-align: right;
}
}
<!-- JS -->
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
Upvotes: 3
Views: 111
Reputation: 48
In your css you need a period before topnav
:
@media screen and (max-width: 640px) {
.topnav.responsive {position: relative;}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: right;
}
}
or you could remove the topnav part altogetther and just have the responsive
part.
Positioning fix:
Change icons position back to relative and put the icon at the top of your nav:
HTML
<div class="topnav" id="myTopnav">
<h3>Brandon Ray</h3>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">☰</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
<a href="#portfolio">Portfolio</a>
</div>
CSS
.topnav.responsive a.icon {
position: relative;
right: 0;
top: 0;
}
Upvotes: 1
Reputation: 4306
Great work so far.
You're missing a dot in-front of your topnav
class selector for your .responsive
rules. See the fixed CSS below,
@media screen and (max-width: 640px) {
.topnav.responsive {position: relative;}
.topnav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: right;
}
}
To fix the overlap of your first mobile navigation dropdown menu item with your menu icon, try adding a margin-top
to the first menu item. A good margin size would be the menu's height of (82px).
This rule would look as follows,
.topnav.responsive a:first-of-type {
margin-top: 82px;
}
Include this rule with your other .topnav.responsive rules (line ~77).
Upvotes: 2