Home >Web Front-end >CSS Tutorial >Why Isn't My Absolutely Positioned Div Moving?
Absolute Positioning Not Working
In an attempt to position a div with the ID "absPos" absolutely within its parent div, a developer encountered an issue where the div remained in the page's top left corner.
Explanation:
Elements with absolute positioning are displaced based on their offsetParent, which is the closest positioned ancestor. In the sample code provided, no ancestors had "positioned" elements, causing the div to be offset from the body element, its offsetParent.
Solution:
To rectify this issue, position:relative should be applied to the parent div. This transforms it into a positioned element and establishes it as the offsetParent for its child.
Modified Code:
<html> <body> <div>
The above is the detailed content of Why Isn't My Absolutely Positioned Div Moving?. For more information, please follow other related articles on the PHP Chinese website!