Home >Web Front-end >CSS Tutorial >How to Position an Element Fixed Relative to Its Parent or the Browser Window?
Positioning Elements: Fixed Relative to Parent or Window
Your question focuses on positioning an element "fixed" relative to its parent using CSS. When an element is positioned fixed, its position is determined relative to the viewport, not its parent element.
Positioning an Element Fixed Relative to Parent
If you want to position an element fixed relative to its parent, you can use the following CSS properties:
#parent { position: relative; } #child { position: absolute; top: 50px; left: 50px; }
This will position the #child element 50 pixels down and 50 pixels to the right of its parent, #parent.
Positioning an Element Fixed Relative to Window
To position an element fixed relative to the window, you can use the following CSS properties:
#element { position: fixed; top: 50px; right: 50px; }
This will position the #element element 50 pixels down from the top and 50 pixels from the right edge of the browser window.
Example
Consider the following example:
<div>
#parent { width: 300px; background: orange; margin: 0 auto; position: relative; } #child { position: absolute; right: 0; top: 120px; }
In this example, the #child element is positioned fixed relative to its parent, #parent. It will move with the parent element as it scrolls vertically, but it will remain 0 pixels to the right and 120 pixels below the top of the parent.
The above is the detailed content of How to Position an Element Fixed Relative to Its Parent or the Browser Window?. For more information, please follow other related articles on the PHP Chinese website!