My website uses PHP and CSS. I am looking for a solution to display a back to top button, but only if the content DIV has enough text so the user needs to scroll down, then click the button to back up.
Is this possible without javascript?
I can place such a button, click it to go to the top of the content DIV, but if there is not enough text to fill the entire content DIV for high resolutions (higher than 1920×1080), the button looks silly as it has no use and there is not enough text to fill the content DIV.
I can manually hide the button with the display: hidden; line, but setting the class of the button to one of three states (normal vs. hide for higher resolutions vs. hide for much higher resolutions) is tedious.
In short, how can I set something up like "If length of text/size of text > height of content div, then display back to top button"?
Playing around with DIVs and CSS
2
Answers
While it’s challenging to achieve this without using JavaScript for dynamic calculations based on the content size and scroll position, you can still make it more flexible using pure CSS for a responsive design. One approach is to use media queries combined with the :empty and :before pseudo-elements to conditionally display the "back to top" button. However, this solution assumes that your content is within a specific container.
Please check my code how I implement this on my website