I want to add a Top button in the website page footer that takes a user back to the top. Can anyone please help me with what to add to a button to do that? I will appreciate that. Thank you!
My personal recommendation is to create seperate files like back-to-top.js and back-to-top.css in the corresponding folders in your theme. Add the back-to-top.js below by adding
in your layout.html (or base.html) which should be located in templates/layout folder.
For the back-to-top.css, add it to your main.css by using
{% include 'path/to/back-to-top.css' %}
Last but not least, add a
<a href="#" class="back-to-top"></a>
to your layout.html (or base.html).
Another option would be to create a custom module with similar code and add the custom module to the layout.html (or base.html; what ever it's called in your case)
best,
Anton
p.s.: Feel free to modify the css with some theme variables
as Interactive is a marketplace, you will need to create a child-theme first in order to be able to modify/extend it.
Have you created a child theme of the interactive theme in the past and are you using it?
If not, it will be a bit more work, but only a few clicks 🙂
Continue here, if you haven't created a child-theme yet:
Go to your Design Manager, find and open the "Interactive"
folder.
Once opened, it should look something like this:
Click on File -> Create Child theme
Put your individual information into the popup fields.
all names are completely up to you, but note that they'll be visible in the source code.
If you want to use several words, I recommend to replace "space" with a dash("-") or underscore("_") or just combine them like "thisIsMyCustomTheme". Spaces are not great in source code 😀
After clicking the "Create Child theme" button, you will see your child-theme setup.
Contiune here if you have created a child-theme before:
Click on File and create a new folder called "js" (optional but recommended).
If the folder shouldn't appear, try reloading the page.
After you see the folder, right click it and select New file in "js"
In the popup, select "Javascript"
in the next step, give it the name "back-to-top"
Click "Create"
Paste the first code block I've shared in my previous reply into the file.
It should look like this
Feel free to modify the code to your needs.
Line 4:
backToTopButton.innerHTML = '↑';
// replace the ↑ with anything you'd like to display as the button text. Can be an icon, text, emoji... Important: keep the quotes
Line 5:
backToTopButton.className = 'back-to-top';
// do not change unless you want to modify everything in the CSS
Line 6:
backToTopButton.setAttribute('aria-label', 'Zurück nach oben');
// replace the 'Zurück nach oben' to something like 'Click here to get back to top'. This is for accessibility/screen-readers
Line 12:
if (window.pageYOffset > 100) {
// replace the 100 with a number you'd like the button to appear. This is the scroll distance. In this example the button will appear once the user scrolls for more than 100px. Usually this number is set to something between 100 and 800
Click the Publish button in the right corner.
Next, create a new folder "css".
Make sure it's on the same level as js and not a child folder. (You can right-click on your theme name in the top of the left sidebar and select "create folder")
Right-click the folder, create a new file and select "CSS + HubL". Give it a name.
Paste the second code block from my previous reply into it.
It should look like this:
Modify it to your requirements.
You can use the theme variables for styling and replace all hardcoded values to automatically meet your theme settings.
This can be a bit fiddly, but you can check the theme-overrides.css from the original Interactive theme and copy some of the button settings over to it.
If you don't want to deal with it, you can simply replace the hardcoded values, with your values for background-color, color, padding, border....
Once happy, publish this file.
Last but not least, open the base.html of your child-theme.
Copy line 22 and replace line 26 with it.
Right-click on the back-to-top.css file and select "copy public URL".
select everything in between the double quotes in line 26 and hit paste(CTRL/CMD+V).
The final step is to open every page that the button should be visible on and changed from the interactive theme to your child-theme. (This is the main reason I always recommend to create a child-theme before even start building the first page)
Here is the solution I found, and it works well. I added this code directly to the website's footer HTML. I hope this helps others. BTW, I can't expand this text window.
Adding a "Back to Top" Button to Every Page on HubSpot
This guide provides the complete HTML, CSS, and JavaScript code needed to implement a fixed, scrolling "Back to Top" arrow button on your HubSpot website using the Interactive theme. Placing this code in your Site Footer HTML will appear on all pages, blog posts, and landing pages.
1. The Code Block
Copy the entire block of code below. This single script contains the button element, its styling (CSS), and the functionality (JavaScript) to handle scrolling and showing/hiding the button.
Note: The styling uses a fixed position in the bottom-right corner.
Code in Footer
<!-- START: Back to Top Button Implementation -->
<style>
#backToTopBtn {
display: none; /* Hidden by default */
position: fixed; /* Fixed position */
bottom: 20px; /* Place the button 20px from the bottom */
right: 30px; /* Place the button 30px from the right */
z-index: 99; /* Make sure it stays on top of other elements */
border: none; /* Remove border */
outline: none; /* Remove outline */
background-color: black; /* Button background color */
color: #6cca0b; /* Updated to the new bright green arrow color */
cursor: pointer; /* Add a mouse pointer on hover */
padding: 15px; /* Some padding */
border-radius: 50%; /* Make it round */
font-size: 25px; /* Increased font size for a slightly larger arrow */
// Function to scroll the document to the top smoothly
function scrollToTop() {
// Use smooth scrolling if supported, otherwise fall back to immediate scroll
window.scrollTo({
top: 0,
behavior: "smooth"
});
}
// When the user scrolls down 300px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
// Check both documentElement (for most browsers) and body (for older ones)
if (document.body.scrollTop > 300 || document.documentElement.scrollTop > 300) {
backToTopBtn.style.display = "block";
} else {
backToTopBtn.style.display = "none";
}
}
// Attach the globally defined function to the window object so it's accessible from the onclick attribute window.scrollToTop = scrollToTop;
</script>
<!-- END: Back to Top Button Implementation -->
End Code in Footer
2. Implementation Steps in HubSpot
Navigate to Settings: In your HubSpot portal, click the Settings icon (gear) in the main navigation bar.
Go to Website > Pages: In the left sidebar navigation, expand the "Website" section and click on Pages.
Find the Templates Tab: At the top of the screen, click the Templates tab.
Edit Site Footer HTML: Scroll down to the Site Footer HTML section.
Paste the Code: Paste the entire code block you copied from Step 1 into the text area of the Site Footer HTML section.
Save Changes: Scroll to the bottom and click the Save button.
Why the Footer HTML?
Placing this code in the Site Footer HTML ensures that it is loaded right before the closing </body> tag on every single page served by HubSpot (including all blog posts and landing pages associated with your chosen domain), making it the perfect global solution for the Interactive theme.
3. Testing and Customization
Test: Go to any page on your live website, scroll down past the top section, and the circular black button with the bright green (#6cca0b) carrot arrow should appear in the bottom right corner. Clicking it should smoothly scroll you back to the top.
Customization: If you want to change the look, you can easily modify the CSS within the <style> tags in the code block.
Future Customization Instructions
To adjust the three elements look for the following lines within the code block in your Site Footer HTML:
Feature to Change
Section in Code
Property/Value to Edit
Description
Arrow/Carrot Character
<button> HTML Element
▲
Change this HTML entity to a different symbol. Common alternatives are: ^ (simple caret), ⇑ (double up arrow), or ⇧ (the original thick arrow).
Arrow/Carrot Color
#backToTopBtn CSS
color: #6cca0b;
Replace #6cca0b with any desired hex code or color name (e.g., red, blue).
Arrow/Carrot Size
#backToTopBtn CSS
font-size: 30px;
You can increase or decrease the pixel value (30px) to change the size of the arrow symbol inside the button.
Anton, I am not a developer, so I don't know how to do this. Is there a step-by-step instruction on how I can create this? I can follow instructions. I am using the Interactive theme.
as Interactive is a marketplace, you will need to create a child-theme first in order to be able to modify/extend it.
Have you created a child theme of the interactive theme in the past and are you using it?
If not, it will be a bit more work, but only a few clicks 🙂
Continue here, if you haven't created a child-theme yet:
Go to your Design Manager, find and open the "Interactive"
folder.
Once opened, it should look something like this:
Click on File -> Create Child theme
Put your individual information into the popup fields.
all names are completely up to you, but note that they'll be visible in the source code.
If you want to use several words, I recommend to replace "space" with a dash("-") or underscore("_") or just combine them like "thisIsMyCustomTheme". Spaces are not great in source code 😀
After clicking the "Create Child theme" button, you will see your child-theme setup.
Contiune here if you have created a child-theme before:
Click on File and create a new folder called "js" (optional but recommended).
If the folder shouldn't appear, try reloading the page.
After you see the folder, right click it and select New file in "js"
In the popup, select "Javascript"
in the next step, give it the name "back-to-top"
Click "Create"
Paste the first code block I've shared in my previous reply into the file.
It should look like this
Feel free to modify the code to your needs.
Line 4:
backToTopButton.innerHTML = '↑';
// replace the ↑ with anything you'd like to display as the button text. Can be an icon, text, emoji... Important: keep the quotes
Line 5:
backToTopButton.className = 'back-to-top';
// do not change unless you want to modify everything in the CSS
Line 6:
backToTopButton.setAttribute('aria-label', 'Zurück nach oben');
// replace the 'Zurück nach oben' to something like 'Click here to get back to top'. This is for accessibility/screen-readers
Line 12:
if (window.pageYOffset > 100) {
// replace the 100 with a number you'd like the button to appear. This is the scroll distance. In this example the button will appear once the user scrolls for more than 100px. Usually this number is set to something between 100 and 800
Click the Publish button in the right corner.
Next, create a new folder "css".
Make sure it's on the same level as js and not a child folder. (You can right-click on your theme name in the top of the left sidebar and select "create folder")
Right-click the folder, create a new file and select "CSS + HubL". Give it a name.
Paste the second code block from my previous reply into it.
It should look like this:
Modify it to your requirements.
You can use the theme variables for styling and replace all hardcoded values to automatically meet your theme settings.
This can be a bit fiddly, but you can check the theme-overrides.css from the original Interactive theme and copy some of the button settings over to it.
If you don't want to deal with it, you can simply replace the hardcoded values, with your values for background-color, color, padding, border....
Once happy, publish this file.
Last but not least, open the base.html of your child-theme.
Copy line 22 and replace line 26 with it.
Right-click on the back-to-top.css file and select "copy public URL".
select everything in between the double quotes in line 26 and hit paste(CTRL/CMD+V).
The final step is to open every page that the button should be visible on and changed from the interactive theme to your child-theme. (This is the main reason I always recommend to create a child-theme before even start building the first page)
Here is the solution I found, and it works well. I added this code directly to the website's footer HTML. I hope this helps others. BTW, I can't expand this text window.
Adding a "Back to Top" Button to Every Page on HubSpot
This guide provides the complete HTML, CSS, and JavaScript code needed to implement a fixed, scrolling "Back to Top" arrow button on your HubSpot website using the Interactive theme. Placing this code in your Site Footer HTML will appear on all pages, blog posts, and landing pages.
1. The Code Block
Copy the entire block of code below. This single script contains the button element, its styling (CSS), and the functionality (JavaScript) to handle scrolling and showing/hiding the button.
Note: The styling uses a fixed position in the bottom-right corner.
Code in Footer
<!-- START: Back to Top Button Implementation -->
<style>
#backToTopBtn {
display: none; /* Hidden by default */
position: fixed; /* Fixed position */
bottom: 20px; /* Place the button 20px from the bottom */
right: 30px; /* Place the button 30px from the right */
z-index: 99; /* Make sure it stays on top of other elements */
border: none; /* Remove border */
outline: none; /* Remove outline */
background-color: black; /* Button background color */
color: #6cca0b; /* Updated to the new bright green arrow color */
cursor: pointer; /* Add a mouse pointer on hover */
padding: 15px; /* Some padding */
border-radius: 50%; /* Make it round */
font-size: 25px; /* Increased font size for a slightly larger arrow */
// Function to scroll the document to the top smoothly
function scrollToTop() {
// Use smooth scrolling if supported, otherwise fall back to immediate scroll
window.scrollTo({
top: 0,
behavior: "smooth"
});
}
// When the user scrolls down 300px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
// Check both documentElement (for most browsers) and body (for older ones)
if (document.body.scrollTop > 300 || document.documentElement.scrollTop > 300) {
backToTopBtn.style.display = "block";
} else {
backToTopBtn.style.display = "none";
}
}
// Attach the globally defined function to the window object so it's accessible from the onclick attribute window.scrollToTop = scrollToTop;
</script>
<!-- END: Back to Top Button Implementation -->
End Code in Footer
2. Implementation Steps in HubSpot
Navigate to Settings: In your HubSpot portal, click the Settings icon (gear) in the main navigation bar.
Go to Website > Pages: In the left sidebar navigation, expand the "Website" section and click on Pages.
Find the Templates Tab: At the top of the screen, click the Templates tab.
Edit Site Footer HTML: Scroll down to the Site Footer HTML section.
Paste the Code: Paste the entire code block you copied from Step 1 into the text area of the Site Footer HTML section.
Save Changes: Scroll to the bottom and click the Save button.
Why the Footer HTML?
Placing this code in the Site Footer HTML ensures that it is loaded right before the closing </body> tag on every single page served by HubSpot (including all blog posts and landing pages associated with your chosen domain), making it the perfect global solution for the Interactive theme.
3. Testing and Customization
Test: Go to any page on your live website, scroll down past the top section, and the circular black button with the bright green (#6cca0b) carrot arrow should appear in the bottom right corner. Clicking it should smoothly scroll you back to the top.
Customization: If you want to change the look, you can easily modify the CSS within the <style> tags in the code block.
Future Customization Instructions
To adjust the three elements look for the following lines within the code block in your Site Footer HTML:
Feature to Change
Section in Code
Property/Value to Edit
Description
Arrow/Carrot Character
<button> HTML Element
▲
Change this HTML entity to a different symbol. Common alternatives are: ^ (simple caret), ⇑ (double up arrow), or ⇧ (the original thick arrow).
Arrow/Carrot Color
#backToTopBtn CSS
color: #6cca0b;
Replace #6cca0b with any desired hex code or color name (e.g., red, blue).
Arrow/Carrot Size
#backToTopBtn CSS
font-size: 30px;
You can increase or decrease the pixel value (30px) to change the size of the arrow symbol inside the button.
My personal recommendation is to create seperate files like back-to-top.js and back-to-top.css in the corresponding folders in your theme. Add the back-to-top.js below by adding
in your layout.html (or base.html) which should be located in templates/layout folder.
For the back-to-top.css, add it to your main.css by using
{% include 'path/to/back-to-top.css' %}
Last but not least, add a
<a href="#" class="back-to-top"></a>
to your layout.html (or base.html).
Another option would be to create a custom module with similar code and add the custom module to the layout.html (or base.html; what ever it's called in your case)
best,
Anton
p.s.: Feel free to modify the css with some theme variables