Home >Web Front-end >CSS Tutorial >Why Does Adding a Top Margin to a Header Push the Entire Header Down?
When adding a top margin to a header div, why does the entire header div get pushed down?
This behavior occurs because a top margin applied to the first visible element in a container often causes it to extend beyond its parent element's available space. As a result, the parent element expands vertically to accommodate the margin space, pushing all subsequent elements down.
Sample Code Snippet:
div#header { width: 100%; background-color: #eee; position: relative; } div#header h1 { text-align: center; width: 375px; height: 50px; margin: 50px auto; font-size: 220%; background: url('../../images/name_logo.png') no-repeat; }
Solution:
To resolve this issue, you can add the overflow:auto property to the parent div. This will cause the parent div to scroll when the margin extends beyond its available space, keeping the header div from shifting down.
div#header { overflow: auto; ... }
For more details, refer to the following link:
The above is the detailed content of Why Does Adding a Top Margin to a Header Push the Entire Header Down?. For more information, please follow other related articles on the PHP Chinese website!