Let's take take a simple example:
<nav id="#mynav">MY NAVBAR</nav>
and a basic style:
#mynav { position : sticky; }
I would like to apply the following styling information to my navbar only when it detaches from the normal flow, in order to visually split it from the main content (with a shadow in this case)
box-shadow : 0px 10px 15px 0px rgba(0,0,0,0.75);
Is there some kind of pseudo-class or media-query-like thing I could use for that? E.g.:
#mynav:some-pseudo-class { box-shadow : 0px 10px 15px 0px rgba(0,0,0,0.75); }
I know there are good plugins for this, but all of them seem to be unable to implement it whithout bypassing the (quite new) native feature position:sticky
. Instead, they do it in an old-fashion way (scroll event, and position:fixed; top:0
).
So, is it possible to do it using position:sticky
, and without using the scroll
event, which slows down the fluidity of the page (I'm not against javascript, but scroll event is too slow)?
Absolute positioning To remove an element from the normal document flow, absolute position is a great option. The position of an absolute element isn't affected by other elements and doesn't affect the position of other elements.
CSS Demo: position To see the effect of sticky positioning, select the position: sticky option and scroll this container. The element will scroll along with its container, until it is at the top of the container (or reaches the offset specified in top ), and will then stop scrolling, so it stays visible.
If you really want position: sticky to work on all modern browsers, then you should consider not using overflow: hidden on the <body> or any wrapper surrounding the main content, but rather put a wrapper around elements that will overflow the viewport. Then, those wrappers should use overflow: hidden.
position: sticky; A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
Unfortunately, as far as I know there are no pseudo class that targets the 'sticky' state.
See post: Targeting position:sticky elements that are currently in a 'stuck' state
An alternative would be to use jQuery to add or remove a class/css styling when it reaches the element that needs to be stickied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With