Home > Software design >  How can I add linear-gradient on the image tag in CSS?
How can I add linear-gradient on the image tag in CSS?

Time:10-03

I am trying to add linear gradient on an image tag but it doesn't work. How can I do it?

#Hero-Image-Div {
    width: 100%;
    height: 37rem;
    background-color: #0072b1;
    position: absolute;
    top: 7.1rem;
    left: 0rem;
}

#Hero-Image {
    width: 100%;
    height: 100%;
    background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("photographer.jpg");
}
<div id="Hero-Image-Div">
    <img id="Hero-Image" src="Homepage-Hero-Image.jpg" alt="">
</div>

CodePudding user response:

Wrap the image element in a wrapper(#Hero_Image-Div). Then start creating the overlay with pseudo-element ::before ::after. Remember that positions are important.

#Hero-Image-Div {
    /* THIS */
    position: relative;
    width: 100%;
    height: 37rem;
    background-color: #0072b1;
    /* position: absolute;
    top: 7.1rem;
    left: 0rem; */
}

/* THIS */
#Hero-Image-Div::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
    background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5));
}

#Hero-Image {
    width: 100%;
    height: 100%;
}
<div id="Hero-Image-Div">
    <img id="Hero-Image" src="https://picsum.photos/200/300" alt="">
</div>

CodePudding user response:

I mention my solution here. just replace my images path with your. and inform me if it is working or not from your end.

--------------------------HTML File--------------------------

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Overlay Effect</title>

        <!-- styles -->
        <link rel="stylesheet" href="./styles.css" />
    </head>
    <body>
        <div >
            <h1>i'm folder image</h1>
        </div>
    </body>
</html>

--------------------------CSS File--------------------------

   * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    /* way 1  */
    div {
        min-height: 100vh;
        color: white;
        border: 2px solid red;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 2rem;
        text-transform: uppercase;
        text-align: center;
    }
    
    .folder-img {
        background: linear-gradient(
                to top,
                rgba(0, 0, 0, 0.3),
                rgba(255, 100, 239, 0.5)
            ),
            url("./big.jpeg") center/cover fixed no-repeat;
    }
  • Related