Why Doesn't 'position: sticky' Work When Setting 'height'?

Patricia Arquette
Release: 2024-11-23 11:04:18
Original
838 people have browsed it

Why Doesn't 'position: sticky' Work When Setting 'height'?

'position:sticky' not working when setting 'height'

As you've noticed, setting the height property on your footer element prevents it from sticking to the top of the page when you scroll down. This behavior occurs because of the way the sticky positioning algorithm works.

According to the CSS specification, a sticky element is treated as relatively positioned until its containing block crosses a specified threshold. In your case, the containing block is the body element, which has a height of 100%. When the main element, which has a height of 92%, is scrolled down, the footer element reaches the bottom of the body element and is considered to be at the opposite edge of its containing block. Therefore, it is no longer treated as sticky and stays at the bottom of the page.

To fix this issue, you can remove the height property from the main element. This will allow the footer element to stick to the top of the page as intended. Alternatively, you can set the height property on the body element to 100vh instead of 100%. This will ensure that the body element is always the same height as the viewport, and the footer element will stick to the top of the page regardless of the height of the main element.

The above is the detailed content of Why Doesn't 'position: sticky' Work When Setting 'height'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template