What is the correct way to add a hover effect for an SVG that is embedded in an ing tag? Can this purely be achieved by CSS?
If not, what is the correct HTML semantic way to embed SVG icons with hover effects?
Thanks
CodePudding user response:
You must use <svg>
tag instead <img>
.
You can check SVG Vectors for your desired type of SVG
CodePudding user response:
If the svg icon is made using an embedded image in the format data:image/png;base64
then to change color on hover: you can use svg filter feColorMatrix:
.R1:hover {
filter:url(#RedFilter);
}
.G1:hover {
filter:url(#GreenFilter);
}
.B1:hover {
filter:url(#BlueFilter);
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="150" viewBox="0 -30 400 150" style="border:1px solid grey;">
<defs>
<filter id="RedFilter" x="-20" y="-20" width="150" height="150">
<feColorMatrix in="SourceGraphic" type="matrix"
values="0 0 0 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
"/>
</filter>
<filter id="GreenFilter" x="-20" y="-20" width="140" height="140">
<feColorMatrix in="SourceGraphic" type="matrix"
values="0 0 0 0 0
0 0 0 1 0
0 0 0 0 0
0 0 0 1 0
"/>
</filter>
<filter id="BlueFilter" x="-20" y="-20" width="140" height="140">
<feColorMatrix in="SourceGraphic" type="matrix"
values="0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 1 0
"/>
</filter>
<filter id="WhiteFilter" x="-20" y="-20" width="140" height="140">
<feColorMatrix in="SourceGraphic" type="matrix"
values="0 0 0 1 0
0 0 0 1 0
0 0 0 1 0
0 0 0 1 0
"/>
</filter>
<image id="Building" width="100" height="100" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABcCAYAAACYyxCUAAAABGdBTUEAA1teXP8meAAABqhJREFUeAHtXc LHUUQ3qhIREFFRA9GV8QfyAp78JYg8eZlwaN6yf4H6kGQnHKQ4M38AR5yih48CHvxZPaw3jws6EEPgiQS4g/wiYoiMfH7wtbwvXLeTL95Mz09u1XQTFV3dXd1ffOqpntmk2NrZdK7MIvF6C4w95pwiK43sJY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<use id="BuildingGreen" xlink:href="#Building" x="150" y="0" ></use>
<use id="BuildingBlue" xlink:href="#Building" x="280" y="0" ></use>