How to Make Larger-Than-Window Element Visible But Not Scrollable

In this tutorial, I will show you how to make an element that is too big for some screens visible when it does fit, but not trigger scrolling when it doesn’t. I’ll also show you how to center it. Essentially, we can make an element in a website behave just like the background of the <body> element. It took me a while to figure out the bugs with this, but eventually I was able to solve the problem.

This might help you understand what we are trying to do

Let’s clarify our objectives.

1. The element must be visible (not cut off) when the browser is big enough in size to see it.
2. It should not trigger the ability to scroll when the window is too small, but when other elements allow you to scroll, will still scroll with them (not fixed.)
3. We should be able to specify a minimum size for the important aspects of the element, so that if the window is even smaller than this size, you can still scroll to those important parts.
4. The element should be centered on the page.

 

Still confused about what this means? Check out this demo of the solution which will help explain what we are trying to do.

The html:

Nothing too fancy, basically just a box or container div around the div with your element. In my case, the div IS the element. The magic, now, is in the CSS.

The CSS:

That’s basically all you have to do. You can put anything you want within the #element div, and it should still behave how it is supposed to. I use it on a website for cycling through divs like a slideshow using jquery cycle. It took me a while to figure out this solution, but I think it works pretty well, so let me know in the comments if you have any trouble with it.

Keep in mind, this isn’t going to work quite the same with any other positioning. You are going to run into problems with just about anything else. If you didn’t care about centering, you would probably be fine leaving your positioning as static but that is probably the best you can hope for. Check out the working demo.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

Leave a Reply

Your email address will not be published. Required fields are marked *