DEV Community

Cover image for Making Responsive Layouts without using Media Queries
Tulsi Prasad
Tulsi Prasad

Posted on • Updated on

Making Responsive Layouts without using Media Queries

Hey everyone! πŸ‘‹ This has been the talk of the hour since grid and flexbox came into the world with their perky aspects of making your content flow responsively around the viewport. πŸ“±

However, it can be frightening too 😨 for those who are starting out on these advanced CSS technologies like grid and flexbox, who may be able to design good-looking layouts 🀩 but not make them responsive-without the need for media queries.

https://i.pinimg.com/originals/63/eb/50/63eb50a853a03196f66adeab41d48467.jpg

πŸ€·β€β™‚οΈ Stuck? Media queries are your savior.

It turns out that writing media queries were like the de facto standard of making responsive design until the era of floats and clearfixes. However, after the launch of grid and flexbox, this hasn't been so. People have come up with ways of not writing lots of media queries and still getting their page awestruck responsive look. πŸ€—

So, shall I stop writing media queries anymore? πŸ‘€ As a matter of fact, no, because media queries will always be there for you if you're stuck somewhere. They are always at your service, but the question is are they really worth writing in that place? If you find no other way to make it, here's your @media query. πŸ’β€β™‚οΈ

🚈 Where to Start?

Well, that's an open question. I won't walk you through building the next Twitter or Facebook in this blog post, rather give you a simple analogy of the things you'll need in order to make things responsive. Mind the words, simple. Yes, you've got this. πŸ’ͺ

https://miro.medium.com/max/1000/1*QBDYhcaeVfJ4bfaqCZZXTw.png

I want you to have a basic knowledge of flex and/or grid for this, or even the idea of what they are or how they work. You can always come back here after having an idea.

πŸ•Έ Let's Get on Grid.

How to set up the grid structure?

We know after writing display: grid, we need to define the rows and/or columns for the layout. As we are making a responsive layout here, which is dynamic as to fit various viewports this can't be something static like in pixels or rems. We need some grid magic here. πŸ’«

https://i.imgur.com/0fzh3Wv.png

P.S.: 1fr means one fraction of the total available space.

Believe it or not! 😱 This above line of code is all it takes to set you up for a basic responsive layout such as the one down below. Let me tell you, you also don't need to define any rows as the grid figures it out automatically. πŸ€– Apart from this, you might wanna add a custom grid-gap to give some beautiful whitespace among them.

ProTip: You can change viewport of codepen, by clicking one of 0.5x, 1x and 2x.

The Original Grid Layout

Of course, there are some additional styling here required to achieve this, but I think grid was the trickiest part. We've just given some styling to all items here to give it a box look! πŸ₯³

πŸ’ Why auto-fit every time?

Huh, it's not much every time, but there is also one property called **auto-fill** which you can use according to your requirements.

https://i.imgur.com/M6PblHQ.gif

Difference between auto-fill and auto-fit

The auto-fill makes more columns of its size when the viewport is increased in width, rather than fitting to the viewport whereas the auto-fit does the opposite. It stretches its items to obtain the full viewport width. So you can now use, any one of them as per your requirements.

Further reading on this down below.

⚑ Let's get to Flex

Flexbox is the one-dimensional containers which can prove quite amazing for layouts that only need flow in one direction. So it definitely fits into our use case.

PS: Although the key here isn't making the whole webpage responsive without using even one media query even if it does make a single section of the page responsive it's worth it.

https://i.imgflip.com/1cxmjl.jpg

✨ The Amazing flex-wrap Property

This is the line, that makes everything as it should be. So, flex-wrap is a property described on flex containers that decide whether the flex items are forced to stay in a single line or can be flown to multiple lines, i.e. responsive style. 🌠

https://i.imgur.com/ETVqh7M.png

Properties of flex-wrap

So, in our case we are going to use flex-wrap: wrap to wrap the items in the flex container to give a shiny little responsive feel to our section. We also use a tiny justify-content: space-around to give a more symmetrical look. 😎

🌈 Can I make responsive layouts now?

Yep. You have learned the concepts behind making awesome layouts. You can start from my codepens above, just fiddle around with the code and then replace the colored blocks with your contents and go slowly. You'll get there. Remember, you always have the @media queries at your disposal, so if you really need them, feel free to use.

πŸ“š Further Reading

If you liked this post, you might love what I share as well on Twitter. Let's catch up! ❀

Discussion (12)

Collapse
vplentinax profile image
Valentiina VP • Edited

I 'm making use of all this right now and it's a wonder 😊 If anyone has trouble understanding auto-fit and auto-fill, they explain it very well on this site. Auto-fit auto-fill explanation

Collapse
leonblade profile image
James Stine

This is a nice flex refresher, but there's not a lot of practical examples here. I recently pushed a design live where I use both flex and grid and media queries are still needed in order to allow for different layouts based on screen size. I know you say here that media queries are still here, but I just wanted to point out that I don't see anyone really eliminating them either unless you're doing really basic layout stuff.

Collapse
heytulsiprasad profile image
Tulsi Prasad Author

I see your point, and you're absolutely right. When designing we might feel like its not a tough nut but while the actual coding there might be a point where it can be frightening, to do everything without media queries. However, even if we're making a small section responsive without media queries (which was never possible in the past), I think that's worth it.

Collapse
leonblade profile image
James Stine

Yes! Thank you for your post as well, it is a great resource!

Collapse
jrc86 profile image
Jonas

I used to dislike working with CSS but I learned flex, still learning, but now it makes more sense.

Collapse
heytulsiprasad profile image
Tulsi Prasad Author • Edited

Haha true though! It goes the same for me as well and after wrapping my head around grid, I finally felt like css is my jam. 😌

Collapse
dar5hak profile image
Darshak Parikh

"wrapping my head"
Pun intended?

Thread Thread
heytulsiprasad profile image
Tulsi Prasad Author

Umm...nah, I just meant it was challenging for me to understand grid, as there are so many properties with it which didn't make any sense at first.

Collapse
helleworld_ profile image
DesirΓ© πŸ‘©β€πŸŽ“πŸ‘©β€πŸ«

Really good, made my understanding deeper, thank you! πŸ™Œ

Collapse
monfernape profile image
Usman Khalil

I really liked your explanation of grid template column. I find grid usually very hard to consume

Collapse
_arunkmr profile image
R.R.Arunkumar

really great !!

Collapse
ufonumo profile image
ufonumo

Thank you for this, really needed this