Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

stephanie harrison roberts
stephanie harrison roberts
1,713 Points

Margins around a background image

Hi All, Anyone tell me where Im going wrong? dont want any white margins around my image.....hat do i need to add?

thanks

<!DOCTYPE html>
<html>
    <title>Obi & El Media Agency</title>
    <link rel="stylesheet" href="New.css">
    <body>
        <div class="main_container">
<!--            <h1 class="main_title">Obi & El</h1>-->

        </div>


    </body>

</html>

```CSS
html{
    height: 100%;
    height: 100vh;

}
.main_container {
    background: url(Brackets_Images/typewriter.jpg)no-repeat;
    background-size: cover;
/*    border: 4px solid red;*/
    height: 100vh; 
    max-width: 50%;
    margin: 0;
    background-position:  50% 50%;

}

3 Answers

Steven Parker
Steven Parker
231,261 Points

Your container with the background is inside the body, and the body has margins by default. You can explicitly remove them in the CSS:

body { margin: 0; }

Also, the HTML code seems to be missing the starting and ending head tags.

I like to universally remove margins and padding to not have as many issues with positioning.

  • { Margin: 0 auto; Padding: 0; Box-sizing: border-box; }

But doing so to the body instead should help too I believe, if you’d rather not use the universal approach.

Steven Parker
Steven Parker
231,261 Points

:warning: Universally removing margins may have unintended side-effects, such as causing all paragraphs to run together.