Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: sidebar with fixed position getting cut off

My website has a sidebar with lots of stuff in it—so much that the browser viewport must have a height of at least 1020 pixels in order to see it all without having to scroll (unless, of course, you zoom out).

I'd like the sidebar's position to be fixed so that when you're on a page with lots of content, the sidebar stays in the same position as you scroll. This was very easy to implement:

div#Sidebar {
    position: fixed;
}

This works well on my computer as long as the browser is maximized because my monitor is running at 1920 x 1200. But if I resize my browser window, the sidebar gets cut off. As I scroll through the page's content, I can see all of the page's content, but the sidebar remains cut off due to its position being fixed. So it seems I only have two options:

  1. Make the sidebar's position not fixed (bad), but allow users to be able to see all of the sidebar (good).

  2. Make the sidebar's position fixed (good), but don't allow users to be able to see all of the sidebar (bad).

Is there a way to combine these two options?

like image 295
Frank Avatar asked Jun 09 '11 17:06

Frank


People also ask

How do you keep position fixed in CSS?

Step 2) Add CSS: To create a fixed top menu, use position:fixed and top:0 . Note that the fixed menu will overlay your other content. To fix this, add a margin-top (to the content) that is equal or larger than the height of your menu.

Does position fixed take up space?

By using position: fixed on an element like the header, the element is removed from the stack and positioned relative to the browser window. That means it no longer takes up space and any elements positioned after it will adjust to fill up that area.

How do I move the sidebar in CSS?

Moving The Sidebar The top position for all 3 columns is set to 0, and then we stick the right sidebar to the right, move the left sidebar left -50% (negative the width of #main-content) and move the main content left 25% (the width of #left-sidebar) to rearrange the columns.

How do I move my sidebar from left to right CSS?

Hi there, You should be able to change where the sidebar appears on Appearance > Customize > Layout > Sidebars. Set the Sidebar Layout to “Sidebar / Content” so the sidebar goes to the left side of the content.


1 Answers

You could do a test in javascript. Either in pure javascript, or with jQuery (would be a lot easier).

Here's an example for jQuery :

$(window).resize(function() {
  if ( $(window).height() < 800) {
    $('#Sidebar').addClass('beAbsolute').removeClass('beFixed');
  } else {
    $('#Sidebar').addClass('beFixed').removeClass('beAbsolute');
  }
});

CSS :

.beFixed {position:fixed;}
.beAbsolute {position:absolute;top:0px;}

By default, use the absolute version, so that user without JavaScript can see the whole sidebar.

like image 61
Kraz Avatar answered Nov 25 '22 15:11

Kraz