Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3877

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:47:07+00:00 2024-11-26T06:47:07+00:00

Responsive Snakes! And what they can teach you about CSS 🐍

  • 61k

“If a snake was responsive…” is my favorite webdev meme. Have a look below if you're not familiar with this extremely important question.

If a snake was responsive...

I love it, it is so silly. But at the same time makes us think a little deeper about responsive design and CSS techniques.

And that's what this article is about! Let's talk about four responsive snake implementations, see how they were made and what they can teach us.

🚨 There's a CodeSandbox for you to play with at the end

Squishy Snake

Squishy Snake

Squishy Snake is the most basic of our responsive snakes. It uses no media queries at all!

Like all the other snakes, it is made by 3 divs: .tail, .body, and .head. And a wrapper, in this case, .snake-1.

<div class="snake-1">   <div class="tail"></div>   <div class="body"></div>   <div class="head"></div> </div> 
Enter fullscreen mode Exit fullscreen mode

There is quite a lot of CSS responsible for drawing the snake itself. But let's focus only on the rules that make it responsive.

In this first case, I wrote CSS rules that say that the tail width should always be 200 pixels long, that the head should be 100 pixels long, and that the body should fill the rest.

.snake-1 {   display: flex; } .tail {   flex: 0 0 200px; } .body {   flex: 1 0 auto; } .head {   flex: 0 0 100px; } 
Enter fullscreen mode Exit fullscreen mode

You can apply the same technique in real life when your page needs two sidebars and some middle content that should fill the space in between!

Thank you, Squishy Snake 🐍. Next!

Curved Snake

Curved Snake

Curved Snake shares the same HTML structure and some CSS, but things get funky when the screen goes short!

The first thing we do is flip the flex order, so instead of horizontal, each snake part is rendered vertically.

.snake-2 {   align-items: center;   flex-direction: column;   height: 62vh;   justify-content: space-between; } 
Enter fullscreen mode Exit fullscreen mode

Now that flex-direction is vertical, we need to update our snake parts so that they have a fixed height instead of width.

.snake-2 .tail {   flex: 0 0 30px;   width: 100%; } .snake-2 .body {   width: 100%; } .snake-2 .head {   flex: 0 0 30px;   width: 100%; } 
Enter fullscreen mode Exit fullscreen mode

And finally, we link the tail, body, and head with some extra pseudo-elements. These are the green vertical parts that only exist when the screen collapses.

.snake-2::before, .snake-2::after {   content: '';   height: 30vh;   position: absolute;   right: 0;   top: 15px;   width: 30px; } .snake-2::after {   left: 0;   top: 50%; } 
Enter fullscreen mode Exit fullscreen mode

Now for the real-life takeaway:

Changing flex-direction to vertical is a life-saver when you're doing responsive pages.

Just stay aware that flex-direction: column inverts how divs flow inside a flex container, so justify-content becomes vertical and align-items horizontal!

Thanks, Curved Snake 🐍. Onto the next one!

Sliced Snake

Sliced Snake

Sliced Snake is pretty much Curved Snake without the ::before and ::after pseudo-elements.

The other difference is that this one doesn't look so lively.

See, the snake eyes are also pseudo-elements (see the CodeSandbox below). So in order to change its eye, all I needed to do was write this handful of CSS:

.snake-3 .head::before {   content: "βœ–"; } 
Enter fullscreen mode Exit fullscreen mode

Real-life takeaway: If you slice a snake it dies. Don't try dealing with snakes yourself, call your local environmental protection agency!

Thanks, Sliced Snake, you will not be forgotten.

Next!

Scrolly Snake

Scrolly

I love Scrolly Snake. This one is the absolute laziest take on a responsive snake.

Instead of allowing it to overflow off-screen, simply add some width and overflow-w: scroll to it and you're done.

.snake-4 {   width: 70%;   overflow-x: scroll; } 
Enter fullscreen mode Exit fullscreen mode

Now, despite Scrolly Snake looking like a really lazy attempt to responsiveness, it teaches us a great lesson:

There are some times that overflow: scroll is the only solution for a responsive problem.

For instance, imagine that you have a huge table full of columns and need to make it responsive, or a super long string that can't be wrapped.

It is important to know all the tools that you have at hand!

CodeSnakebox

Try editing/playing/breaking the examples below:


Thanks for reading! Can you think of another way to make a snake responsive?

Follow me on Twitter and let me know you liked this article!

And if you really liked it, make sure to share it with your friends, that'll help me a lot πŸ˜„

codenewbiecsshtmlwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.