Adjust the colors of an image without using CSS filters

Is there a way to modify the color of an image using CSS?

I typically use the following code to achieve this effect:

filter: hue-rotate(339deg) brightness(1);

However, this method has its limitations. For instance, if the image is predominantly black, the filter doesn't produce the desired result. Additionally, if the image contains multiple colors, it only alters variations of a single color. I am seeking a solution that allows me to adjust all colors in the image.

Answer №1

To add a color filter to an image, follow these steps:

  • Create a layer with a partially transparent background using the rgba() function.

Here is a demonstration:

const layer = document.querySelector('.layer');
input.addEventListener('input', function() {
  const r = parseInt(input.value.substr(1, 2), 16)
  const g = parseInt(input.value.substr(3, 2), 16)
  const b = parseInt(input.value.substr(5, 2), 16)
  layer.style.backgroundColor = `rgb(${r}, ${g}, ${b}, 0.9)`;
})
.container {
  position: relative;
  width:fit-content;
}

.layer {
  position: absolute;
  top: 0;
  left: 0;
  height:100%;
  width:100%;
}

.img {
  height: 100px;
  width: 100px;
  background: url("https://i.imgur.com/0etSSYl.png");
  background-size: cover;
}
<div class="container">
  <div class="layer"></div>
  <div class="img"></div>
</div>

<hr>
<input type="color" id="input">
<-- apply color filter

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to launch a website in Chrome's headless mode while navigating around any blockers of the mode itself

Below is the XPATH I am using to extract price information from various websites, except for Myntra. This XPATH works perfectly on my local Windows system with Selenium, Python3 version, and Chrome driver. Driver path: driver = webdriver.Chrome("/usr ...

Having trouble creating multiple PDFs with mPDF without having to store them on the server

I am creating several PDF files in a loop using the mPDF library. Here is a snippet of my code: for($i=0;$i<=3;$i++) { $mpdf = new mPDF(); $stylesheet = file_get_contents('style.css'); $mpdf->WriteHTML($stylesheet,1); $mpd ...

Utilizing Bootstrap to display selected portions of the list content

I am working with a select element that looks like this: jQuery('.my-select').selectpicker(); .list_img{ width: 30px; height: 30px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script ...

Styling elements with CSS

I've been attempting to align a button to the right of another button. The example above illustrates what I'm trying to achieve. I used Bootstrap to build it, which has a useful navbar feature for layout. However, despite my efforts to use right ...

Utilize jQuery append() to create a grid layout using <td> tags

I need to integrate address information from an object into an HTML table using the append() method. The initial HTML table structure is as follows: <table class="shipping_information" border="1"> </table> To achieve this, I place the addre ...

Aligning Card in Center using Bootstrap

Having trouble centering the card properly, as a beginner I'm not sure what to do. </head> <body> <div class="container"> <div class="row justify-content-center"> <div class="col-md-4 co ...

The main menu items in jQuery do not display the child sub menu upon clicking

I'm currently in the process of creating a unique mobile menu for my Wordpress website, but I'm facing some challenges when it comes to displaying and hiding the sub-menu items. I've assigned the class name .menu-item-has-children as my sele ...

Isolating the controls bar from the video content area within JWPlayer

Currently, I am utilizing JWPlayer for my video playback needs. I am curious if there is a method to separate the controls bar from the video content, similar to the design showcased in this link: https://i.sstatic.net/bZUA9.png Alternatively, are there a ...

Automation of transmitting HTML data to a database

After dabbling in web development for some time, I've come to realize that transferring data between an HTML form and a database can be quite challenging. My usual solution involves using an AJAX call to a PHP script, as illustrated in the image below ...

Tips for positioning label/input box/button on Internet Explorer 7

Here is a Fiddle example you can check out, along with the corresponding code : <div class="menu-alto"> <ul> <li> <a title="Link" href="#">Link</a> </li> <li class="newsletter ...

Tips for exchanging divs in a mobile view using CSS

Illustrated below are three separate images depicting the status of my divs in desktop view, mobile view, and what I am aiming for in mobile view. 1. Current Status of Divs in Desktop View: HTML <div id="wrapper"> <div id="left-nav">rece ...

Modify HTML text using conditional CSS without JavaScript

Apologies for asking such a beginner question, but I've searched high and low for a similar post, to no avail. Here's my query: I have a paragraph text in my HTML code that I want to automatically replace with new text when I click a specific B ...

Inquiry into the use of Jquery.ajax()

Hey there, I'm curious about using Jquery Ajax to retrieve a numeric value from a website. Any suggestions on where I should begin? Any advice would be greatly appreciated. Thanks in advance! Best regards, SWIFT ...

Ways to incorporate margins into text on PDF pages produced by Puppeteer without altering the background color

I am currently using puppeteer to generate PDFs that contain dynamic content. My goal is to include margins/padding above and below the text on consecutive pages. Unfortunately, when I try to add margins with the property margin: { top: "1cm", bottom: "1 ...

Update the CAPTCHA, while also refreshing the entire registration form

JavaScript Snippet <script language="javascript" type="text/javascript"> function updateCaptcha() { $("#captchaimage").attr('src','new_captcha_image.php'); } </script> New Image Code <img src="new_ ...

The specified height for input[type=button] in Firefox/Safari is adjusted by subtracting the padding and border

When implementing the following CSS: input[type=button] { background-color: white; border: 1px solid black; font-size: 15px; height: 20px; padding: 7px; } along with this HTML: <input type="button" value="Foo" /> I anticipate that the t ...

Emphasize a specific line of text within a <div> with a highlighting effect

I'm looking to achieve a similar effect as demonstrated in this fiddle As per StackOverflow guidelines, I understand that when linking to jsfiddle.net, it's required to provide some code. Below is the main function from the mentioned link, but f ...

What causes images to expand automatically when selected in my JavaScript gallery?

I am struggling with a simple JS gallery of images where I want to display a large image at the top and small thumbnails below it. The issue I am facing is that when I hover over an image in the thumbnail section, the big image changes as expected. However ...

In PHP, specify the dimensions of an image

How can I specify the size to display the image as 400X400: height = 400 width = 400 Please provide the code for sizing it as 400X400, thanks. Below is the code snippet: <?php // Retrieve data from the people table $sql = "select * ...

Ensure the background image is repeated without any cropping or adjust it to make it symmetrical

Looking for this: https://i.sstatic.net/zz8lP.png Wanting this (also cropped but symmetrical): https://i.sstatic.net/Wrrlh.png What I currently have: https://i.sstatic.net/S1qtM.png div.row.separator div.col { height: 40px; width: 100%; padding: 0px ...