Is there any way to embed an SVG filter element in an HTML document, so that it can be applied to SVG and HTML elements (via CSS) at some point? The problem with putting it inside an svg element which is itself embedded in the HTML document is that the svg element itself takes up space, which is undesirable.

1 Answers

Alexandr_TT On Best Solutions

You can try it like this:

  • Wrap the svg in thediv
  • For div, assign the css propertyposition: absolute;which will output it from the general stream and will not interfere with the markup
  • hide the div
  • In CSS, apply a filter by itsid to any other HTML `markup object



I have to mention some of the nuances of using the display: none;

Here in this question the problems of applying SVG filters in various browsers are discussed in detail.

@Paul LeBeau in its answer recommends using SVG width = "0"and height = "0" instead of display: none;

Below is the code from his answer.

<video autoplay controls muted src="" style="width: 488px; height: 360px; filter: url(#temperature_filter)">

<svg xmlns="" width="0" height="0">
  <filter id="temperature_filter" color-interpolation-filters="sRGB">
    <feColorMatrix type="matrix" values="1 0 0 0 0 0 0.694 0 0 0 0 0 0.431 0 0 0 0 0 1 0"></feColorMatrix>

As written in the comments @enxaneta is better to use:

svg{position: absolute; width: 0; height: 0;}