Margin-Top Pushes Div Down: Solution Revealed
Applying a top margin to the first visible element on a page can cause the entire containing div to move downward. This can be particularly noticeable in header sections. To resolve this issue, apply the following CSS property to the parent div:
overflow: auto;
This property allows the parent div to automatically accommodate the increased height caused by the top margin on the child element.
Here is a modified sample code snippet incorporating the solution:
div#header{ width: 100%; background-color: #eee; position: relative; overflow: auto; } div#header h1{ text-align: center; width: 375px; height: 50px; margin: 50px auto; font-size: 220%; background: url('../../images/name_logo.png') no-repeat; }
By adding the overflow: auto property, the #header div will automatically expand to fit the height of the h1 element, preventing it from pushing the entire header down.
The above is the detailed content of Why Does a Top Margin Push My Div Down, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!