DEV Community

Cover image for Scroll to top button with just HTML CSS
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ

Posted on โ€ข Edited on

Scroll to top button with just HTML CSS

Hello dear friends ๐Ÿ‘‹
๐Ÿคž Hope you all doing good in your coding journey.

In this post i have to show a "scroll to top" button which you might have seen on professional and long website pages which help is to go back to top of the page.

Confused ๐Ÿคฏ

Explore this one

scroll to top button with JQUERY

Top comments (28)

Collapse
ย 
calebdre profile image
Caleb Lewis โ€ข

Ah so the scroll-behavior: smooth css on the html tag makes the bowser a bit smarter and prevents a refresh! I was just playing with it in codepen and it works with ids on the page too!

Very clever and useful - thanks! ๐Ÿ™Œ๐Ÿพ

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

yeah its a short invention :)

Actually i am working on a "Insurance" service based website and I want to add this scroll to top button on it, that's why all this efforts :)

by the way, thanks for sharing your feelings in comment. You are a nice guy.
thanks

Collapse
ย 
sirjoe profile image
Ibrahim Joseph โ€ข

same thing that came to mind; using the id on the page too.

Collapse
ย 
francoisaudic profile image
francoisaudic โ€ข

Not accessible for keyboard and screen-reader users, because the focus is not going at the top of the page, with the scroll, and the link hasn't a label that can be rendered by screen-reader to indicate the purpose of the link.

You can take a look at my fork if you want to see some fixes of these issues : codepen.io/Kain314/pen/ExQLLJd

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Oh wow thanks for this feature update, you are a nice. thank you very much.

but I'm still not able to access it without mouse :|

Collapse
ย 
enesislam profile image
enesstr โ€ข

why people afraid of JavaScript? Don't understand..

Collapse
ย 
alvaromontoro profile image
Alvaro Montoro โ€ข

It's not a matter of being afraid of JavaScript. It's using the languages within their domain of action. A redirection is part of the HTML (an anchor), and the scrolling animation is UI (so CSS). Why add JavaScript to do something that shouldn't be handled by JavaScript?

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Exactly ๐Ÿ˜โ˜บ๏ธ๐Ÿคญ

Sometimes we have consider time also when working on a project ๐Ÿ˜Œ.

In my case I have used jQuery because it has its own ready functions ๐Ÿ๐Ÿ๐Ÿ

Thread Thread
ย 
enesislam profile image
enesstr โ€ข

My manโ˜บ๏ธ

Thread Thread
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Your man :)

shy

Collapse
ย 
esogargar profile image
Israa Gargar โ€ข

This is really help me thanks โค๏ธ๐Ÿ˜Š๐Ÿฆพ

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Enjoy

Collapse
ย 
fago12 profile image
Fago12 โ€ข

Hey. I am new. There is also this scroll behaviour using JavaScript, where once you click on a link to another page on the same site, scrolling starts from last position of the previous page to the top. I don't know if anyone understands this. I just want to know what feature that is.

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Hello fago, welcome to our community :)

I'm not able to understand your curiosity, can you please share some screenshot or something as a example. If possible!

Collapse
ย 
iykex profile image
Nana Quame Vybes โ€ข

Simple yet awesome ๐Ÿ‘. However, shouldn't the icon/buttonโฌ†๏ธ appear when the reader scroll to a portion down before showing rather than it being sticky on the post โ‰๏ธ

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข โ€ข Edited

Yes Nana, In this post my main goal is to make a simple "scroll to top" working button in next posts I will try to add that hide show feature also.

Thanks for your priceless time, I appreciate it :)

radhe radhe

Collapse
ย 
alexandev_cwa profile image
Alexander Machic โ€ข

Amazing post, thanks for share this post to learn more ๐Ÿ˜…๐Ÿ‘Œ

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Your welcome Alex ๐Ÿ˜œโ˜บ๏ธ

I always like share my learnings and experiences ๐Ÿšฉ

Collapse
ย 
iansoper profile image
Ian Soper โ€ข

This doesnโ€™t work for me on mobile (Safari, iOS 15.5). Is anyone else getting different results?

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

It's working for me. try different browser.

my it's not compatible in Safari or make sure you have scroll down to bottom then click that green button.

peace :)

Collapse
ย 
ryanguitar profile image
Ryan Els โ€ข

Very cool.
Thanks for sharing.

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Your welcome Ryan,

How you are going to use it?

Collapse
ย 
daniele_pais profile image
Daniele Pais โ€ข

Excellent ๐Ÿ‘๐Ÿ‘๐Ÿ‘

Collapse
ย 
atulcodex profile image
๐Ÿšฉ Atul Prajapati ๐Ÿ‡ฎ๐Ÿ‡ณ โ€ข

Thanks ๐Ÿ™ Daniele