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

Why does margin for div with text enlarge background image?

When I am trying to give margin-top for div with text (.titie and .logo), background-image (.main) is enlarging. Why is it happening?

.header_list {
    float: right;
    margin-right: 12%;
    padding: 1% 0%;
}

.header_list li {
    list-style: none;
    display: inline;
    font-family: agorathin;
    font-size: 1.8em;
    padding-right: 7%;
}

.logo {
    float: left;
    padding-top: -1%;
    margin-left: 5%;
}

.main {
    height: 100%;
    padding-top: 80px;
    padding-bottom: 50%;
    background-image: -webkit-linear-gradient(rgba(0, 0, 0, 0.6) 2%, rgba(0, 0, 0, 0.74) 96%), url('../images/Slideshow_Sony_Website.jpeg');
    background-image: linear-gradient(rgba(0, 0, 0, 0.6) 2%, rgba(0, 0, 0, 0.74) 96%), url('../images/Slideshow_Sony_Website.jpeg');
    background-position: center;
    background-size: auto, cover;
    color: white;
    text-align: center;
}

.title {
    margin: 0 auto;
    margin: 20%;
}

1 Answer

Your background-size is set to "cover" on the .main div. If you add margin to the elements inside of .main, the .main div gets bigger and the background-image has to cover more space.

Thanks. I changed it. But title doesn't go down

.main {
    height: 100%;
    padding-bottom: 50%;
    background-image: -webkit-linear-gradient(rgba(0, 0, 0, 0.6) 2%, rgba(0, 0, 0, 0.74) 96%), url('../images/Slideshow_Sony_Website.jpeg');
    background-image: linear-gradient(rgba(0, 0, 0, 0.6) 2%, rgba(0, 0, 0, 0.74) 96%), url('../images/Slideshow_Sony_Website.jpeg');
    background-position: center;
    background-size: auto;
    color: white;
    text-align: center;
}

.title {
    margin: 0 auto;
    clear: both;
    margin-top: 30%;
}

Could you please give me a hint for a background image settings that won't be changing according to other elements?

Right now I have to put padding-botom and top to enlarge height of the background. Is there any way to set the properties for the background image so it always width is always 100% and the height is auto?