Chakra UI Crash Course #5 – Responsive Styles

In this lesson we’ll take a look at how to apply some responsive styles in Chakra UI.

⭐⭐ Watch the whole course now (without ads) on Net Ninja Pro:
https://netninja.dev/p/chakra-ui-crash-course

🐱‍💻 Access the course files on GitHub:
Course files –

🐱‍💻 React Tutorial:
On Net Ninja Pro –
On YouTube –
🐱‍💻 React Router in Depth Tutorial:
On Net Ninja Pro –
On YouTube –

🐱‍💻 CSS Flexbox Tutorial:
On YouTube –

🐱‍💻 CSS Grid Tutorial:
On Net Ninja Pro –
On YouTube –

🐱‍💻 Chakra docs –
🐱‍💻 VS Code –


Okay then gang so in the last lesson we Saw how to make a grid layout using two Different components the simple grid Component which is what we see here on The dashboard page with all these little White boxes and also the grid component Which was used for the two sections of Content that we have on the page the Left sidebar and then the content and Now that on the right now out of the box The simple grid component this one right Here is pretty responsive so on smaller Screens it adapts the width of the Elements in each row and that's really Nice but the other grid component the One that we use for the sidebar and the Main content over here doesn't do that Out of the box so we need to implement Some responsiveness to this manually and We can do that by using some of chakra's Built-in break points and its responsive Style system likewise we can style other Elements not just grid components Differently for different screen sizes So let's give this a whirl So I'm just on the chakra docs looking At the responsive Styles and if we Scroll down here we can see all the Break points that chakra comes with so We have small medium large XL and 2XL And also before small we have something Called a base kind of point which is Zero width basically so if you want Something to apply from zero width all

The way up you would use that base Property so these are the different Properties we can use to style things Differently for these screen sizes you Can change the values of these by Editing or updating the theme and I'm Going to show you how we can extend or Update the theme later on in the course For now let me show you how we use these Different properties if you scroll right Down here We can see that we're applying to this Text component different font sizes at Different screen sizes so the base one From zero width is 24 pixels but then as The screen gets larger to a medium Screen it goes to 40 pixels when it gets Larger again to a large screen it goes To 56 pixels so that's how we change Properties based on these different Break points and it's mobile first we Start with the base and we work our way Up okay Cool so let's try this out in our Project so I want to start off with a Simple example inside the dashboard so Let me do a text component click on this To import it at the top and we will save For the text hello so that's in the First grid item right here we should see It in the top left which we do now I Want that to be a different color for Different screen sizes so the way we do This is by say color is equal to

Something now we need a dynamic property So curly braces and the property value Is an object so two sets of curly braces The first step because we're using a Dynamic value and the second set which Is an object okay so now we can say for Base screen sizes so that's zero pixels And up we want this to be pink now if I Saved it and left it there this means That we want to color this pink for very Small screen sizes zero pixels in width All the way up to the largest screen Size it will always be pink so we can See it's pink at this screen size and up And if we get smaller it's always pink I'll okay Now then I also want to add on the Medium property to say at medium sized Greens and up it's going to be blue Color and then for large size screens And up it should be green so now if we Go back over here You can see if we go to the small screen Size is pink when we get to a medium Sized screen it goes to blue and then When it gets to large it goes to Green Okay so that's how we can apply these Different styles at different break Points like this so now we know the Basics let's go to the root layout and Let's apply some responsive styles to These grid items so that we can change How many columns in width they take up At different screen sizes

So then for call span for the first one Instead I will use an object as a value And then we'll say at base it's going to Be six columns in width I'll explain This in a second for large screens two Columns in width the extra large and up It's going to be one column in width so That means when we have things like Mobile screens it's going to be full Width because we have six columns in the Grid and this grid item is taking up all Six columns when we get to large screens It's going to take up two columns in Width so it's going to be like a sidebar Again for extra large where we have a Bit more room we only need one column Okay So I'm going to copy this and I'm going To paste it down here but change the Values so this call span for the second Grid item which is all the main content And the navbot and the navbar sorry is Going to be 6 for the base size so They're going to stack on top of each Other this takes up the full width and This takes up the full width and then For large screens it's going to be four Because we have two over here plus four Is six but extra large it's going to be Five because one plus five is six okay Now I also want to apply some responsive Styles for the padding so object and We'll say base is going to be 20 pixels And then also when we get to large

Screens we're going to increase that to Be 30 pixels So the padding is larger for larger Screen sizes so let me save this and we Can see now When we're in a medium-sized screen it's Taking up those two columns in width When we get to I think that was actually large yeah so When we're at Large Size screens it's Two columns in width at extra large it's One it gets smaller when we go right to Tiny screen sizes now this takes up 100 In width and so does this okay so that My friends is now all working and that's How we can apply responsive styles to Different components now before we go on I have just noticed a typo right here it Shouldn't be HV it should be pH so if we Save that now you're going to see Something happen and this is taking up 100 VH height so it's taking up the full Height of this browser as we load it Onto the screen now that's fine when we Have these larger screen sizes because That's what we wanted because even if we Were to comment out all of this right Here All of this content which is bringing The grid height up then it's still going To be full height and in fact no we Can't do that because we need some kind Of content so let me just do a div right Here just so we have some content save

It it now takes up the full height right Here okay and if we didn't have this Height right here if it was just like Taken away let's save it then this Height would only be whatever the Content dictates it to be so we need That in however If we just undo what we did here Because we're saying that the Min height Is always this even for small screens When we get down to small screens it's Still taking up that massive height so This needs to be responsive as well so What I will do is say that this only Kicks in at a certain break point now we Don't need the base break point right Here because the height is going to be Automatic right here so we just say when We get to large screens and up then I Want you to apply a height or a mid Height of 100 VH like so okay so save That and now you can see we have that Smaller height for smaller screens when We get to larger screens it's going to Be 100 VH again awesome

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *