0

I cannot seem to figure out how to get the space between each element to disappear. I mainly want to remove the white space on top. I have tried using margin-top:0 and it didn't work. Any advice would be greatly appreciated.

This is my html

<body>
    <div style="background-color:green;">
        <h1>top</h1>
    </div>
    <div style="background-color:blue;">
        <h1>body</h1>
    </div>
    <div style="background-color:red;">
        <h1>footer</h1>
    </div>
</body>

This is my css

body, div, header {
    padding:0;
    margin:0;
    margin-top:0px;
}
1
  • that approach is correct, but you are hiding "header" instead of h1 also try a cssreset.com Commented Feb 26, 2014 at 3:57

2 Answers 2

2

The space is caused by the User Agent Stylesheet (the default styles applied by your browser).

You need to target the H1 also.

html, body, div, h1 {
   margin:0;
}

Alternately, rather than individually targetting each element, you can use the universal CSS selector * to target everything at the start:

 * {
     margin:0;
     padding:0;
    }

To avoid trouble, you should start your CSS with a CSS reset. This sets all padding, margins etc on all elements to ensure browsers interpret your styles from the same starting point rather than relying on each browser's individual user agent stylesheet.

If you want to go modern, you can use Normalise.css

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help! I will definitely check out your links
2

Include h1 on your CSS:

Fiddle

body, div, header, h1 {
    padding:0;
    margin:0;
    margin-top:0px;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.