2

I have been working with CSS and HTML to create basically a banner and navbar. I've been trying to get it to stick to the very top, but after I saw it in chrome, I noticed both the body tag and the HTML tag are detatched. I've tried removing all the margins and padding from every single part that could be responsible for the space, but I couldn't get rid of it. What is a good way to do this?

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="style/normalize.css" />
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <div class="wrapper">
        <header>
            <nav>
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="demo.html">Demo</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>
</html>

CSS code:

html {
    margin: 0;
    padding: 0;
}

body {
    margin: 0;
    padding: 0;
}

.wrapper {
    margin: 0;
    padding: 0;
}

header {
    margin: 0;
    padding: 0;
}

a {
    text-decoration: none;
}

 nav
{
  text-align: left;
}

nav ul
{
  list-style: none;
  /*margin: 0 10; corrected*/
  margin: 0;
  padding: 0;
}

nav li
{
  display: inline-block;
}

nav a
{
  font-weight: 800;
  padding: 15px 10px;
  font-size: 1em;
}

Screenshot (as requested):

Screenshot

7
  • 6
    Please add nav ul { margin: 0; }. Commented Oct 19, 2014 at 8:58
  • 1
    can we have a screenshot of the problem highlighted as the question is not that clear Commented Oct 19, 2014 at 9:00
  • 2
    use reset css cssreset.com which will not allow elements to take default styles given by browsers Commented Oct 19, 2014 at 9:00
  • @emmanuel Thanks! That fixed it. It seems that is was because it said margin: 0 10; w/o the units (10px). Would you happen to know why? Commented Oct 19, 2014 at 9:04
  • 1
    margin: 0 10; is invalid CSS; measurements must have units. since the 10 is invalid and there's no such thing as a "partially" valid property, the whole line is ignored. Commented Oct 19, 2014 at 9:10

5 Answers 5

6

I tried every solution here and nothing really worked, mainly because I don't have navigation in my simple page or a header.

What worked for me was using the universal selector like this:

*, html, body {
   margin: 0;
   padding: 0;
}

4 years later, hope it helps someone.

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

Comments

1

2 years later - I just solved a similar problem - I had extra spaces. Try removing the blank spades before your first (it's indented with spaces or a tab in the code)

Comments

1

I had a similar problem just now and i resolved with

header {
    position: absolute;
    top: 0;
    right: 0;
}

Comments

1

You can use this code snippet

body{
    margin: 0%;   
}

for more information please refer to the given website:

https://www.w3schools.com/css/css_boxmodel.asp

Comments

0

You can remove the space using below code

nav ul {
list-style: outside none none;
padding: 0px;
margin-top: 0px;
}

1 Comment

Thanks, but the space is still there. It's at the top of the body element, but the body element is not at the top of the html element.

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.