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

willkey
willkey
3,650 Points

Website Learning Project. Want to change Background image opacity but NOT the elements on top. e.g. navigation bar etc..

I am trying to change the opacity of a Background image, BUT it is also changing the opacity of the elements on top as well e.g. navigation bar, social media bar. BUT I don't want this to have opacity changed. Anyone got any ideas on how to achieve this?.

<div class="site-wrapper">
            <header class="header-wrapper">
                <div class="info-bar">
                    <nav>
                        <ul class="social-media-icons">
                            <li><a href="#"><i class="fas fa-envelope-square"></i>&nbsp;info@acme-accounting.com</a></li>
                            <li class="margin-right"><a href="#"><i class="fas fa-envelope-square"></i>&nbsp;Call Us: 01234 567 891</a></li>
                            <li><a href="#"><i class="fab fa-facebook"></i></a></li>
                            <li><a href="#"><i class="fab fa-twitter-square"></i></a></li>
                            <li><a href="#"><i class="fab fa-instagram"></i></a></li>
                            <li><a href="#"><i class="fab fa-linkedin"></i></a></li>
                        </ul>
                    </nav>
                </div>
                <nav class="navbar header-nav">
                    <ul class="flex">
                        <a class="branding margin-right" href="#">Acme Accounting</a>
                        <li><a href="#">Home</a></li>
                        <li><a href="#">Tax Services</a></li>
                        <li><a href="#">Accounting Services</a></li>
                    </ul>
                </nav>
            </header>

and the CSS code below.

* {
    margin: 0;
    padding: 0;
    text-decoration: none;
    list-style-type: none;
}

html {
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

* {
    -webkit-box-sizing: inherit;
    box-sizing: inherit;
}

*::after, *::before {
    -webkit-box-sizing: inherit;
    box-sizing: inherit;
}

html, body {
    height: 100%;
    width: 100%;
}

.site-wrapper {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 90px 1fr 60px;
    grid-template-areas: "header-wrapper" "main-wrapper" "footer-wrapper";
}

.header-wrapper {
    grid-area: header-wrapper;
}

.main {
    grid-area: main;
}

.footer-wrapper {
    grid-area: footer-wrapper;
}

.header-wrapper {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 30px 60px;
    grid-template-areas: "info-bar" "header-nav";
}

.info-bar {
    grid-area: info-bar;
}

.header-nav {
    grid-area: header-nav;
}

.site-wrapper {
    background-image: url("../../images/bg-img2.jpg");
    background-position: center;
    background-size: cover;
    background-repeat: no-repeat;
    height: 100vh;
}

.info-bar {
    height: 30px;
    line-height: 30px;
}

.info-bar .social-media-icons {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
}

.info-bar .social-media-icons a {
    color: #000;
    font-weight: 600;
}

.margin-right {
    margin-right: auto;
}

.header-nav {
    height: 60px;
    line-height: 60px;
}

.flex {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: start;
    -ms-flex-pack: start;
    justify-content: flex-start;
}

.branding {
    color: #71c441;
    font-size: 32px;
}

.branding:hover {
    color: #724733;
    font-weight: bolder;
}

.navbar a {
    display: block;
    margin-left: 25px;
}

3 Answers

Dylan Conroy
Dylan Conroy
9,144 Points

You could make another div the same size as your content container, and then use z-index to stack them, and explicitly set the background opacity.

Paul Walker
Paul Walker
28,904 Points

Have you try to put another class in your <div class="site-wapper cover-image"> something like this and then use the opacity on the cover-image class. Also, I've noticed that the background-image: url("../../images/bg-img2.jpg"), I think all you need is url("../images/bg-img2.jpg").

Gari Merrifield
Gari Merrifield
9,598 Points

It looks like this may have already been answered some time ago : https://teamtreehouse.com/community/backgroundimage-opacity

and another reference : https://css-tricks.com/snippets/css/transparent-background-images/

They appear to just be more detailed versions of Dylan and Paul's answers