CSS position: fixed Property Value
position: fixed is a value used in CSS positioning properties forfixed positioning. Elements using fixed positioning willbreaks out of the document flow, relative to thebrowser viewport(viewport), and willnot move as the page scrolls。
This is the core technique for implementing fixed navigation bars, back-to-top buttons, sidebar ads, and similar features.
Word meaning:fixed comes from Latin fixare, meaning "fixed and installed," indicating that once the element's position is determined, it no longer changes.
Basic Syntax
position: fixed Needs to be used together with top、right、bottom、left property to specify the position relative to the viewport.
/* 语法 */
position: fixed;
/* Common combinations: fixed at the top */
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
}
/* Fixed in the bottom right corner */
.back-to-top {
position: fixed;
bottom: 20px;
right: 20px;
}
Core Features
Feature 1: Positioning relative to the viewport
A fixed-positioned elementis not affected by its parent element, and is always positioned relative to the visible area of the browser window.
Feature 2: Out of the document flow
The difference between absolute In the same way,fixed Elementdoes not occupy document space, surrounding elements arrange normally,ignoringthe existence of the fixed-positioned element.
Feature 3: Fixed, does not move with scrolling
This is fixed The difference between absolute 'sKey distinction: No matter how the page scrolls, the fixed-positioned element always stays at the specified position.
Feature 4: Activates z-index
Fixed positioning activates the z-index property, allowing you to control the stacking order of elements in the viewport.
Example
Let us understand in depth through examples how position: fixed characteristic.
Example 1: Fixed top navigation bar
This is fixed The most common application scenario: a fixed navigation bar.
Example
Fixed positioning example
This is a fixed navigation bar implemented with position: fixed.
No matter how you scroll the page, the navigation bar stays at the top of the page.
Content area 1
This is the first section of content, used to fill the page so a scrollbar appears.
Content area 2
This is the second section of content.
Content area 3
This is the third section of content.
Content area 4
This is the fourth section of content.
Example 2: Back-to-top button fixed at the bottom-right corner
Fixed positioning is also commonly used to make a "back to top" button:
Example
Back-to-top button example
Scroll the page to see the fixed button effect.
↑
Example 3: Fixed sidebar
Fixed positioning can also implement a fixed sidebar:
Example
Main content area
This is the main content, which moves with the scrollbar.
Content paragraph 1 - fills the page to create a scrolling effect.
Content paragraph 2
Content paragraph 3
Content paragraph 4
Content paragraph 5
Content paragraph 6
Content paragraph 7
Content paragraph 8
Example 4: Footer fixed at the bottom
Sometimes you need to fix the footer of a page with little content at the bottom:
Example
A page with little content
This page has very little content, but the footer is still fixed at the bottom.
Implemented using flexbox's flex: 1 together with position: fixed.
Example 5: Fixed overlay
Fixed positioning can also be used to create a full-screen overlay:
Example
Fixed overlay example
Click the button to open the pop-up.
Notes
Mobile compatibility issues:In iOS Safari and some mobile browsers,
position: fixedthere may be compatibility issues. Elements may move when an input field gains focus. Solutions include: using-webkit-position: fixedor settingtransform: translateZ(0)to enable hardware acceleration.
z-index stacking level
A fixed-positioned element's z-index is calculated relative to theviewport, not to its parent element.
Differences from absolute positioning
| Feature | position: absolute | position: fixed |
|---|---|---|
| Positioning reference | nearest positioned ancestor element | Browser viewport (viewport) |
| Page scroll | Moves with page scrolling | Fixed, does not move |
| Displays normally | May not display or behave abnormally |
Prevent fixed-positioned elements from being printed
@media print {
.fixed-element {
position: static; /* 打印时取消固定定位 */
}
}
Common Application Scenarios
Scenario 1: Fixed top navigation
.nav-header {
position: fixed;
top: 0;
width: 100%;
height: 50px;
z-index: 100;
}
Scenario 2: Fixed back button
.floating-btn {
position: fixed;
bottom: 20px;
right: 20px;
}
Scenario 3: Chat window
.chat-widget {
position: fixed;
bottom: 20px;
right: 20px;
width: 300px;
height: 400px;
}
Summary
position: fixed is the core technique for implementing fixed UI elements:
- relative tobrowser viewportpositioning, not affected by the parent element
- fixed, does not move with page scrollingMobile
- Elementbreaks out of the document flow, does not occupy space
- Activate
z-index, can control the stacking order - commonly used for fixed navigation bars, back-to-top buttons, fixed sidebars, and overlay pop-ups
- pay attention to compatibility issues on mobile devices
CSS position property