
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
Detect when an Element Gets Fixed in CSS position:sticky using Intersection Observer
By applying various CSS styles to the element with the sticky position, we can easily detect it.
The following example shows this property.
Example
<!DOCTYPE html> <html> <head> <style> #first { background-color: lightgrey; height: 10px; } #navbar-top { background-color: lightgrey; height: 2px; } #container { position: sticky; top: 0; box-shadow: inset 0 0 25px navy; height: 55px; text-align: center; font-size: 24x; line-height: 55px; font-weight: bold; transition: font-size 0.4s ease-in; } .sticky-navbar { box-shadow: inset 0 0 15px orange!important; font-size: 20px !important; } #parent-container { background-color: aliceblue; height: 3300px; } </style> </head> <body> <div id="first"></div> <div id="navbar-top"></div> <div id="container">Watch Me!</div> <div id="parent-container"></div> <script> let newObserver = new IntersectionObserver(function(entries) { if(entries[0].intersectionRatio === 0) document.querySelector("#container").classList.add("sticky-navbar"); else if(entries[0].intersectionRatio === 1) document.querySelector("#container").classList.remove("sticky-navbar"); }, { threshold: [0,1] }); newObserver.observe(document.querySelector("#navbar-top")); </script> </body> </html>
Output
This will produce the following result −
- Related Articles
- CSS position: sticky;
- Role of CSS position: sticky;
- CSS position: fixed;
- How to create a fixed/sticky footer with CSS?
- How to create a sticky element with CSS?
- How to create a fixed/sticky header on scroll with CSS and JavaScript?
- Fixed Positioning Using CSS
- Position your HTML element with CSS
- Execute a script when the element gets focus in HTML?
- Create a sticky navbar with CSS
- Execute a script when the element gets user input in HTML?
- Setting Background Position using CSS
- Disable text wrapping inside an element using CSS
- Fixed Positioning in CSS
- How to create a sticky image with CSS?

Advertisements