Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Sticky Positioning for Dynamic Layouts | Mastering CSS Positioning
Advanced CSS Techniques

book
Sticky Positioning for Dynamic Layouts

Sticky positioning combines aspects of both relative and fixed positioning. When an element is positioned sticky, it will behave like a relatively positioned element until it reaches a certain threshold on the page. At this point, it will switch to acting like a fixed element.

css
position: sticky;

Let's consider the following illustration. The element with the text content I am sticky has the sticky positioning.

When an element is inside a container, and the container remains within the viewport, the element will behave as if it were relatively positioned. However, when a portion of the parent container moves out of the viewport, the element becomes fixed in place as long as some part of the container remains on the screen. Once the entire container exits the viewport, relative positioning is reactivated, and the element will disappear from view along with the container.

Let's have some practice and create a couple of sections with sticky titles to them. So a user could always see the title of that section which he reads now, and then it is supposed to disappear on scroll.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div class="container">
<h3 class="title">Home</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab,
consectetur neque labore ea nesciunt veritatis nam qui at aut atque quo
sint provident facere quisquam. Impedit beatae eligendi ut quisquam
blanditiis enim quos! Explicabo nihil alias quo exercitationem
repellendus ipsum harum! Voluptas cum, qui quibusdam, tempora eveniet
iusto sit delectus nulla blanditiis vitae distinctio? Est eligendi
officia repellendus praesentium iusto esse deleniti, temporibus qui
asperiores quidem laboriosam eos maiores quos alias! Natus, consectetur
iste, rerum optio mollitia facilis obcaecati nesciunt ut vel officiis
aperiam temporibus. Quas, dolorem! Fugit ipsa, officiis fuga nisi
obcaecati optio inventore, beatae accusamus cum quod vel voluptas
delectus est provident repellendus dolore, et nesciunt dignissimos.
Dicta magnam facere sint distinctio magni eveniet cumque placeat
excepturi ducimus, ad reiciendis voluptatem asperiores quis aliquid
repudiandae impedit, quasi eius animi dolore. Alias praesentium id, ad
quidem earum eos adipisci ea minus ab eveniet excepturi mollitia vitae.
In, eveniet quos.
</p>
</div>
<div class="container">
<h3 class="title">Product</h3>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ab,
consectetur neque labore ea nesciunt veritatis nam qui at aut atque quo
sint provident facere quisquam. Impedit beatae eligendi ut quisquam
blanditiis enim quos! Explicabo nihil alias quo exercitationem
repellendus ipsum harum! Voluptas cum, qui quibusdam, tempora eveniet

Note

It's important to mention that we must specify the top or bottom property, so the browser can understand where we expect to stick an element.

question mark

What is the behavior of the element with the position: sticky property?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 8
some-alt