# Position: Sticky
Position: sticky is a unique CSS positioning property that combines aspects of both relative and absolute positioning. Here's an easy-to-understand explanation and notes:
## Definition
According to MDN:
- A sticky element does not leave the normal document flow (similar to `position: relative`).
- It "sticks" to its nearest **scrolling ancestor** when certain conditions are met, behaving like `position: absolute`.
## What is a "Scrolling Ancestor"?
A **scrolling ancestor** is any parent element that creates a scrollable area. This could be:
1. An element with an `overflow` property (e.g., `overflow: auto`, `overflow: scroll`).
2. The entire HTML document if no scrollable parent exists.
## How Sticky Elements Behave
1. **Before reaching the scrolling ancestor:**
- The sticky element behaves like `position: relative`, moving with the flow of the document.
2. **After reaching the scrolling ancestor:**
- The sticky element behaves like `position: absolute`, sticking to a fixed position relative to the scrolling ancestor.
- The exact "sticking" position can be adjusted using `top`, `right`, `bottom`, or `left`.
## Example
Consider the following example:
- A container element with content taller than its height and `overflow` set, creating a scrollable area.
- Inside the container is a sticky green box.
```html
<div class="container">
<div class="content">...</div>
<div class="sticky-box">I am sticky!</div>
<div class="content">...</div>
</div>
```
```css
.container {
height: 300px;
overflow: auto;
border: 1px solid black;
}
.sticky-box {
position: sticky;
top: 0; /* Sticks to the top of the scrolling ancestor */
background: green;
padding: 10px;
}
```
**Behavior:**
- As the user scrolls down, the sticky box moves upward like a `relative` element.
- When it reaches the top of the container (its scrolling ancestor), it sticks there and remains visible.
- The `top: 0` defines the position at which it sticks.
## Key Notes
1. The sticky element sticks only within its scrolling ancestor. It won’t stick to the screen if the ancestor scrolls out of view.
2. Sticky positioning depends on `overflow` properties of ancestors. If `overflow: hidden` is set, it may prevent sticky behavior.
3. Ensure you specify `top`, `right`, `bottom`, or `left` to activate the sticky behavior.
## Troubleshooting Tips
- **Not sticking?**
- Check if any ancestor has `overflow: hidden` or `overflow: clip`.
- Ensure the sticky element is within a scrollable ancestor.
- **Unexpected behavior?**
- Confirm that the sticky element and its ancestors are properly sized and positioned.
Position: sticky is a powerful tool for creating dynamic and responsive designs, especially useful for headers, menus, or important information that needs to stay visible during scrolling.