Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Laura Kyle
Laura Kyle
19,794 Points

Positioning blues: Image overlapping div

I'm missing something that is no doubt an easy fix, but no matter how much I play around with it I can't seem to make it work.

I'm trying to get my div (topblock) to fill 20% in height. However, it seems to only leave enough room for the header. The image (banner) is the height I want it to be. Is it an overlap issue?

<header> 
    <div id="topblock"> 
        <h1>My Headline</h1> 
    </div>  
</header>


    <img id="banner" src="------" alt="------">
#topblock {
    width: 100%;
    height: 20%;
    padding: 0px auto;
    margin: 0px auto;
    background: linear-gradient(black, #121211);
}
 #banner {
    height: 22%;
    width: 100%;
    padding: 0px;
    margin: 0 auto;
}

h1 {
font-family: 'Archivo Narrow', sans-serif;
font-weight: 700;
margin: 0px auto;
padding: 0px auto;
color: #EE66FF;
}

I think you may be looking at it the wrong way. You want your #topblock div's height to be 20% of what? If you specify a height in a percentage, by default, CSS will take it as 20% height of the total height of the closest ancestor element with a position of relative or absolute (maybe fixed too, I'm not sure).

So if you want to make it be 20% of the height of your header element (which is its closest ancestor, or parent) you have to specify a height for your header element and set its position to relative (or absolute if you want).

Sorry if that's confusing. I highly recommend the tutorial on CSS positioning to understand how that all works better.

Laura Kyle
Laura Kyle
19,794 Points

That's not confusing at all, that's a huge help and clears up the misunderstanding I seem to be having. Thank you!