Home >Web Front-end >CSS Tutorial >How Does an Absolutely Positioned Div Affect Its Parent Container's Height?
Absolute Positioned Div Impact on Parent Height
When working with CSS, it's crucial to consider how elements' positioning affects their surrounding elements. One common challenge is ensuring that an absolutely positioned div doesn't ignore other elements within its parent container.
Consider the following HTML and CSS code:
<div>
parent { position: relative; width: 100%; } child1 { width: auto; margin-left: 160px; } child2 { width: 145px; position: absolute; top: 0px; bottom: 0px; }
The goal is to position child2 before child1, especially on mobile devices where navigation should be at the bottom. However, child2 has a dynamic height, which means setting a fixed height for the parent div is not an option.
Unfortunately, since absolutely positioned elements are removed from the flow, they are ignored by other elements within the parent container. Setting overflow:hidden on the parent div or using Clearfix won't help.
The Solution
As recognized in the problem statement, the solution lies in understanding that absolutely positioned elements are taken out of the flow. Therefore, it's not possible to set the parent's height based on an absolutely positioned element.
Options to Consider:
The above is the detailed content of How Does an Absolutely Positioned Div Affect Its Parent Container's Height?. For more information, please follow other related articles on the PHP Chinese website!