My take on making a responsive navigation bar in 2018 with media queries and a sprinkle of animations and javascript. In this tutorial we are going to learn media queries, keyframe animations, basic javascript toggling, building a burger menu and more!
DISCLAIMERS: I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites.
Please watch: "Should You Become A Software Engineer?"
Credit: developedbyed
Hello, what's up everyone? Okay, finally back after like three weeks. Oh my god, it's okay though
Because we are going to build a navigation in a pretty modern way
And i'm going to show you a few tricks that you can do some cool animations in javascript
So we're going to build a simple navigation and we're going to also have a mobile version. Oh, I just spoiled everything there. No
Okay. Anyways, so on mobile it's going to be like this, right?
when you click we're gonna have this nice animation here and all the links are gonna
Nicely animate them like this and this is super customizable
so you can make
like the delay
stronger or faster or slower and
Very very easy and like the animation here. You can easily change it in css
So we're gonna do one cool trick here in javascript. That's gonna be enable us to do this
All right, so without a further ado let's get started, okay, so obviously we're gonna close this
and youtube
Everything. Okay. Good. I'm gonna open up a
My vs code here
fresh
And I think I made a folder here. Let's see navigation tutorial. There we go
All right gonna be quite quite simple so we're gonna start obviously with our index.html
We're gonna generate
a new project
We're gonna name this
navigation
okay with the lowercase n
and
Yeah, let's get started. Okay, so we're gonna say net we're gonna use html5
Uh, and we're gonna see we're gonna build a div with a class of logo
Here is where our logo is gonna go. It's called
the nav
Original okay. And besides this we're gonna have a ul with an li so list right and
Uh, I think I named this home
And in vs code if you hold shift and alt and press down
You can duplicate really easily and nicely so we're gonna name this about we're gonna say work
And our projects
Perfect good and for now, I think we we are done with this. So let's open this up in live server
And the port is already in use
so
And close that there we go, okay, i'm gonna put this here
Actually
I just maximize this
for now
Because we still need to add our style.css
We're gonna link it we're gonna say style and i'm gonna remove some basic
Things here we're gonna remove the padding
and we're gonna say
box size
Border box just so we don't run into some weird issues
Okay, that's it
Now let's start with this nav thing here
So, whoa, what did I press?
Okay good, so we removed all the basic styles
And
We can start so we're gonna say nav, right we're gonna say display flex
so all our
So it comes like next to each other
And then we're gonna say justify content space around
So now it's gonna be like this
You're also gonna say align items center, which is gonna center it vertically. Okay, so justify content is gonna
Well, it's not gonna center it was gonna space space it around
Okay
And we're also gonna add a min height to this. Let's say 8vh
And I also added a background color to this
Give me a second. It's
5d4954
I believe
Yeah, okay. It's that one
I think we're gonna remove this for now just so we can see what we're doing and we're gonna add this back later. Okay?
and I believe we also are using a
We go on google fonts I used
poppins
I said hey, man, let me use let me use something different because i'm using monster art and
railway too often so
I thought i'd switch it up
Try something new my life
Good, so we can just copy and paste this
in there
Get the poppin
Go back to this
And we're just gonna add it here in our nav
There we go works perfectly good
next
Actually, I think we can add back the background color
so
I didn't want to add it just so you can
See clearly where the nav is and where this is, but we're gonna change the color now
Anyway, so i'm gonna say logo. We're gonna say color
white
And i'm actually not gonna keep it white i'm gonna go here just lower it down a bit
Because you don't want to have that huge contrast. It's not gonna look that good
If it's pure white
So i'm just lowering that down
And I also did
Text transform here
and uppercase
So all our text is uppercase
We're gonna say letter spacing five pixels
it does
exactly
What it says so it just makes a bit of space in between each letter
And let's add a font size of 22
Pixelinos good awesome, maybe 20
All right, let's go with 20 good
Now for our ul here we're gonna add a class of nav links here
All right
Now we're gonna go back. I forgot to add this. I'm, sorry, so we're gonna say nav links here
And again, same story. We're gonna say display flex
So it goes like this
And we're gonna add a width to this
Because right now wait, let me show you if I add the background color of red
That's the width right
now
If I want to make this
Like if I add here justify content spaced around
Like nothing's gonna happen, right because it doesn't have any space
So the cool thing is is you can add display flex to this justify content spaced around and then you can just play around
with the width
If I want this more spaced around I can just add 35
Now it's more spaced around
I can go 40
Even more spaced around so you can mess with this quite nicely, which I really like
But for this, let's just add 30. Okay
Now this is looking quite ugly. So we're gonna select all the a tags
Nav links a
We're gonna add the same color that we did up here
Just so we're consistent good
We're gonna get rid of that text
Decoration and we're gonna say none
Good
I believe I also added a letter spacing here of three pixels. Let's see how that looks good
and a font weight
of bold
Good I actually think this is too big
That's what she said font size 14 pixels
Yep, much better
Good
Good. Good. Good. All right. We also need to remove so we're going to say nav links li
Just so we remove these bullet points here. Okay
We're gonna say
What is the bullet? How do you remove that? I always forget list
It's list style
None. Yes. There we go. Awesome. Good. Good. Good. All right, so that's it for our
um
Desktop view quite simple
Yeah, not much not much nothing interesting
Okay, so the way I build my my nav for the mobile
Is I basically do this and then in my nav
I'm gonna add a burger class here. So i'm gonna say burger
Like that i'm gonna add three divs here one
Two three like you can do it with svgs, but you can do it so fast with this that I kind of prefer this one
so
The burger has like three lines, right? So i'm gonna name them line one
line one
I'm, actually gonna just copy this
And we're gonna say line two
Line three
And
Yeah, that's that that's it. That's the whole thing
Good
So now if we go back
It's gonna be here
Now you're gonna be like hey, but this kind of ruins our layout. Well
Not really because we're gonna display it none. So it's only gonna appear in our mobile view. So down here we're gonna say burger
Like this
Actually, we don't need to display none because we don't see anything because our divs are empty
Right. These are empty
So what we're gonna do is select
Burger, i'm going to select all the divs in here
Because they're all going to be the same
I'm gonna give a width of 25 pixels a height of five pixels
You can play around with these values if you don't like the way it looks
Again i'm gonna get this color again, just so we're consistent here good. I'm gonna add a margin
So it has a bit of spacing. All right, it's not working
Let's see, why is it not working burger div div class burger. All right, let's see what I did wrong here
We have the width a height margin
This is the time we try to figure out
What the hell did you do wrong
I see it there
I see the lines there
I just don't okay. So it's not a color. It's a background color
There we go, wow, that looks ugly
That looks really ugly
Let's let's add three pixels here
Oh, there we go. Again. See you can play around with this
Until you like you can do two pixels if you think it's too big. That's what's
Okay
You leave it at three
Good now we're gonna go up here
We're gonna say burger and just say display none
That's it
Looks like this now good awesome
Now we can work on our mobile
So go all the way down here and say media
screen and
Max width is gonna be
678 pixels. There we go
And just to test it out. We're gonna say body display none
I always do this. It's a habit. Okay, so maybe
The width can be bigger here
So before it hits the break point you can maybe increase the width
Right
You can go like 40 until it looks good you can mess around with that
Right here or maybe you can do another break point if you want
So
If you don't want this to be spaced out like this
You want it like this, but when you go smaller a bit
So let's say around here
Around I don't know a thousand something pixels. You can just increase the width of this
So it's gonna look nicer. It's gonna look more spaced out
Actually, let's just do that. Okay, so you see what i'm saying, but this is just a quick fix here
Just so it's gonna look okay
I'm, just gonna take the nav links here
And for this in particular i'm just gonna increase the width
To I don't know 50
60
Uh, let's see
Yeah, so something like that just make sure it looks okay across
Yeah, just play around with that I want to cover mobile
This is like tablet tablet zone
Good. All right, let's continue
So here what we're gonna do
Is we want to modify this?
Because we don't want this to be here
So to do that, actually, I think I can just stick this here
This so this is mobile
Around this, okay
And we're gonna select again the nav links then we select it
And i'm gonna say position
absolute
Wow, okay. There we go
And we're gonna say
I'm gonna put it right
Zero pixels, so it's stuck here on the right side
i'm gonna add a height of
Let's see how much height I added here, I believe it's 8vh so to be full screen i'm gonna say 92vh
Like that
But that's gonna give us full screen
Good and also we're gonna say top
8vh
So it's gonna be right under our nav bar. Good. We're also gonna copy the background
So let's copy this background to see what it actually looks like
Good looks like that perfect and now all we need to do is display
flex
we're gonna say
flex direction
Column so it goes down like that align items
Uh, I don't know let's justify content center
No, it's not it's align items
Because we changed the direction to column so the order changes
Center there we go
So we have a nice
Spaced out nav here good. Awesome
Uh, let's see if I missed something here
You can also mess around with the width
So you can make it smaller and make it bigger i'm gonna keep it at 50
But you can also go full screen if that's that's the thing you want
But I believe I I use 50
And the thing that I showed you at the beginning
Okay. Now we don't want this to be here. So what we can do is transform translate x
100 which is basically gonna move this whole container
off the screen
And perfectly it's gonna move it off the screen so it's here
Now we don't want this to scroll
So all we do is body
And we're gonna say overflow x
hidden
There we go. We cannot scroll anymore good. Awesome
um, let's see
We're gonna say
We also want to
make
The let me just remove this for a second
The original state of these is going to be hidden. So
We're going to say opacity
Zero, and when we click on the burger
It's going to fade in nicely. So for start we're going to just say nav links
Li
Okay, so what that does basically
We cannot see anything good
and also
Let's put this back overflow x
to hidden
and the last step
is
Get that burger
And just say display
Lock
So we took it from display hidden
To
Display block on mobile and look at that
Nicely positioned and everything. This is centered here
And boom remember because it's still in the context of of the nav, which we added display flex
So it's still gonna have the nicely
spaced around
Burger
So
There you go
Awesome another thing we can do is maybe add a cursor to this
So cursor pointer
So boom, there we go
Great
All right. Let's let's we still have like one or two things to do here
So i'm thinking if we should do the javascript or not
Okay. Well, let's think in css for a moment when we click on this burger
We want the navigation to show
So the only thing we need to do is basically take this transform and put it back to zero
So to do that is i'm going to go here to the bottom and say nav active
And we're going to say transform translate
x back to zero
So when we click on this burger the navigation should get the zero transform
Yeah, that's it basically not not nothing else to it
Good and also
We should have a transition
Because we don't want it to boom pop up like that
so let's say
We want to transition the transform
We're going to say 0.5 seconds ease
in
Good awesome
All right, let's leave it at that for now and then we're gonna get to the other ones
Okay, so I want you to create a app.js or name it whatever you want
At the end of your html
We're gonna add it
Good
And now what we're gonna do
Is make a function so we're gonna say const nav slide
Is equal to a anonymous function
awesome
Here we're gonna declare
Uh, we're gonna get the burger and we're gonna get the slider. So we're gonna say burger
Is equal to document dot query selector
And we're gonna say burger
Good and we also want to get the
The nav which is document dot query selector
Nav links
Awesome
So what we're gonna happen is when we click on the burger. We want the nav link to get the class of
Nav active
So let's do that. So we're gonna see nav burger
Add event listener on click
We want to run a function
and what we want to happen is
The nav should
Get a class list. We're gonna say class list
And we're gonna say not add
but toggle
and we're gonna toggle the class list of
Nav active
Let's see how that works
And it doesn't work
Okay, why?
Because we need to invoke
this function
so nav slide
now if you
Now if you're gonna have multiple functions i'd recommend actually building a app
Function here and you can call
All your smaller functions
here
Just to keep it cleaner. But for now, i'm just gonna do like this
We're gonna invoke this function. So now if we click
It slides in nicely good
Now the problem is
How are we gonna animate?
These in one by one
well
First we need to make
An animation
Let's build out an animation
We're gonna go down here we're gonna say add
keyframes
And we're gonna say nav link fade. Okay
From and to so obviously we want from opacity zero to one
And also
I actually want to add a
Little movement to this i'm gonna say 50 pixels and it's gonna go from 50 pixels
To
zero pixels
It's gonna slide and like that nicely good
Now, how do we add this
Well
What we're gonna do
Is we're gonna get all the nav links in this
So to do that
All we have to do is up here
We're gonna say const nav links. So these are the individual links
This one this one this one and this one
So we're gonna say document dot query selector
Query selector all because we want to select all of them. We're going to say nav links
Li like that
Good so this is gonna be the toggle nav
Animate links just to be clear now. The cool thing here is we can do this we can say nav links
Dot for each so for each link we're gonna run a function here
And also the cool thing here is
So these are the individual links right you also have access to index
Which if we console log this is just going to bring back
The individual like how many how many links we have so in this case we have one two, three
One two, three four, right?
Because we have four here
One two, three four awesome. Now, why do we want the index?
well
This way you can actually easily animate
the
The
delay between each
So let me just quickly show you so if I say here link
dot style dot animation
Is equal to we're gonna do backticks here and say nav link fade. Okay the animation we did
0.5 seconds system last four keys
Forwards and i'm going to interpolate it here
with index
And i'm going to divide this by seven or by five. So what does this mean? Let's see
And we're going to say seconds here so this would be the delay i'm going to console log this for you to see
So if we say console log
Index
Divided by seven
It's gonna give us
At 12
It's gonna give us
a nice
Well, what was that noise a nice delay between each
And it's gonna be
equal
So it's gonna look really smooth and it's really nice
And the awesome thing is you can easily add more
You're if you have a designer or someone you can easily add more links here it's going to automatically recognize it
It's going to add it. It's going to add the proper delay to it
and
It just works so you can add more here and it's going to be the same look I can just add more
boom
nice
Delayed between each of them and then you can easily add different
Animations here and it's just going to work
so
Divide by seven if you want it
Like that you can go 0.5. You can mess around with this
And if you want to have an initial delay, you can just add a plus 0.2 or something
So it starts off automatically with the delay and I believe I did this
in the
In my
Original site so we're gonna say 0.3. So we add a basic delay to it. Awesome. Let's see. Does that work?
Refresh let's add a bigger delay. I want to see if this actually works
There we go, it works
Now we have a bit of problem here the problem is
Let's see how much delay I need here, let's go one second
Yeah, so it fades in
After
The nav opens so let's go 1.5 here
Yeah, something like that now the problem is this stays like this we don't want that we want it to
If we close it we want it to happen again
So to do that is quite easy. We just copy this
We're going to say if
link
dot style dot animation
So if this exists if our links have animation on it
Then we're going to say link
That style that animation is going to be equal to nothing else
We're going to add our animation
Boom
No not working
All right, so i'm extremely stupid. The reason why this didn't work is we need to add it in the
Event listener. Oh my god
Just copy everything from here
And paste it in here
Now move this in here
Just to make everything clear
Jesus good
Actually, let's move this back a line
Oh, okay, never mind, here's the first line, let's see boom
Boom there we go
Last thing okay. I also added a background
Here just I just did it so the page is not empty
But the last thing we have to do
Is just change the burger
So to do that is quite simple
We added line one line two line three
So what we do is go here and just add a toggle class to this. So we're going to say toggle
We're going to say line one
We're gonna copy this
Two three
Two oh wait
Two and three
good
And for each line we want something different
So for this one the first one the upper one here, we're gonna say transform rotate
We're gonna say minus 45 degrees
And we're gonna say translate
We're gonna move this minus five pixels
This is the x
and six pixels
on the
y
We're gonna copy this paste it here and here we're just gonna say minus here
And here it's gonna be plus 45 degrees
And for the middle one, let's just get rid of it
Let's say opacity zero
Awesome
All right, so all we have to do is just toggle it in here so what we want is
I'm gonna say let me make sure i'm in this
Yeah here
Burger animation
Good so for this
We're gonna select so when on burger again
So when we click on the burger
we want
What do we want well
We want to add
On the burger class list
A toggle we're gonna say
Oh, I named the toggle, okay, so we're gonna toggle the toggle class
Wow awesome good, let's see
That works the middle one these don't work
Why is that happening? Let's see
so basically
all of these
Should get this let's see what's happening here
Line one
Line two and line three
We're gonna add the toggle
To the line one line two and line three so this effect should take place here
What did I miss what did I miss?
Transform
Rotate well, here's one problem
Here is the second problem
Wait what's going on here?
There we go
So kind of messed up my spacing in there
All right, make sure make sure it's okay
But we also want to add a transition to these so it doesn't look so
so boring
So let's add I think I use let's see how much I used
I used a 0.3 ease on this so try to go and find your
Burger diff here add a transition
Of
all
0.3 seconds ease
And we should be done. So
There we go. Awesome. Okay. So there we go. Look at that. Nice
And the cool thing again is you can add another one of these and you don't have to modify the delay or anything
It's just gonna work magically
Look at that. It's just gonna work so you can also remove let's remove these two
Bam
There we go. Awesome
There we go. That's it guys. Hope you enjoyed this tutorial. Oh, I know this was long as hell
so I apologize for that, but
Yeah, this was quite quite fun to make hope you enjoyed it. And yeah, make sure to suggest anything else you want to see
Uh, yeah, I kind of also want to do some kind of
Vlog type of videos definitely just about life about web dev for sure
and
Yeah, enjoy your day. It's it's hot as hell here. So i'm gonna go take a shower. Okay
peace