Only allow the body to scroll x pixels css

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Only allow the body to scroll x pixels css



Lets say my website is 2000px long, my viewport is 1000px tall like so:



enter image description here



By default this will allow the website to scroll 1000px. In order to scroll that distance the scroll bar must move from the top to the bottom.



However what if I only want the scroll bar to be able to scroll 500px, thus hiding the bottom 500px of the website, but I don't want to change the height of my website.



I tried to do this using the css code below:


body
height: 1500px;
max-height: 1500px;
position: absolute;



But this didn't work because body doesn't have a parent element to add scroll bars around it.



Is there any other way to do this?





Why do you want to have part of the page like this not be visible? Also, body does have a parent element: html.
– TylerH
Aug 10 at 14:43


body


html





@TylerH That worked, thank you I set the html element to overflow scroll. It's for a javascript plugin I'm trying out, I want to bind scroll distance to a javascript event.
– YAHsaves
Aug 10 at 14:45






Glad to hear that worked; please add your solution as an answer and give it the checkmark so others know what solved it for you!
– TylerH
Aug 10 at 14:53





Not sure if this has been solved or not, if not then why not just put it in a div that has a max-height of 1500px (allows the body to scroll to 1500px) with overflow:hidden I also don't see why you would do this as it is a massive usability issue and is likely to result in a high bounce rate
– Pete
Aug 10 at 15:06



overflow:hidden




2 Answers
2



I believe position should be relative not absolute. Do you need it to be the body element? Why not create a container element and just wrap the pages content in that so it would be



Example


<body>
<div class="main-container">
website content
</div>
</body>



Styling:


.main-container
height: 1500px;
max-height: 1500px;
position:relative;





I tried position relative, but it didn't work either. I want to do it on the body because I am developing a javascript plugin that needs to work on as many websites as possible, so I won't be able to wrap a websites elements in a parent div ahead of time
– YAHsaves
Aug 10 at 14:42



After some discussion in the comments I realized the html element is the parent of the body element. Thus using the css code in my answer and setting the html overflow to scroll everything works as desired.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard