DEV Community

Cover image for Responsive spacing with ChakraUI (React)
James A.
James A.

Posted on • Edited on

3 2

Responsive spacing with ChakraUI (React)

ChakraUI is a React based user interface library. Basically it helps you build more user friendly interfaces while still opting for optimal functionality. The creators of ChakraUI put in a lot of work to ensure that components built with ChakraUI can be made responsive as possible.

In this article we will be exploring how to make responsive elements/components. We all agree that right spacing makes user interfaces more easy to navigate and also tender to the eyes.

Here we see the power of spacing and how ChakraUI Allows us to be flexible with it.

ChakraUI makes it easy to add spacing to our components. We will now look at some options:

When creating components with Flex / Grid we are giving the option of adding a gap attribute. This attribute typically expects a number value ie: {4} or a string ie: 4rem and also an object which we will see in the code sample below.

We can pass in a singular value for the gap attribute but whatever happened to responsive code?

Referencing image: We can pass in a singular value for the gap attribute but whatever happened to responsive code?

On the left image we have passed in a value for the gap attribute. This works but it isn’t optimal and doesn’t gives us the opportunity to make our components as responsive as we want.

On the right, we are not introduced to a more optimal way of adding spacing to our component. The gap attribute accepts an object that also accepts specific attribute which value will represent spacing values at each breakpoint. Let’s see this more extensively!:

lg: Screens not larger than 60em (Default: 992px). Macbook Pros Laptops

md: Screens not larger than 48em (Default: (768px) ie:Tablets,IPads`

base: This is default for all screen if we do not specify others.

Please read the full documentation of how ChakraUI specifies their spacing here

One more interesting thing is this principle can also be applied to fontsizes, colors, display toggle.

I hope you found this article helpful in some way.
Enjoy the rest of your day!

Best regards.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
marvelcodes profile image
Marvellous

lg: Screens larger than 60em
md: Screens larger than 48em and smaller than 60em
sm: screen larger than 30em and 48em

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay