Sticky
Sticky
lets you use position
sticky on an element with some enhancements.
-->
Sticky
lets you use position
sticky on an element with some enhancements.
Instead of having to remember to use position:sticky
and top
, just use Sticky
and you’ll have a sticky element.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Just change the top
value if you want the sticky element have a little bit of offset.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
All four directions are supported. To activate these other directions, just set top
to auto
and the direction (bottom
, left
or right
) to the value you desire.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Sticky
Sticks to the left
Sticky
Sticks to the right
Just use our Sticky
script and add Sticky-check
to your element. Elements that are sticking will now have a sticking
class.
In this Demo, elements will turn blue when they stick!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Sticky
Sticks to the left
Sticky
Sticks to the right
Sticky
lets you use position
sticky on an element.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
For position:sticky
to work, it must be satisfy three conditions:
overflow: auto
or overflow: scroll
. The <body>
element is also considered a scrollable ancestor.Sticky
must contain sufficient height for it to scroll.Sticky
element must have a top
or bottom
attribute to determine where it sticks. If scrolling horizontally, it must have a left
or right
attribute.If these two conditions are satisified, Sticky
would work. If not, Sticky
won’t.
What trips most people up is trying to use Sticky
on a sidebar.
There are two questions to resolve here:
If you want the entire sidebar to be sticky, you can add position: sticky
to the sidebar. At the same time, you also need to ensure the sidebar is not stretch
.
We resolve this problem for you in Sticky
if you use any of our Grid
layout components.
Content Line 1
Content Line 2
Content Line 3
Content Line 4
Content Line 5
Content Line 6
Content Line 7
Content Line 8
Content Line 9
Content Line 10
Content Line 11
Content Line 12
Content Line 13
Content Line 14
Content Line 15
To make one element in the sidebar sticky, you have to make the sidebar stretch the full height. Then, add position:sticky
to the element within.
Sticky
needs a top
, bottom
, left
, or right
attribute to determine where it sticks. By default, we set top
to 0
because that’s the most common use case.
You can change top
to reposition how far away from the top the Sticky
element should stick.
Content Line 1
Content Line 2
Content Line 3
Content Line 4
Content Line 5
Content Line 6
Content Line 7
Content Line 8
Content Line 9
Content Line 10
Content Line 11
Content Line 12
Content Line 13
Content Line 14
Content Line 15
Sidebar
If you want Sticky
to stick to the bottom, set top
to auto
and bottom
to the value you desire.
Content Line 1
Content Line 2
Content Line 3
Content Line 4
Content Line 5
Content Line 6
Content Line 7
Content Line 8
Content Line 9
Content Line 10
Content Line 11
Content Line 12
Content Line 13
Content Line 14
Content Line 15
Sidebar
If you want Sticky
to stick to the left, set left
to the value you desire. Setting top
to auto
is optional.
Sticks to the left
If you want Sticky
to stick to the right, set right
to the value you desire. Setting top
to auto
is optional.
Sticks to the right
If the parent scrollable container has a padding
, Sticky will respect that padding
value before calculating the top
, bottom
, left
, or right
values.
You can adjust the top
, bottom
, left
and right
values accordingly to compensate for the padding
values.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem.Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Do note that only one value — top
, bottom
, left
or right
— will be
used because Sticky
can only stick to one side. To compensate for the
padding
on other sides, you may have to use
Breakout
or negative margins.
You can change the z-index
value for Sticky
with the z-index
property or --z-index
variable.
CSS doesn’t provide a way to detect if an element is in a sticking state, so we wrote a script that does it. You can use the script by running the Sticky
script we included.
Next, add Sticky-check
to the Sticky
element to check for a sticking state.
Sticky
items that are sticking will have a sticking
class added to them.
You can style these items with CSS or Tailwind. If you use our layouts preset, you can use the sticking
variant too.
Here’s a bonus styling tip. If you want to style elements within a sticking
one, you have to use Tailwind arbitary variants. (Or, just use CSS if you’re more comfy with that!).
The Sticky
script uses MutationObserver
to observe the DOM for Stick-check
items that are loaded asynchronously. By default, it listens to DOM changes for 1000ms
.
You can change this value when using the Sticky
script.
If you wish to disable the MutationObserver
behaviour, set observerTimeout
to 0
.
Even if you don’t use MutationObserver to watch for Sticky-check
elements, you can always run the script again when you load your asynchronous sticky element.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Sticky
lets you use position
sticky on an element.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
For position:sticky
to work, it must be satisfy three conditions:
overflow: auto
or overflow: scroll
. The <body>
element is also considered a scrollable ancestor.Sticky
must contain sufficient height for it to scroll.Sticky
element must have a top
or bottom
attribute to determine where it sticks. If scrolling horizontally, it must have a left
or right
attribute.If these two conditions are satisified, Sticky
would work. If not, Sticky
won’t.
What trips most people up is trying to use Sticky
on a sidebar.
There are two questions to resolve here:
If you want the entire sidebar to be sticky, you can add position: sticky
to the sidebar. At the same time, you also need to ensure the sidebar is not stretch
.
We resolve this problem for you in Sticky
if you use any of our Grid
layout components.
Content Line 1
Content Line 2
Content Line 3
Content Line 4
Content Line 5
Content Line 6
Content Line 7
Content Line 8
Content Line 9
Content Line 10
Content Line 11
Content Line 12
Content Line 13
Content Line 14
Content Line 15
To make one element in the sidebar sticky, you have to make the sidebar stretch the full height. Then, add position:sticky
to the element within.
Sticky
needs a top
, bottom
, left
, or right
attribute to determine where it sticks. By default, we set top
to 0
because that’s the most common use case.
You can change top
to reposition how far away from the top the Sticky
element should stick.
Content Line 1
Content Line 2
Content Line 3
Content Line 4
Content Line 5
Content Line 6
Content Line 7
Content Line 8
Content Line 9
Content Line 10
Content Line 11
Content Line 12
Content Line 13
Content Line 14
Content Line 15
Sidebar
If you want Sticky
to stick to the bottom, set top
to auto
and bottom
to the value you desire.
Content Line 1
Content Line 2
Content Line 3
Content Line 4
Content Line 5
Content Line 6
Content Line 7
Content Line 8
Content Line 9
Content Line 10
Content Line 11
Content Line 12
Content Line 13
Content Line 14
Content Line 15
Sidebar
If you want Sticky
to stick to the left, set left
to the value you desire. Setting top
to auto
is optional.
Sticks to the left
If you want Sticky
to stick to the right, set right
to the value you desire. Setting top
to auto
is optional.
Sticks to the right
If the parent scrollable container has a padding
, Sticky will respect that padding
value before calculating the top
, bottom
, left
, or right
values.
You can adjust the top
, bottom
, left
and right
values accordingly to compensate for the padding
values.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem.Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eligendi debitis facere dolore deleniti neque excepturi necessitatibus, molestiae quis amet tempore totam quaerat blanditiis quasi magni rem. Ducimus necessitatibus eum quia.
Do note that only one value — top
, bottom
, left
or right
— will be
used because Sticky
can only stick to one side. To compensate for the
padding
on other sides, you may have to use
Breakout
or negative margins.
You can change the z-index
value for Sticky
with the z-index
property or --z-index
variable.
CSS doesn’t provide a way to detect if an element is in a sticking state, so we wrote a script that does it. You can use the script by running the Sticky
script we included.
Next, add Sticky-check
to the Sticky
element to check for a sticking state.
Sticky
items that are sticking will have a sticking
class added to them.
You can style these items with CSS or Tailwind. If you use our layouts preset, you can use the sticking
variant too.
Here’s a bonus styling tip. If you want to style elements within a sticking
one, you have to use Tailwind arbitary variants. (Or, just use CSS if you’re more comfy with that!).
The Sticky
script uses MutationObserver
to observe the DOM for Stick-check
items that are loaded asynchronously. By default, it listens to DOM changes for 1000ms
.
You can change this value when using the Sticky
script.
If you wish to disable the MutationObserver
behaviour, set observerTimeout
to 0
.
Even if you don’t use MutationObserver to watch for Sticky-check
elements, you can always run the script again when you load your asynchronous sticky element.
This is the CSS for Sticky
.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
This is the CSS for Sticky
.
Class | Description |
---|---|
Sticky-check | Use Sticky script to check if element is sticking |
sticking | Will be present when element is sticking |
Property | Description |
---|---|
top | The top position of the Sticky element |
bottom | The bottom position of the Sticky element |
left | The left position of the Sticky element |
right | The right position of the Sticky element |
Variable | Default | Description |
---|---|---|
--z-index | 15 | z-index value for the Sticky element |
The Sticky
script has the following :
Prop | Default | Description |
---|---|---|
selector | undefined | The container for the script. You can pass a selector or element into this value. When undefined , it defaults to document.body . |
options | Optional options object |
Below are the options available to Sticky
.
Option | Default | Description |
---|---|---|
observerTimeout | 1000 | The amount of time to watch the DOM for elements with Sticky-check . To disable the watching behaviour, set this to 0 |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Class | Description |
---|---|
Sticky-check | Use Sticky script to check if element is sticking |
sticking | Will be present when element is sticking |
Property | Description |
---|---|
top | The top position of the Sticky element |
bottom | The bottom position of the Sticky element |
left | The left position of the Sticky element |
right | The right position of the Sticky element |
Variable | Default | Description |
---|---|---|
--z-index | 15 | z-index value for the Sticky element |
The Sticky
script has the following :
Prop | Default | Description |
---|---|---|
selector | undefined | The container for the script. You can pass a selector or element into this value. When undefined , it defaults to document.body . |
options | Optional options object |
Below are the options available to Sticky
.
Option | Default | Description |
---|---|---|
observerTimeout | 1000 | The amount of time to watch the DOM for elements with Sticky-check . To disable the watching behaviour, set this to 0 |