secluded/content/posts/adding-a-better-scroll-to-t...

92 lines
2.9 KiB
Markdown
Raw Normal View History

2021-01-11 17:34:38 +00:00
---
title: "Adding A (Better) Scroll To Top Button Without JavaScript"
description: "The title is very self-explanatory"
2021-11-10 08:07:08 +00:00
author: Amolith
2021-01-11 17:34:38 +00:00
cover: /assets/pngs/code.png
date: 2020-05-11T20:39:00-04:00
2023-03-19 23:39:36 +00:00
draft: false
2021-01-11 17:34:38 +00:00
categories:
- Technology
tags:
- 100 Days To Offload
- HTML
- CSS
- Websites
toc: true
---
2021-06-26 20:51:30 +00:00
I'm a fan of using as little JavaScript as feasible on a website and
implementing a scroll-to-top button in JS is just ridiculous.
Nevertheless, there seems to be a plethora of copypasta for it so I
thought I would write about implementing one in pure HTML and CSS. The
title is just a playful poke at [Kev Quirk](https://kevq.uk) who
recently posted about [exactly the same
thing](https://kevq.uk/adding-a-scroll-to-top-button-without-javascript/)
2021-11-10 08:07:08 +00:00
but with different styling :wink:
2021-01-11 17:34:38 +00:00
## HTML
2021-06-26 20:51:30 +00:00
There's only one attribute to add to an existing HTML tag near the top
of your page and a single line for the button itself.
For the attribute, you'll need to use an
[ID.](https://www.w3schools.com/hTML/html_id.asp) When the button is
clicked, the user will be taken to whatever element has this attribute.
For my site, I simply added it to the header at the very top.
2021-01-11 17:34:38 +00:00
2021-06-26 20:51:30 +00:00
``` html
2021-01-11 17:34:38 +00:00
<header class="header" id="top">
```
All that's required for the button is:
2021-06-26 20:51:30 +00:00
``` html
2021-01-11 17:34:38 +00:00
<a href="#top"><button class="top">Top</button></a>
```
## CSS
2021-06-26 20:51:30 +00:00
The basic HTML above is exactly the same as what Kev's article has. The
CSS is where ours will diverge. Having a button at the very bottom of
the page is perfectly fine but I use my site as more than a blog; it's
reasonable to expect visitors to simply search for a link or whatever
else and move on. Having a floating button that stays in the same place
as the user scrolls is a good way to facilitate this.
2021-01-11 17:34:38 +00:00
2021-06-26 20:51:30 +00:00
``` css
2021-01-11 17:34:38 +00:00
.top {
position: fixed;
bottom: 10px;
right: 10px;
max-width: 50px;
max-height: 50px;
width: 100%;
height: 100%;
padding: .5px;
border-radius: 8px;
justify-content: center;
background: #3b3d42;
font-size: 1rem;
font-weight: 800;
text-decoration: none;
cursor: pointer;
}
```
2021-03-22 06:55:30 +00:00
2021-06-26 20:51:30 +00:00
The `position`, `bottom`, and `right` lines are what tell your browser
to render the item in the bottom right. The position is `fixed` so that
means we can put it wherever on the page we want and it will stay there
as the user scrolls. `right` and `bottom` say that the element is to be
positioned 10 pixels above the bottom of the page and 10 pixels from the
right. It's rather hidden on desktop but I'm not expecting desktop users
to click it very often; that's what the `Home` key is for, after all,
and it works across every website. I'm expecting mobile users to make
use of it the most.
2021-01-11 17:34:38 +00:00
---
2021-06-26 20:51:30 +00:00
This was posted as part of
[#100DaysToOffload](https://100daystooffload.com/), an [awesome
idea](https://fosstodon.org/@kev/104053977554016690) from [Kev
Quirk](https://kevq.uk/). If you want to participate, just write
something every day for 100 days and post a link on social media with
the hashtag!