Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a fixed element, but scroll it horizontally

Tags:

html

css

fixed

Having some issues getting this to work. PLEEEEEEASE help.

I would like an element fixed to the top of the viewport, when the user scrolls down the page it remains at the top of the viewport... easy. If the window is narrower then 960px the horizontal scrollbars appear. If the window is scrolled horizontally I would like the content inside this fixed element to scroll with it.

Please check out the demo, the two green boxes should always line up. Make your window narrow and scroll horiz, notice how they no longer line up.

Is this possible without JavaScript? Should work in IE7+ and not totally break in IE6.

http://www.louiswalch.com/beta/t/_scrolltest4.html

like image 337
Louis W Avatar asked Mar 09 '11 18:03

Louis W


People also ask

How can you horizontally center a fixed sized element through CSS?

Add CSS. Set the top and left properties to 50% to center the left-top corner of the <div>. Set the margin-top and margin-left properties to the negative half of the height and width of the <div>.

How do I fix a div on horizontal scroll?

For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.


1 Answers

Ah, interesting question. I don’t think you can achieve that without JavaScript.

position: fixed means that the element is positioned relative to the viewport. You want that vertically, but you don’t want it horizontally. I don’t think there’s any way to achieve that in CSS.

like image 99
Paul D. Waite Avatar answered Sep 30 '22 07:09

Paul D. Waite