Home > Web Front-end > CSS Tutorial > How to Remove Extra Space Above an HTML Header?

How to Remove Extra Space Above an HTML Header?

Patricia Arquette
Release: 2024-12-23 02:23:13
Original
888 people have browsed it

How to Remove Extra Space Above an HTML Header?

Removing Margin Around an HTML Header

When displaying a header element, some users encounter unnecessary space above it. To address this issue, we can leverage the following CSS code:

body {
  margin: 0px;
  padding: 0px;
}

header {
  margin: 0px;
  padding: 0px;
  height: 20em;
  background-color: #C0C0C0;
}

h1 {
  margin-top: 0;
}
Copy after login

This adjustment targets the

element specifically, setting its top margin to 0 to eliminate the unwanted spacing.

The initial issue stemmed from a phenomenon known as margin collapsing. In CSS, when two elements with margins overlap, the resulting margin is equal to the larger of the two margins. This means that in the provided HTML and CSS code, the margin around the header element was collapsing with the margin around the

element, resulting in the extra space above the header. By setting the margin-top of the

element to 0, we effectively disable margin collapsing and eliminate the unwanted space.

The above is the detailed content of How to Remove Extra Space Above an HTML Header?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template