If you want to keep the details within the emoji you can use filter: url(svg)
where url()
will take an svg filter
svg {
display: none;
}
div {
-webkit-filter: url(#red);
filter: url(#red);
}
<svg>
<filter id="red">
<feColorMatrix type="matrix" values="
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0" />
</filter>
</svg>
<div>ππ§πΌ</div>
While the use of feColorMatrix
may seem daunting at first, it is actually straight forward to define your own colors. Take the following example which represents the color red:
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
If you grab the bold values from above, we can see how they relate to the RGBA color mode:
1 - red (r)
0 - green (g)
0 - blue (b)
1 - alpha (a)
Here each color relates to a value from 0 to 255 divided by 255. Thus, if you had the color green, which has a RGBA of:
R G B A
(0, 255, 0, 1)
Then your values would be:
0/255 -> 0 (r)
255/255 -> 1 (g)
0/255 -> 0 (b)
1 -> 1 (a) - don't divide
And so, using these values in our matrix we get:
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0
You can now add this as an additional filter (or remove the old one) and give it an id
so that it can be referenced within css filter: url(#filterID)
See implemented version below:
svg {
display: none;
}
.red {
-webkit-filter: url(#red);
filter: url(#red);
}
.green {
-webkit-filter: url(#green);
filter: url(#green);
}
<svg>
<filter id="red">
<feColorMatrix type="matrix" values="
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0" />
</filter>
<!-- \/ --- change id to be referenced within css -->
<filter id="green">
<feColorMatrix type="matrix" values="
0 0 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 1 0" />
</filter>
</svg>
<div class="red">ππ§πΌ</div>
<div class="green">ππ§πΌ</div>
<feColorMatrix />
color matrix generator:
For convenience, I've made a feColorMatrix
generator, which allows you to pick or enter a color as well as an opacity and generate a feColorMatrix from that color:
const colorPicker = document.getElementById("color-picker");
const alphaPicker = document.getElementById("alpha-picker");
const codeOutput = document.getElementById("code-output");
const alphaValue = document.getElementById("alpha-value");
const pickedFilter = document.getElementById("picked-filter");
let r=0, g=0, b=0, a=1;
const generateFeColorMatrix = () => `<feColorMatrix type="matrix" values="
${r} 0 0 0 0
0 ${g} 0 0 0
0 0 0 ${b} 0
0 0 0 ${a} 0"
/>
`;
const update = () => {
const feColorMatrixStr = generateFeColorMatrix();
codeOutput.textContent = feColorMatrixStr;
pickedFilter.innerHTML = feColorMatrixStr;
}
colorPicker.addEventListener("input", (e) => {
const hexparts = e.target.value.match(/(\w{1,2})/g);
([r,g,b] = hexparts.map(hex => (parseInt(hex, 16) / 255).toFixed(2)));
update();
});
alphaPicker.addEventListener("input", e => {
a = e.target.value;
alphaValue.textContent = a;
update();
});
alphaValue.textContent = a;
svg {
display: none;
}
#color-picker {
float: right;
}
#code-output {
background-color: #f6f6f6;
}
#emojis {
-webkit-filter: url(#picked-filter);
filter: url(#picked-filter);
font-size: 30px;
}
<input type="color" id="color-picker" />
<input type="range" id="alpha-picker" value="1" min="0" max="1" step="0.01"/><span id="alpha-value">1</span>
<pre id="code-output">
</pre>
<svg>
<filter id="picked-filter"></filter>
</svg>
<p id="emojis">ππ§πΌ</p>
Please note Currently, this solution has very limited browser support. To see a full list of browsers supported by this feature please see here.