csswg-drafts icon indicating copy to clipboard operation
csswg-drafts copied to clipboard

[css-position-3] New `container-fixed` value for `position` within a `@container`

Open brandonmcconnell opened this issue 2 years ago • 4 comments

With the growing support for @container, it’d be cool if CSS added a new position value to make an element fixed within the context of its container.

The main workaround for this I've used thus far is to position an element absolute within its parent with the top value offset by the current scroll distance of its parent.

We can certainly bikeshed on the value name a bit more as container-fixed is a bit verbose, but that's the general idea.

brandonmcconnell avatar Apr 24 '23 20:04 brandonmcconnell

I'm having difficulty envisioning how this would work. It feels like this would be the same as establishing the container as a containing block for fixed positioning, but for a descendant item specifically. In practice, it would be the same as absolute positioning.

SYwaves avatar May 04 '23 06:05 SYwaves

@SYwaves The same as absolute positioning if the container had a sibling element to the absolute element in its first level that was a scrollable box. Otherwise, an absolute position won't appear "fixed" as the user scrolls in the container.

That approach also necessitates that the absolute element appears on the container's top level (under its relative position, as other elements may also use relative positioning) vs. being any arbitrary number of layers deep as fixed positioning usually supports.

brandonmcconnell avatar May 04 '23 10:05 brandonmcconnell

Oh, it's something to the effect of background-attachment: scroll, where the fixed thing is attached relative to the border of the container itself?

SYwaves avatar Jan 10 '24 07:01 SYwaves

Also see https://github.com/w3c/csswg-drafts/issues/7475, specifically https://github.com/w3c/csswg-drafts/issues/7475#issuecomment-1182982687 where I suggested an extension to position: fixed that indicate where something is fixed against.

Fitting things into my proposed syntax, your proposal would become position: fixed / container;

bramus avatar Mar 28 '24 23:03 bramus

Looks like another use case of the position-container proposal (#9868)

xiaochengh avatar Apr 02 '24 06:04 xiaochengh

@xiaochengh Yes, perhaps an extension

brandonmcconnell avatar Apr 02 '24 06:04 brandonmcconnell