Why Can a Fixed-Positioned Element Shift with Its Sibling Elements?

Patricia Arquette
Release: 2024-10-24 18:43:26
Original
373 people have browsed it

Why Can a Fixed-Positioned Element Shift with Its Sibling Elements?

Why a Fixed Position Element Moves with Its Sibling

Despite being removed from the document flow with position: fixed, an element can still be affected by its siblings. This occurs due to the way the viewport is calculated.

The viewport is the area within the browser window that is visible to the user. It's calculated using the in-flow elements, which are the elements that participate in the regular document flow.

In the provided HTML, the first in-flow element is

, which has a margin-top: 90px. Since the
element is fixed, it's not considered in the viewport calculation.

However, the default margin of 8px on the element collapses with the margin on

. This results in both elements moving down by 90px, affecting the position of the fixed header.

To fix this, simply add top: 0 to the header CSS to place it at the top of the viewport, independent of the sibling elements' margins.

The above is the detailed content of Why Can a Fixed-Positioned Element Shift with Its Sibling Elements?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!