"Back to Top" button

Hi all. I try to add a small <span> element in order to make a “Back to top” button for long content pages. But after working with most of my pages, I found there’s a situation here: in some of the longer pages, the button would disappear until I scroll down to some point of that page. Am not sure if this is a bug or not, see if anyone can point me a fix or direction here…

My simple code is here:

  1. I add this at top of the page: ^Top
  2. then I add this to the bottom of the page:
<span style="
		  width: 80px;
		  height: 40px;
		  border-radius: 10px 10px 0px 0px;
		  background-color: white;
		  box-shadow: 0px 5px 10px #888888;
		  text-align: center;
		  line-height: 100%;
		  font-weight: bold;
    	  font-size: 14px;
		  color: white;
		  position: fixed;
		  bottom: 0;
		  right: 5%;
		  margin: 0;
">
[[#^Top|TOP]]
</span>
  1. As the 2 GIFs showing below, the “TOP” button would appear normally at the right bottom of the page. But then if I add more content to the page, the “TOP” button wouldn’t show up, until I scroll down to a certain point of that page.

TOP appear
TOP disappear

2 Likes

It’s hard to say without seeing the code used to make this work. Mind sharing that?

Sure! Here’s the code that the “TOP” button works fine:

 ^TOP

# 1 Lorem Ipsum
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."
"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

---

## 2 Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

---

## 3 Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

---

## 4 Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

---




<span style="
		  width: 80px;
		  height: 40px;
		  border-radius: 10px 10px 0px 0px;
		  background-color: white;
		  box-shadow: 0px 5px 10px #888888;
		  text-align: center;
		  line-height: 100%;
		  font-weight: bold;
    	  font-size: 14px;
		  color: white;
		  position: fixed;
		  bottom: 0;
		  right: 5%;
		  margin: 0;
">
[[#^Top|TOP]]
</span>

And here’s when the button would disappear but appear again while scrolling down a bit:

 ^TOP

# 1 Lorem Ipsum
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."
"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

---

## 2 Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

---

## 3 Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

---

## 4 Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

---

## 5 Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

---

## 6 Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

---

## 7 Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

---

### 8 Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).

---

### 9 Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.

---

### 10 Where can I get some?
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.


<span style="
		  width: 80px;
		  height: 40px;
		  border-radius: 10px 10px 0px 0px;
		  background-color: white;
		  box-shadow: 0px 5px 10px #888888;
		  text-align: center;
		  line-height: 100%;
		  font-weight: bold;
    	  font-size: 14px;
		  color: white;
		  position: fixed;
		  bottom: 0;
		  right: 5%;
		  margin: 0;
">
[[#^Top|TOP]]
</span>

What theme are you using? That grey looks :+1:.

If it is a custom CSS, could you please share?

Originally it’s the Warmth theme from the Community themes. But since it got some layout issues therefor I did get help from people in Discord to tweaks some of the css codes… yet since it’s long ago I don’t quite recall which file is changed. You may try applying the Warmth theme and see it yourself. :slight_smile:

interesting. should it be added only to the end of the page or it can be used as a snippet

As @Archie said, add the CSS as a snippet, not inline in the page. See Preferences → Appearance → Snippets.

1 Like

As snippet right? I see… let me try it out.
Thanks guys~~

1 Like

See if I get it right…

I made a BackToTop.css and put that into the snippets folder:

.BackToTop {
  width: 80px;
  height: 40px;
  border-radius: 10px 10px 0px 0px;
  background-color: white;
  box-shadow: 0px 5px 10px #darkgrey;
  text-align: center;
  padding-top: 1.5%;
  line-height: 100%;
  font-weight: bold;
  font-size: 14px;
  color: white;
  position: fixed;
  bottom: 0;
  right: 5%;
  margin: 0;
}

Then I pair the ^TOP and <span class="BackToTop">[[#^Top|TOP]]</span> in the page I want to insert the “Back to top” button. Seems it is working fine.

2 Likes

You got it.

I think you could even do .internal-link[data-href="#^Top]" as the selector, so you don’t need the span tag at all. I also switched the background colour to use a variable so that it should look better in different themes or in light mode:

.internal-link[data-href="#^Top"] {
  width: 80px;
  height: 40px;
  border-radius: 10px 10px 0px 0px;
  background-color: var(--background-primary-alt);
  box-shadow: 0px 5px 10px #darkgrey;
  text-align: center;
  padding-top: 4%;
  line-height: 100%;
  font-weight: bold;
  font-size: 14px;
  color: white;
  position: fixed;
  bottom: 0;
  right: 5%;
  margin: 0;
}

With the above, you can just write [[#^Top|TOP]] anywhere in the document and the CSS will pick it up.

1 Like

Wow!! Thank you sooo much @ryanjamurphy ! Really learnt a lot from here!! :smiley:

2 Likes

Awesome, a little but awesome addition!
I’ve one issue though,
If I click on the new top button, a new window of the same note opens, instead to jump back at the beginning of the current note… Is this expected behavior?

One can also go to the start/end of a note by using the command goStart/goEnd from the following plugin:
c4ctus5/editor-commands-remap: Obsidian plugin to map hotkeys to editor commands

Hi Jopp, if things went well, pressing the top button should bring you directly back to the top on the same page. Unless if you had Pin the current page, then every link you click on the page would being opened as a new windows (including the “top” button, which actually is a link by nature).

If you are familiar using command or hotkeys, maybe the plugin suggested by @Wen would lead you a better user experience as it isn’t rely on hyper-links thus would be more streamline then the tedious setup and limitations of using the “back to top” button. :slight_smile:

Dear all,

another possibility is to:

  • install editor-commands-remap plugin
  • install customizable-page-header-buttons
  • create (manually) a new button “Back to top” to start the command (Go start) from the page header. To do this, it’s necessary to add the following lines to data.json inside customizable-page-header-buttons plugin folder:
{
      "id": "editor-commands-remap:go-start",
      "icon": "double-up-arrow-glyph",
      "name": "Go to start (back to top)",
      "showButtons": "desktop"
}

The main problem so far: the solution only works in Editor Mode, not in Preview Mode. :disappointed_relieved:

@ryanjamurphy i am trying your code, it shows the button but clicking on it doesn’t work and it says there is no such a section. i think i am missing something here but can’t figure out what

Good news: after updating Obsidian to v.0.13.14, the new default editing mode (Live Preview) gives me the best of both worlds. The button works (since it’s editing mode) and I can see results of Dataview queries.

I too tried your code and I can see the button but I have 2 major issues with it:

  1. The button disappears after I scroll down a little ways.
  2. When I click the button it does nothing, but the popout says “unable to find section #^Top”
1 Like