Search code examples
htmlcssbackground-imagebackground-coloropacity

Why are opacity wrapping my main content?


I want to have a background image in my main section. Then there will be an empty div below it. It will have an opacity of 0.8. Below that there will be a div that will contain my content and that content will have an opacity of 1. Or that I gave 0.8 opacity above so that it doesn't get into the bottom content box. How to do it?

I want to make it like this:

.opening-job-parent{
    background-image: url("./images/header-banner-image-one.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    height: 30vh;
    width: 100%;
    position: relative;
    margin-bottom: 50px;
}

.opening-job-parent .empty-div{
    background-color: #7f73eb;
    width: 100%;
    height: 100%;
    position: absolute;
    opacity: 0.8;
}

.opening-job-container{
    opacity: 1;
    color: white;
}
<section class="opening-job-parent">
    <div class="empty-div">

    </div>
    <div class="opening-job-container">
        <div class="opening-job-details">
            <div class="opening-job-text">
                <h1>Over 10k opening jobs</h1>
                <p>If you are looking for free HTML templates, <br> you may visit Tooplate website. <br> If you need
                    a
                    collection of free templates, <br> you can visit Too CSS website.</p>
            </div>
            <div class="opening-job-account-link">
                <a href="#">Create an account</a>
                <a href="#">Post a job</a>
            </div>
        </div>
    </div>
</section>


Solution

  • I have solved your problem.

    .one {
        position: relative;
    }
    
    .one img {
        width: 100%;
        height: 100%;
        background-position: 50%;
        background-repeat: no-repeat;
    }
    
    .one .empty-div {
        background-color: #7f73eb;
        width: 100%;
        height: 100%;
        max-width: 520px;
        max-height: 520px;
        position: absolute;
        opacity: 0.8;
        top: 30%;
        right: 20%;
    }
    
    .one .opening-job-container {
        position: absolute;
        width: 100%;
        max-width: 420px;
        height: 100%;
        top: 0;
        left: 0;
        transition: .3 ease;
        gap: 35px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        text-align: center;
        opacity: 1;
        color: black;
    }
    <div class="one p-0">
        <img class="img-fluid" src="https://fastly.picsum.photos/id/1059/600/495.jpg?hmac=tULMNx8RZcb-7PPKKp-o_-hNedg3OdCJNDG2NUGHXiU" alt="new1">
        <div class="empty-div"></div>
        <div class="opening-job-container">
            <div class="opening-job-details">
                <div class="opening-job-text">
                    <h1>Over 10k opening jobs</h1>
                    <p>If you are looking for free HTML templates, <br> you may visit Tooplate website. <br> If you need
                        a
                        collection of free templates, <br> you can visit Too CSS website.</p>
                </div>
                <div class="opening-job-account-link">
                    <a href="#">Create an account</a>
                    <a href="#">Post a job</a>
                </div>
            </div>
        </div>
    </div>