CREATING A RESPONSIVE BUTTON WITH CSS FLEX

InShot_20220903_103745327.jpg

Hello devs👋! Hope we had a splendid week.

A few days ago I was writing a post on the leofinance dApp with my mobile phone, along the line I had to switch to my tablet to finish it up. When I did I noticed that the way the buttons on the page were displayed on my phone was kind of different from the way they were on my tablet.

My mobile phone view:

Screenshot_20220902-113512_1.png

My tablet view:

Screenshot_20220902-113533_1.png

Now this is actually very normal as webpages are built to be responsive, that is they can automatically adjust theirselves to fit any screen size so as to produce the best view and enhance better user experience.

But it did get me curious to know how it works so I decided to try recreating the "PUBLISH" button with HTML and CSS and I'll be sharing a simple tutorial about how I got it done.

I started off with writing the HTML, I created the container div for the button, then the button element and I nested a png image of the upload icon and another div containing the "PUBLISH" text in the button tag.

Screenshot_20220903-020612_2.png

Then I styled the button to make it appear the way it looked on the page by setting the color , background color, font, height and others. Then I set the with to span 25% of the viewing screen , which is what I'd use to test the responsiveness of the button. The margin was also set to position the botton in the middle of the screen.

Screenshot_20220903-091614_1.png

As for the responsiveness, I realized the easiest and less complicated way to achieve this is by using CSS Flex. So I set the button display to flex which will make the contents nested in it to become flexible.
Then I set the button to wrap, which will make the div containing the the publish text to go below the icon when the space in the button is limited due to a smaller viewing screen. The overflow was also set to hidden and I added some gap, this is to hide the "PUBLISH" text completely when it wraps.

Screenshot_20220903-020315_2.png

Now let's see the outcome.

View on larger screes:

Screenshot_20220903-091540_1.png

View on smaller sreens:

Screenshot_20220903-020813_1.png

And that's how I did it, on larger screens the icon and the text will be displayed side by side but on smaller screens the text is hidden but the functionality of the button is retained.

Thanks for your time and have a nice weekend!😊



0
0
0.000
9 comments
avatar

Very nice! You didn't use any media query? That would have been less complicated and also more responsive but overall I love the approach you took, well done.

0
0
0.000
avatar

Thanks a lot bro!
Yeah I thought of using media query cos that's what I'd have done normally but I'm trying to learn flex box so I thought I should just get a bit practical with it here.
But I think I prefer the flex method tho😅

0
0
0.000
avatar

Good idea, that's certainly a great way to learn flexbox.

0
0
0.000
avatar

Congratulations @charlrific! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 400 upvotes.
Your next target is to reach 500 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Our Hive Power Delegations to the August PUM Winners
Feedback from the September 1st Hive Power Up Day
Hive Power Up Month Challenge 2022-08 - Winners List
Support the HiveBuzz project. Vote for our proposal!
0
0
0.000
avatar
Thank you for sharing this amazing post on HIVE!
  • Your content got selected by our fellow curator @priyanarc & you just received a little thank you via an upvote from our non-profit curation initiative!

  • You will be featured in one of our recurring curation compilations and on our pinterest boards! Both are aiming to offer you a stage to widen your audience within and outside of the DIY scene of hive.

Join the official DIYHub community on HIVE and show us more of your amazing work and feel free to connect with us and other DIYers via our discord server: https://discord.gg/mY5uCfQ !

If you want to support our goal to motivate other DIY/art/music/homesteading/... creators just delegate to us and earn 100% of your curation rewards!

Stay creative & hive on!
0
0
0.000