Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make position:fixed div scroll horizontally

I have a div with position:fixed, as the image below suggests.

I want it to be fixed vertically, but I want it to scroll horizontally with the rest of the content (notice the horizontal scroll bar).

Is this possible with CSS? Or do I need Javascript (in this case, how could I do it)?

enter image description here

like image 587
John Assymptoth Avatar asked Jan 18 '12 15:01

John Assymptoth


2 Answers

Old question, but I was also looking for a solution also, and found a nice simple jquery solution, and figured someone else might find it helpful if nothing else:

    $(window).scroll(function(){
        $('#header').css('left', originalLeft - $(this).scrollLeft());
    });

with header being the div, and originalLeft being, well, the initial left:position. This also works if someone scrolls and then zooms out, such that it would go where you would want it to.

like image 62
Fewfre Avatar answered Oct 27 '22 00:10

Fewfre


You need javascript. Here you can read a tutorial (with mootools): http://www.rickyh.co.uk/css-position-x-and-position-y/. Here a similar question, solved with jquery: CSS: fixed position on x-axis but not y?

like image 28
Alessandro Pezzato Avatar answered Oct 26 '22 23:10

Alessandro Pezzato