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.
Today we are going to create an animated responsive navbar with html css and javascript. We are going to use flexbox to structure our navbar and css media queries to toggle the burger menu from desktop to mobile.
Rather than having a typical slide out navbar we are going to implement an expanding circle animation using clip-path.
If you are interested in learning/creating a responsive navbar then I highly recommend you downloading the starter files bellow.
Hello there my gorgeous friends on the internet this video we're gonna create a responsive navigation bar
But rather than having that boring block just expand when we're on mobile when you click on the hamburger up here
We are gonna add this liquid motion
Expansion thing to it. Okay. So as you can see here on the screen when we click it
It just expands very nicely and it's liquidy and motiony. I have no idea what i'm talking about. So let's just roll the intro
Just roll it
All right, so what we have here in vs code
I just have an empty index.html a style.css
And we have an app.js
All of them are empty besides the css, which I usually just remove the basic stylings here and i've attached this svg
Which you're gonna find on my github
So let's get going. Uh, we are gonna go to index.html here and we're just gonna add a minimal markup here
It's not it's not too bad. So all we have to do is just create a nav here
And here we're gonna have the hamburger which is gonna actually be invisible
So we're gonna just create three divs here with a class of line and which we can
Duplicate by holding shift alt and just pressing down twice. That's gonna give us three lines and that's it
That's all we need in here
Now this again is gonna be invisible and when we're on mobile, we're gonna just add these styles to it
So that's all we have there and then we have a ul with the actual links to it
To the like about contact and projects so you can do ul dot and then you can add the class name
Which i'm gonna add nav links like so
Hit enter and here we're gonna have an li with an eight anchor tag in here
I'm, just gonna add a hashtag here. It's not gonna take us anywhere for now
I'm gonna add about and i'm gonna duplicate these twice again here. I'm gonna rename this to contact
And down here we're gonna rename this to projects and that's that's everything we need here
Uh, that's all the nav and just to have something interesting on the nav on the
On the page not just the nav bar. We're gonna add just a section here called
Landing, okay with the class of landing and here i'm just gonna have the image of that svg that I have attached
I'm gonna call this dots again. This is just optional. You don't need to do this, but i'm gonna do it
Just so everything looks nice and we can pop this open
with
My other click doesn't work open with live server. I have to change this stupid thing. Okay, here we go
That's all we have and it looks amazing. I'm gonna drop it in here
Like so i'm gonna make it smaller. Actually, i'm gonna make it full screen because we're gonna optimize this for desktop first
So let's go into our style.css and make this thing look half decent
Okay, so all we have to do is let's just grab the nav here
I'm gonna give it well, that's weird. Let's switch to english. I'm gonna add a height of 10 bh to this
So that's going to be the size of our nav bar and i'm just going to add the background
Of and we can actually go to the circle svg and get this one, which is 5b78c7
It's going to be this nice bluish color
So let's take a look and that's what we have there as you can see the links are up there
And that's all we need. So let's also get the nav links
So this is the unordered list on the page and we can display this flex
So it goes horizontally like this. Okay, so they're not going in a column anymore
And actually let's make these visible. So i'm going to change the colors of them. I'm going to get nav links
I'm going to do a nasty selection here
I don't recommend usually you add a class to this as well, but we're going to do this one fast
I'm going to add a color of white so we can see and i'm also going to remove the text decorations and all the other
Things that we usually just don't need so we do that then we're going to do the text decoration
We're going to remove this we're going to say none font size
Let's make the 16 pixels, which I think it's the default. So i'm just repeating myself. Let's get the body here
I'm going to add the font family
And i'm going to change this to sans serif
like so oh
Not font display font family
Let's just do this one because it looks
It doesn't look that bad. Okay, perfect. So as you can see there they are now to remove these dots
From there we can do list style and we can do none and the nav links
Whoops, and that's going to remove us all those dots up there
All right, so we just have the nav links in here and what we can do is we can add a width to these
Let's make it width 50
So this is the unordered list. So just to visualize this i'm going to add a background color of
of light coral
So this is what we have. This is our unordered list here. I'm going to add a height of 100
So it's the same height as our nav because that's what we want. So height 100
Okay, there we go. And now I want these to be spaced around and aligned in the center
So to do that, all you have to do is say justify content space around that's going to do it horizontally
Like so and then we can do align items
Center, let's do it vertically
There we go. Okay, but now I kind of don't really want this to be here
I want these to be up on this side
So, how can we do that? The simplest way we can do that is just to add a margin
left of auto
So when you add auto it's automatically gonna jump it to this side of the page good and now we can remove the light coral
Because we don't need that
Let's get rid of this hit save. There we go. Pop this open looks looks looks wonderful. Okay
Perfect. Let's also just modify this a bit. So it looks half decent
So i'm gonna go back to vs code here and down here. Let's just get the landing
Landing like so i'm gonna add a height of 90
Vh, okay, so we have 10 here and I add 90 here just so it's full screen. So 90 plus 10. I believe it's 100
And then we're gonna do just display flex again here justify content center align items
Center, okay, just aligning everything in the center here. Perfect. And now what we can do is just get the landing
H1 here and just add a margin of like 100 pixels and a font size of 50 pixels make it bigger
And let's also get the color
The upper color here, which is the pink
Is it pink?
Actually, let's see
Hit save. Well, don't hit save
Color and then add this color. Let's see. Yeah, we got this nice pinkish color not pink. It's purple. What's wrong with me today?
Okay, there we go, I always mess up pink and purple
Let me know if you do that as well, or if i'm just very very stupid
Okay, so we have that going
Perfect, and now we can just mess around and make this responsive. So we finish there. Now, how can we make this responsive?
It's super easy. We just go down here. We add a
Round symbol and then say media
Screen and and what we can do here is add parentheses and we can define
The value when this media
Query should take effect. So if I say here max width
I'm going to add a colon and if I say
768 pixels like so and then I just open curly brackets again
Like we normally would if I add body here, I can just get the body and say display none
So basically what happens when I hit the 678 pixel mark, it's going to add display none
To our body and as you can see, it's not working
The reason why that's not working is we need to add the space down here
Okay, so media screen and max width hit safe
And as you can see now when we go all the way down here, it's gonna add display none
Okay, so we don't want to add display none to this
So we're just gonna make this small again like so and we can actually stick our our our preview here so we can visualize everything
Okay, so we want this to take effect around
I don't know around here. Okay
so what we're gonna do is we're gonna get the let's
Let's get the
Nav links first. So we're gonna get the nav links
And what I want to do is add a position fixed to these like so and it's gonna bring it down here as you can see
i'm gonna just add a background
of
We're gonna get the color from up here that bluish color again
So we're just gonna stick the background so we can see what's happening here. So there it is
Okay, and what we're gonna do is add a height of 100 vh
At a width of 100 percent. So it's all full screen like that. Okay. Now this is fixed
So we cannot do anything on the page anymore
Okay, so that's what we got. We're gonna also add a flex direction column to this so it goes down that way
Okay, perfect. So that's what we have for now here
I'm gonna add display none to this just for a bit so we can visualize everything else
So as you can see since we added a position fix to this nav links, we don't have anything up here anymore
So let's add the burgers
So let's go all the way up here or down there either way. We can just get the
the line
All right. So each line
We can add a width of 30 pixels. Let's add a height
of three pixels
A margin, let's actually add just a background white to this and see what we got
Okay, so we just got those three lines up there
But we kind of want to have some space in between them so we can do that by adding a margin
Of five pixels and there we go. We got our nav bar going right there our hamburger. Perfect
let's also get the
Nav here i'm going to add a position relative to this because I want to move this
burger
All the way here in a very specific position
So now if I add position relative to the nav I can get the hamburger
Remember the lines that we have here are actually in the hamburger. Okay
So we can add a position absolute and we can move the whole thing
All right, absolute i'm gonna add a cursor pointer so it looks clickable
Okay, perfect
And then down here we can just add right five percent
Like that it's going to move it that way. Let's add a top of like twenty percent
It's kind of going to move it there and to actually position this better
We can do a transform
translate
minus five percent and
Minus fifty percent. Okay, just position it
Actually, I think it's the other way around
actually, okay, I added minus fifty percent here and
Here I add the twenty so I have to add fifty and then it's perfect
Perfectly going to be in the center. I cannot speak anymore. These hamburgers are making me want to eat and
Okay
Perfect. So that's what we have
Good, let's bring back the nav links here and we are almost done
So what we need to do is to get that beautiful looking circular
animation
is
down here
rather than hiding this
This nav what we can do is we can mess around with something called clip path
So if you don't know what a mask is, don't worry. I'll explain to you
Let's say we have this piece of paper. Okay, and now I want to make a mask of a circle in it
So i'm going to kind of cut out a circle here
All right, and this will be my mask
This is how it looks. Okay, it kind of looks like a circle and what we can do is we can have
This can be our nav. Okay, our nav bar big blue nav bar and if we put
The mask on it only that small green is going to be visible
Okay, and then we can take this and we can expand this mask all the way till it fills up
It fills up it fills up the whole screen after that terrible analogy we can get going so down here
In our nav links, we're going to create that mask
So to do that, we're going to use something called clip path like so clip path
And we can add a circle as you can see up there
Hit that and now what we can do is we can add how big we want this circle to be
So if I add 100 pixels hit save as you can see it's going to be 100 pixels if we add 50
It's only going to show 50 pixels
Okay, but we want to start this off
at
Let's start it off at zero. Okay
But before actually we do that. I actually want to show you what else we can do here
So we have a hundred there and after we can add an at like that at
And we can also define the position we want this to be at so to define the position
We can add percentages just like top left right
Bottom, okay. So to do up we can go 90
Actually, that's going to be right. I apologize
And then we can put it up with this 10. So it's going to go up
However, if I want to make it go more up I can do like something like minus 10
That's going to bring it all the way up here. So it's kind of out of frame of it
Okay, so that's the way we can do that. And actually this
property
Just to warn you doesn't have that great of a support
So to add a bit better support to this make sure please please please make sure you also add this
You're going to do a line web kit
Like so web kit
clip path
Clip path like so okay
so
dash web kit
another line
clip
Line path and then colon and we can just copy everything from up here and paste it down here hit save
Okay, it's gonna basically
Be the same thing. All right, and then we can also add a transition to this because we want to transition it
Uh, we're gonna add a transition all one second and we can do an ease for now
Actually, we can do an ease out
Okay, it's probably gonna make it look cooler. Okay now we can create a
A open class to this so in javascript when we hit this it's going to animate
So let's take a look on how we can do that. We're going to create a nav links
dot
Open like so
Okay, and here i'm just gonna copy
These two properties from up here
So let's hit copy and drag it
and back here
Actually, let's close this up for more space. I don't know why it didn't do that sooner hit paste this sucker and
Let's let's modify this. So now we cannot see what we're modifying here
Because this is going to be added after so let's just try to modify it up here
And then we're going to copy the properties and we're going to change everything back to 100 pixels, so let's do
Let's see 500. Let's see how that looks
And actually we have to modify both of these I believe so look at that 500
If we do 800 now, here's kind of like trial and error and you have to see
At what size it's going to fill up most of your screen because if you go smaller
It's going to probably fill it up more
And if you make it wider it's going to fill it up less
so now
Come on work with me. All right, let's try a thousand
All right that fills it up full screen for us. Let's expand it a bit
perfect
All right. See it's popping out there a bit so you can probably go a bit more but i'm gonna leave it like so
So just experiment with this and you can go bigger or smaller
All right, so we need around 1000
So i'm gonna switch this back to a hundred like we had it before and down here when we add the class
We're gonna add a thousand to both of these. Okay, hit save
Perfect. Let's hit refresh and now what we can do
is
Let's also change these to column. So the landing
Flex direction column. Okay
Okay, perfect, there we go, let's go into our javascript
And add that class
So close everything up here make some space
Back here. What we can do is just get the hamburger hamburger. It's going to set it equal to document a query selector
And we can get the hamburger
All right, we can also get the nav links
It's going to be equal to document the query selector. We're going to do the same things here
Nav links and let's also get the each individual links
const links it's going to be equal to
Document dot query selector all i'm going to select all of them here
I'm going to do nav links like we did up there, but i'm also going to select the li
Okay, so all the allies in this
Okay, let's just add the opening transition to see if it works so we can get the hamburger
We can add an event listener to this
And on click we can just run a arrow function here
What I want to do is I just want to get the nav links
Dot class list i'm gonna toggle a specific class and which class will the open that we just created
So hit save let's see hit and boom. There we go. We're getting a nice nice super nice animation
Oh ed, we have a problem. Where's my hamburger? It's gone. I ate it
Okay, because i'm really hungry
So let's go back to our style.tss and the reason why that is is because this expands right on top of the navbar
So what we can do is we can go up to our hamburger and we can just add a z index of like two
Or something and then it's still gonna be up here. So wow, here we go expand this
Beautifully looking good. Now. Another problem is this
is
Still well, it's not clickable
It should be but as a safety precaution what we can do is we can go to nav links here
and we can add a
pointer event
None, and when we open it, we can add those point pointer events back
Pointer events all there we go. So we don't have any issues with clicking them
Hit open. There we go. Beautiful. Everything is clickable close
And there we go, that's all we have now to also add these to fade in individually we can quickly do that by
Let's just do it this way. We can go below our
our landing here
I'm, just gonna add nav links
Li i'm gonna add opacity zero to all of them. Okay
And now let's also get the nav links li a again super ugly selection
I apologize. I'm gonna change the font size to like 25 pixels. So it's a bit larger and here what we can do
is
I can get the nav links
Li i'm gonna get the ends child
well and
and
I can never write this correctly and child we can get the first one and we can apply a deep a
Different transition to all of them. So i'm gonna get the first one the second one third one
And i'm gonna just change one two three here
What we can do is we can just apply a transition
all 0.5 seconds ease
And we can apply a delay here so I can say 0.2 seconds
Paste it here paste it here and we can do 0.4
Seconds here and 0.6 seconds here. Okay, so we're gonna apply the opacity back
And what we can do is just get the allies here and apply a fade class to them
And just add opacity to one
Okay, this doesn't do anything right now. They're still invisible
But what we can do is back in javascript. We can add this
Fade class to them. So let's just quickly go back here. Super simple
All we have to do is get the links and for each link here in parentheses
We're gonna run an arrow function and what we need to do here is just get the link class list i'm gonna toggle
The fade class and that's that's pretty much it. Let's see open this up. Look at that
We get a super nice fade we can close this back up and there you go
Just like that you can add more personality to your navigation
So thank you again very much for taking the time and hopefully you learned something new
Also, please drop a sub if you enjoy this type of content on this channel and until next time
liquid liquid fluidity