-1

When I apply background-color to a section, it leaves white spaces on the right and left side of the element.

The white space around the section.

I want to remove them so that the background-color fills out the whole width of the section. I think I could solve it with extra padding, but is that the right thing to do?

Maybe I am blind, but I also did not find any CSS from the parent elements, which mess up the CSS from the section. I attached the CSS and HTML down below. I believe the error is caused by the CSS.

HTML:

<!DOCTYPE html>
<html>
    <link rel="stylesheet" href="css/style.css">
    <header id="header">
        <img id="header-img" src="https://cdn.freecodecamp.org/testable-projects-fcc/images/product-landing-page-logo.png" alt="Logo">
        <nav id="navbar">
            <ul id="nav-list">
                <li><a class="nav-link" href="#newsletter">Newsletter</a></li>
                <li><a class="nav-link" href="#video">Video</a></li>
                <li><a class="nav-link" href="#privacy">Privacy</a></li>
            </ul>
        </nav>
    </header>

    <body>
        <section id="newsletter">
            <form id="form" action="https://www.freecodecamp.com/email-submit" method="post">
                <label for="email">Subscribe to our newsletter!</label>
                <br>
                <input type="email" id="email" placeholder="Your E-Mail" required>
                <br>
                <input type="submit" id="mail-news-submit" value="Subscribe!">
            </form>
        </section>
    </body>
</html>

CSS:

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap');

/* Debugger for unwanted CSS properties. Source: https://blog.wernull.com/2013/04/debug-ghost-css-elements-causing-unwanted-scrolling/
* {
    background: #000 !important;
    color: #0f0 !important;
    outline: solid #f00 1px !important;
  }
*/

:root {
    --smoky-black: #191516ff;
    --green-sheen: #72bda3ff;
    --apricot: #ffcab1ff;
    --brick-red: #ce4257ff;
    --little-boy-blue: #72a1e5ff;
    font-family: 'Roboto', sans-serif;
}

#header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    background-color:white;
    width: 100vw;
    height: 2.5vw;
    top: 0;
    left: 0;
}

#nav-list {
    padding-right: 1vw;
}

a {
    text-decoration: none;
    color: black;
}

a:hover {
    color: grey;
}

#nav-list li {
    list-style: none;
    display: inline-block;
}

#header-img {
    width: 20vw;
}

#newsletter {
    text-align: center;
    padding-top: 3vw;
    background-color: var(--apricot);
    margin: 0;
}

#email, #mail-news-submit {
    margin-top: 0.3vw;
    margin-bottom: 0.3vw;
}

#mail-news-submit {
    color: white;
    background-color: var(--smoky-black);
    padding: 0.5em 1em;
    border-radius: 10em;
    border: none;
}
Marten
  • 645
  • 7
  • 21

2 Answers2

2

You have to remove margin and padding from the body which is set by default.

Let me know if that helps. If yes, please mark my answer as accepted.

body {
margin: 0;
padding: 0;
}
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
-1

There is margin set by default by browser- user agent stylesheet.(8px in Google Chrome) You need to set the margin to body to 0 as mentioned in above answer.

body {
margin: 0;
}
Murali
  • 21
  • 5