Загрузка...

How to Darken an Image in CSS

Learn how to effectively darken an image using CSS techniques, including background blending and overlays. Get step-by-step instructions with code snippets!
---
This video is based on the question https://stackoverflow.com/q/72208525/ asked by the user 'Gigi' ( https://stackoverflow.com/u/19031964/ ) and on the answer https://stackoverflow.com/a/72209173/ provided by the user 'Tamam45' ( https://stackoverflow.com/u/16520568/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: why may image is not darken? HTML CSS

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
How to Darken an Image in CSS: A Step-by-Step Guide

Have you ever found yourself trying to add a dark overlay over an image for your website, only to wonder why it’s not working? You’re not alone. Many web developers face challenges when trying to achieve the desired visual effect with images using CSS. In this post, we will explore a common scenario where a user wanted to darken an image but encountered some issues and provide a clear solution.

The Problem

You want to darken an image by applying a black overlay with an opacity of 0.7 on top of your image using the :after pseudo-element in CSS. However, despite your attempts, the overlay doesn’t seem to work as intended.

Here’s a snippet of the CSS and HTML you might typically use:

Original CSS Code

[[See Video to Reveal this Text or Code Snippet]]

Original HTML Code

[[See Video to Reveal this Text or Code Snippet]]

In this case, you may find that the darkening effect isn’t applying successfully. Let’s break down the solution to this problem.

The Solution

To effectively darken the image, we can utilize a different approach by setting the background image directly in CSS along with the overlay. Here’s how:

Updated CSS Code

[[See Video to Reveal this Text or Code Snippet]]

Updated HTML Code

[[See Video to Reveal this Text or Code Snippet]]

Explanation of the Solution

Background Property:

In the new CSS, we set the background property directly on the .home class. This includes both the overlay color (with an RGBA background) and the URL of the image.

Background Size:

The background-size: cover; ensures that your image fills the entire background of the section, maintaining its aspect ratio.

Blend Mode:

The background-blend-mode: darken; property ensures that the black overlay effectively darkens whatever image is applied as the background.

Conclusion

By adjusting the way we apply the darkening effect over the image, you can achieve the desired visual impact effectively using CSS. This method not only simplifies the code but also enhances performance since the background image is loaded more efficiently.

Now you can confidently add dark overlays to your images in your web projects!

Feel free to share this article with those who might benefit from it, and remember to experiment with different colors and opacities to find the perfect aesthetic for your website.

Видео How to Darken an Image in CSS канала vlogize
Страницу в закладки Мои закладки
Все заметки Новая заметка Страницу в заметки