CREATING A RESPONSIVE BUTTON WITH CSS FLEX
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:
My tablet view:
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.
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.
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.
Now let's see the outcome.
View on larger screes:
View on smaller sreens:
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!😊
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.
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😅
Good idea, that's certainly a great way to learn flexbox.
https://twitter.com/kushyzeena/status/1566085280641294336
The rewards earned on this comment will go directly to the people( @kushyzee ) sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.
Congratulations @charlrific! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
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:
Support the HiveBuzz project. Vote for our proposal!
I wonder why you would use ALL CAPS in the title. All caps text is generally less readable than lower-case text, see https://en.wikipedia.org/wiki/All_caps.
Wow this was helpful.
Thanks a lot!
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!
Thanks a lot 🙏