Home >Web Front-end >CSS Tutorial >How Can I Set a Container's Height to Encapsulate Absolutely Positioned Children?
Container Height with Absolutely Positioned Children
In this situation, where containers have only absolutely / relatively positioned children, determining the container's height to keep its children contained can be challenging.
In CSS, absolutely positioned elements are exempt from the regular document flow. As a result, their dimensions do not impact their parent's dimensions.
Possible Solution with JavaScript:
If maintaining absolute positioning is essential, you can employ JavaScript to accomplish the desired effect. Specifically, you can write a script that retrieves the height of the absolutely positioned children after they have rendered and uses that value to adjust the height of the parent.
Alternative Solution with Float and Overflow:
An alternative approach is to utilize float: left/float:right and margins to mimic absolute positioning while preserving the children within the document flow. To accommodate the height of the children, you can apply overflow: hidden to the parent container (or employ other clearfix techniques).
The above is the detailed content of How Can I Set a Container's Height to Encapsulate Absolutely Positioned Children?. For more information, please follow other related articles on the PHP Chinese website!