CSS position: sticky property value
position: sticky is a relatively new value among CSS positioning properties.Sticky positioningIsRelative positioningandfixed positioningis a hybrid of relative and fixed positioning: before crossing the specifiedthresholdthreshold, it behaves like relative positioning; after that, it behaves like fixed positioning.
This is a modern solution for effects such as "sticky navigation" and "fixed table headers."
Word meaning:sticky means "sticky," indicating that the element will "stick" at a certain position without moving.
Basic Syntax
position: sticky It needs to be combined with a threshold in at least one direction (top、right、bottom、left) to specify where the "sticky" behavior takes effect.
/* 语法 */
position: sticky;
/* Common combination: fix when scrolling 10px from top */
.sticky-header {
position: sticky;
top: 10px;
}
How It Works
sticky The way sticky positioning works can be broken down into two phases:
Phase 1: Normal scrolling (relative positioning)
When the element in the viewport hasnot yet reachedthe specified threshold, it is laid out according to the normal document flow,and moves with the page scroll. At this point it is equivalent to position: relative。
Phase 2: Fixed display (fixed positioning)
When the element scrolls toexceeds thethe specified threshold, the elementFixedstays at that position and no longer scrolls with the page. At this point it is equivalent to position: fixed。
Threshold conditions
| property | Trigger conditions |
|---|---|
top: 10px |
Fixed when the top of the element is more than 10px from the top of the viewport |
bottom: 10px |
Fixed when the bottom of the element is more than 10px from the bottom of the viewport |
left: 10px |
Fixed when the left of the element is more than 10px from the left of the viewport |
right: 10px |
Fixed when the right of the element is more than 10px from the right of the viewport |
Core Features
Feature 1: Dual behavior
sticky combines the characteristics of both relative and fixed, making it a perfect combination of the two.
Feature 2: No need for the parent element to set positioning
The difference between absolute Unlike fixed positioning, sticky elementsNot requireddo not require any positioning properties set on the parent element.
Feature 3: Limitation of parent container overflow
If the parent element sets overflow: hidden、overflow: auto or overflow: scroll, sticky positioning may fail.
Feature 4: Activates z-index
sticky positioning will be activated z-index attribute.
Example
Let us understand in depth through examples how position: sticky characteristic.
Example 1: Sticky navigation bar
This is the most common application of sticky positioning: creating a sticky navigation bar.
Example
RUNOOB Tutorial
Chapter 1: Getting Started
This is the content of Chapter 1. Scroll down to see the sticky effect of the navigation bar.
Chapter 2: Intermediate
This is the content of Chapter 2.
Chapter 3: Advanced
This is the content of Chapter 3.
Chapter 4: Practice
This is the content of Chapter 4.
Chapter 5: Projects
This is the content of Chapter 5.
Example 2: Fixed table header row
sticky positioning is ideal for fixing the header of a table:
Example
Student score table
Scroll down to see the fixed header effect:
|
Student ID |
Name |
Chinese |
Math |
English |
|---|---|---|---|---|
|
001 |
Zhang San |
85 |
92 |
88 |
|
002 |
Li Si |
90 |
87 |
95 |
|
003 |
Wang Wu |
78 |
85 |
82 |
|
004 |
Zhao Liu |
92 |
88 |
90 |
|
005 |
Qian Qi |
88 |
91 |
87 |
|
006 |
Sun Ba |
85 |
89 |
93 |
|
007 |
Zhou Jiu |
80 |
86 |
84 |
|
008 |
Wu Shi |
91 |
93 |
89 |
|
009 |
Zheng Shiyi |
87 |
85 |
88 |
|
010 |
Chen Shier |
89 |
90 |
91 |
Example 3: Sticky sidebar effect
When page content is very long, the sidebar can be fixed after scrolling to a certain position:
Example
Main content area
Scroll down to see the sticky effect of the sidebar.
Content area 1
This is the first paragraph of content.
Content area 2
This is the second section of content.
Content area 3
This is the third section of content.
Long content area
This is a relatively long content area, used to demonstrate the sticky effect of the sidebar.
Keep scrolling down...
Content area 4
This is the fourth section of content.
Content area 5
This is the fifth paragraph of content.
Example 4: Fixed section headings
sticky positioning is ideal for fixing section headings in an article:
Example
Book chapter example
Chapter 1: First Encounter with Programming
Programming is a creative art. This chapter will show you what programming is and why learning it is so important.
Chapter 2: Setting Up the Environment
Before starting to write code, we need to set up a development environment. This chapter introduces how to install the necessary tools and configure the development environment.
The content area is long enough to produce a scrolling effect.
Chapter 3: Basic Syntax
This chapter introduces the basic syntax of programming languages, including core concepts such as variables, data types, and operators.
Chapter 4: Control Structures
Control structures determine the execution order of a program. This section introduces how to use conditional statements and loops.
Chapter 5: Functions
Functions are the basic unit of organizing code. This chapter explains how to define and call functions.
Example 5: Bidirectional sticky positioning
You can set sticky positioning in both horizontal and vertical directions at the same time:
Example
Class Schedule (Bidirectional Sticky)
Scroll down and to the right to see the effect:
Monday
Tuesday
Wednesday
Thursday
Friday
8:00
Math
Chinese
English
Physics
Chemistry
9:00
English
Math
Chemistry
Chinese
Physics
10:00
Physics
Chemistry
Math
English
Chinese
11:00
Chinese
Physics
Math
Chemistry
English
Notes
Browser compatibility:
position: stickyIt is well supported in modern browsers, but not supported at all in IE. If IE support is needed, you will need to use JavaScript or another polyfill solution as a fallback.
The effect of parent element overflow
If the parent element sets any of the following properties, sticky positioning willfail:
overflow: hidden
overflow: auto
overflow: scroll
A threshold must be set
At least one of top、right、bottom or left TheAmust be set, otherwise sticky will not take effect (the element will be laid out according to the normal document flow).
Threshold calculation
The threshold for sticky positioning iscalculated relative to the viewportrather than the parent element.
Comparison with other positioning values
| Positioning value | Whether it breaks out of the document flow | Fixed conditions | Compatibility |
|---|---|---|---|
static |
No | None | All browsers |
relative |
No | None | All browsers |
absolute |
Is | None | All browsers |
fixed |
Is | Always fixed | All browsers |
sticky |
No before the threshold, yes after the threshold | Scrolling past the threshold | Modern browsers (not supported in IE) |
Common Application Scenarios
Scenario 1: Sticky navigation
.nav {
position: sticky;
top: 0;
}
Scenario 2: Fixed table header
th {
position: sticky;
top: 0;
}
Scenario 3: Fixed sidebar
.sidebar {
position: sticky;
top: 20px;
}
Scenario 4: Article section headings
.chapter-header {
position: sticky;
top: 0;
background: white; /* 固定时显示背景 */
}
Summary
position: sticky is a very practical technique in modern web layout:
- IsRelative positioningandfixed positioninga combination of
- requires setting a threshold in at least one direction (top/right/bottom/left)
- Before scrolling past the threshold: laid out according to the normal document flow (relative positioning)
- After scrolling past the threshold: fixed at the current position (fixed positioning)
- no need for the parent element to set any positioning
- setting overflow on the parent element will cause failure
- well supported in modern browsers, not supported in IE
- commonly used for sticky navigation, fixed table headers, fixed sidebars, and fixed section headings
CSS position property