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

Elvis Reinis Zeltītis
Elvis Reinis Zeltītis
1,925 Points

Image hiding behind background

Hello!

So im trying to make a website to see how far will I be able to get, and in middle of the way I encountered a problem. The problem is when im adding an image inside or outsive my navigation div, the image appears under background-color. Have tried a few things, none of them worked sadly.

HTML file

<!doctype html>
<html>
     <head>
        <title>Welcome!</title>
    </head>
        <link href="styles/main.css" rel="stylesheet"/>
    <body>
    <header>
        <div class='nav'>
            <img src="img/logo.png">
            <ul>
                <li><a href='#'>Home</a></li>
                <li><a href='#'>About</a></li>
                <li><a href='#'>Contacts</a></li>
            </ul>
        </div>
    </header>
    <section>
    </section>
    <footer>
    </footer>

    <script src="scripts/main.js"></script>
    </body>
</html>

CSS file

/***** Body *****/

body {
    margin: 0;
}


/***** Header *****/
#logo {

}

.nav ul {
    list-style-type: none;
    margin: 0;
    padding: 150px 0 0 0;
    width: 100px;
    position: fixed;
    top: 0;
    bottom: 0;

    background-color: #f1f1f1;
}

.nav ul li a {
    display: block;
    color: #000;
    padding: 12px 0 12px 20px;
    text-decoration: none; /* Removes default links color and underline */
}

.nav ul li a:hover {
    background-color: #555;
    color: white;
}

-- Edit

I've found the solution to this problem so I decided to post it rather than delete it, because it could help someone out!

All I did Was I added z-index to image and set it's position to relative! :) If anyone can explain it more detailed I would me more than happy!

#logo {
    position: relative;
    z-index: 1;
}

1 Answer

Steven Parker
Steven Parker
229,744 Points

By default, items will be rendered in the order they occur in the document, so later items will be "on top" of earlier ones. Normal document flow prevents overlap, but by using fixed position you placed your ul element over your image.

By assigning a position with z-index, you override the default stacking order and put your image on top.