Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

opacity(): a filter for rendered transparency

Scale the alpha of the rendered image without removing the element from layout or interaction.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

filter: opacity(50%);

opacity() is a filter function applied through filter. At 0 or 0% its rendered image becomes transparent; at 1 or 100% it is unchanged. An omitted argument means 100%, values above that are clamped and negative arguments are invalid. This function is distinct from the opacity property, although both can influence the final alpha and can compound when used together.

Transparency is not removal. A transparent element can still occupy space, receive pointer interactions and participate in keyboard navigation. If a control should be unavailable, address its interaction and accessibility state explicitly instead of relying on filter: opacity(0). When filters are composed, opacity() is one stage of that filter chain, so its position can matter with effects that create or combine pixels.

The example paints red over an explicit white background. Half opacity produces RGB 255,128,128, zero produces white and a value above 100% gives the unchanged red. It also creates a transparent button: hit testing still finds it and its measured width remains 40 pixels. The test separates visual alpha from layout and interaction behavior.

Examples

const sample=async style=>{const svg='<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8" fill="white"/><foreignObject width="8" height="8"><div xmlns="http://www.w3.org/1999/xhtml" style="width:8px;height:8px;'+style.replaceAll('&','&amp;').replaceAll('"','&quot;')+'"></div></foreignObject></svg>';const image=new Image();image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg);await image.decode();const canvas=document.createElement('canvas');canvas.width=canvas.height=8;const context=canvas.getContext('2d');context.drawImage(image,0,0);return [...context.getImageData(4,4,1,1).data].slice(0,3).join(',')};
const half=await sample('background:red;filter:opacity(50%)');const zero=await sample('background:red;filter:opacity(0)');const over=await sample('background:red;filter:opacity(200%)');
const element=document.createElement('button');element.style.cssText='position:fixed;left:0;top:0;width:40px;height:30px;filter:opacity(0)';document.body.append(element);
console.log('half-over-white='+half+'; zero-over-white='+zero+'; over='+over+'; hit='+String(document.elementFromPoint(20,15)===element)+'; width='+element.offsetWidth+'; negative='+CSS.supports('filter','opacity(-1)'));

Result

half-over-white=255,128,128; zero-over-white=255,255,255; over=255,0,0; hit=true; width=40; negative=false

The transparent button remains a hit target. A hidden-looking control is not automatically disabled or removed from accessibility.

Sources