Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS position element "fixed" inside scrolling container

Tags:

html

css

position

i wonder if anyone has found a solution for this?

i am looking for a solution to attach an element to the top of a scrolling container

HTML:

<div class="container">   <div class="header">title</div>   <div class="element">......</div>   ... (about 10-20 elements) ...   <div class="element">......</div>  </div> 

all "elements" have position:relative,

the container has the following CSS:

.container {   position:relative;   width:200px;   height:400px;   overflow-y:scroll; } 

i want the header to stay on top of the container, independant of its scrolling position and the elements scrolling underneath.

the CSS so far:

.header {   position:absolute; /* scrolling out of view :-( */   z-index:2;   background-color:#fff; } .element{   position: relative; } 

all elements are block elements, and i can not move the header outside of the container. jquery is no option at this point.

like image 474
Siggi Gross Avatar asked Jun 29 '12 11:06

Siggi Gross


People also ask

Which CSS style places an element at a fixed location within its container?

1)static: this is the default value. 2)sticky: the element is positioned based on the user's scroll position. 3)fixed: the element is positioned related to the browser window.

How do I make my div fixed and scrollable?

To make div fixed scrolling to that div with CSS, we can use the position: sticky CSS property. position: -webkit-sticky; position: sticky; top: 0; z-index: 1; to set position to sticky and z-index to 1 to make the element with the sticky position show on top after we scroll to it.

How do you fix an element inside a div?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.


2 Answers

I think your solution pass with position:sticky. Seems it's like position:fixed but respects the relative position to his parent.

Unfortunately this is an experimental feature, and is only supported in Chromium. You can see more details in this test page.

The pure css solution that comes into my mind is with a little change of the markup. You can set a container only for the "elements" as this:

<div class="cont_elements">       <div class="element">......</div>       ..... </div> 

And give the overflow to this inner container. Now, your header sticks at top.

Here's a working demo.

like image 147
Arkana Avatar answered Sep 24 '22 11:09

Arkana


Here's the solution I came up with using position: sticky (so no IE unfortunately):

https://codepen.io/waterplea/pen/JjjMXzR

The idea is to have a 0 height sticky container on top of the scrolling container, so it sticks but doesn't push any of the content below and then position your stuff absolutely inside it. This way you have width but do not have height, so you can only kinda position something from the top like I did with button in my example.

EDIT: Found a way to make it have 100% height and not push the content below using float. Updated the codepen. Had to use calc(100% - 1px) because of this bug in Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1612561

like image 42
waterplea Avatar answered Sep 22 '22 11:09

waterplea