How to Darken an Image with CSS
Nazanin Ashrafi

Nazanin Ashrafi @nazanin_ashrafi

About: Trying to teach myself to be a web dev #Self_teaching (with the help of community) I write about the thing I wish I knew earlier...

Joined:
Aug 12, 2020

How to Darken an Image with CSS

Publish Date: Nov 22 '20
72 15

There was a time when I had to look everywhere to find out how to make images darker and now I know 3 ways to do this. so lets get to the point.

1. Using opacity :



Setting the basic:

This is the original pic:
Mamamoo Solar



Setting css :

First we lower the opacity of the pic



img {
opacity: 0.5;
}


Enter fullscreen mode Exit fullscreen mode

By setting the opacity to 0.5 , the images will turn white(ish)

Now all we need to do to make it darker is to change the background color to black :



ul {
background-color: black;
}


Enter fullscreen mode Exit fullscreen mode



We can use this to create cool hover effect. you can check out the pen that I made

2. Using filter property:

Just use filter: brightness(50%); for the image to lower the brightness.

3. RGBA colors:

Let's say you want to set an image as as background. if you don't darken it ,everything you put on the image like buttons, texts etc will not look cool. you need to darken it to make other stuff stand out
So how do we do that?
It's simple. let me show you

Setting the basic:



header {
    width: 100%;
    background-image: url(EauDp1hUcAAE-bK.jpg);
    background-position: center;
    background-size: cover;
}


Enter fullscreen mode Exit fullscreen mode

For making it darker, we add linear-gradient(black,black).
Choose a color and lower the transparency of the color



header {
background-image: linear-gradient(rgba(0, 0, 0, 0.527),rgba(0, 0, 0, 0.5)) , url(EauDp1hUcAAE-bK.jpg);
}


Enter fullscreen mode Exit fullscreen mode





To sum it up :

  • Use opacity and filter property to darken an image and create cool hover effect with it.
  • Use RGBA colors to make your background image darker.

You can connect with me on twitter

Comments 15 total

  • Ritvik Dubey
    Ritvik DubeyNov 23, 2020

    That's really helpful 🙌

  • shahryar slg
    shahryar slgAug 10, 2021

    Thank you for your great post. it helped me.
    And I got cheerful when I saw the first search result Is an Iranian's article, Congratulations :)

  • EmbedConner Code
    EmbedConner CodeAug 13, 2021

    Thanks for your help!

  • Joseph
    JosephNov 13, 2021

    thank you so much, this came very handy.

  • E-Robin
    E-RobinDec 4, 2021

    Thanks for this code It really helps creating my first website

  • enesstr
    enesstrMay 18, 2022

    third section helped

  • Sohanur Rahman Emon
    Sohanur Rahman EmonJul 28, 2022

    It is really cool

  • Dipesh Joshi
    Dipesh JoshiDec 30, 2022

    this is helpful today I'm searching on google and its leads to your Artical.

  • Lawrence Narh-Lawson
    Lawrence Narh-LawsonFeb 15, 2023

    This really helped me today on a project, thanks :-)

  • Dusty Shaw
    Dusty ShawOct 4, 2023

    Thanks, just used this at work :)

  • Sk Selim
    Sk SelimAug 7, 2024

    Thankyou 🙂

Add comment