How to create a 'scroll to top' button with HTML
Create an HTML button and set the click event to call the JavaScript function; 2. Use CSS to pin the button to the lower right corner of the page and set the hidden default state; 3. Listen to the scroll event through JavaScript, and display the button when the scroll distance exceeds 300px, and scroll smoothly to the top when clicked. Finally, a return to the top button to improve the user experience is realized, and the full functionality is completed in collaboration with HTML, CSS and JavaScript.
Adding a "scroll to top" button improves user experience by letting visitors quickly return to the top of a long webpage. Here's how to create one using HTML, CSS, and a bit of JavaScript.

1. Create the Button with HTML
Start by adding a button element in your HTML. You can place it near the end of your content or in a fixed position.
<button onclick="scrollToTop()" id="scrollBtn">Top</button>
This creates a simple button that calls a JavaScript function scrollToTop()
when clicked.

2. Style the Button with CSS
Use CSS to position the button (usually fixed in the bottom-right corner) and make it visually appealing.
#scrollBtn { display: none; /* Hidden by default */ position: fixed; bottom: 20px; right: 20px; z-index: 99; font-size: 18px; padding: 10px 15px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; } #scrollBtn:hover { background-color: #0056b3; }
The button is hidden initially ( display: none
) and will only appear when the user scrolls down a certain distance.

3. Add JavaScript to Control Visibility and Scrolling
You need JavaScript to:
- Show the button when the user scrolls down
- Hide it when near the top
- Smoothly scroll back to the top when clicked
// Show/hide button based on scroll position window.onscroll = function() { const btn = document.getElementById("scrollBtn"); if (document.body.scrollTop > 300 || document.documentElement.scrollTop > 300) { btn.style.display = "block"; } else { btn.style.display = "none"; } }; // Scroll to top function function scrollToTop() { window.scrollTo({ top: 0, behavior: 'smooth' }); }
Note:
document.documentElement.scrollTop
is used for modern browsers, whiledocument.body.scrollTop
handles older ones.
Optional: Use an Icon instead of Text
You can replace the text "Top" with an upward arrow icon using Unicode or an icon library like Font Awesome:
<button onclick="scrollToTop()" id="scrollBtn">↑</button>
Or with Font Awesome:
<button onclick="scrollToTop()" id="scrollBtn"> <i class="fas fa-arrow-up"></i> </button>
(Just make sure to include the Font Awesome library in your HTML.)
That's it. With these three parts — HTML structure, CSS styling, and JavaScript logic — you have a fully functional scroll-to-top button. It's not complex, but small details like smooth scrolling and smart visibility make a big difference in usability.
The above is the detailed content of How to create a 'scroll to top' button with HTML. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This tutorial explores the problem of crawling failure if JavaScript dynamically loads content when crawling URLs from web pages using the R language rvest package. The article explains in detail why traditional HTML parsing methods may be invalid and provides an efficient solution: by identifying and directly calling the API interface behind the web page, using the httr package to obtain JSON data, thereby successfully extracting the required information.

To disable HTML form elements, you can use the disabled attribute, which can prevent user interaction and the element value will not be submitted with the form. This attribute is of a Boolean type and can be directly added to form element tags such as input, textarea, select, or button. For example, it can also be dynamically controlled through JavaScript, such as document.getElementById("myInput").disabled=true. If the element cannot be edited but the value is still submitted, you should use the readonly attribute. The disabled attribute is simple and effective and widely supported.

TolinktoaspecificpartofapageusinganchorsinHTML,assignauniqueidtothetargetelement,suchas,thencreateahyperlinkwithhref="#section1"toscrolltothatsection,andforcross-pagelinking,usethefullURLlikepage.html#section1,ensuringsmoothnavigationwithou

Create an HTML button and set the click event to call the JavaScript function; 2. Use CSS to pin the button to the lower right corner of the page and set the hidden default state; 3. Listen to the scroll event through JavaScript, and display the button when the scroll distance exceeds 300px, and scroll smoothly to the top when clicked. Finally, a return to the top button to improve the user experience is realized, and the full functionality is completed in collaboration with HTML, CSS and JavaScript.

Theactionattributespecifieswheretosendtheformdata,andthemethodattributedefineshowtosenditusingHTTPmethods.1.TheactionattributesetsthedestinationURL(absoluteorrelative);ifomitted,theformsubmitstothecurrentpage.2.Themethodattributeuses"get"to

This article details how to use CSS to achieve transparent and floating custom scroll bars on content. By combining overflow: overlay; attributes and scrollbar pseudo-element styles for different browsers (WebKit/Firefox), you can precisely control the color, transparency, width and rounded corners of scrollbars, thereby improving the visual consistency and user experience of the web interface.

Use the accept attribute to limit the upload type of HTML file, such as accept="image/*" only allows images, accept=".pdf" only allows PDF, accept=".doc,.docx,.pdf,.txt" allows multiple specified types, and can combine JavaScript to verify file types to improve user experience, but security verification must be performed on the server side, because the accept attribute is not secure and the browser supports are different, and it is only used to improve availability rather than replace server verification.

Use it to create line-breaking spaces in HTML, such as preventing the display of numbers and unit branches; 1. Used to avoid breaking lines between names, values and units; 2. Maintain the text format within the line; 3. It can be used as a blank placeholder, but CSS is recommended; other space characters such as , , etc. are suitable for special scenarios, but in most cases it is sufficient; be careful not to abuse the layout, CSS should be used instead, and multiple will not be merged, and the screen reader can recognize it normally, so it is necessary to use reasonably to ensure the text is displayed coherently.
