Here's a thought-provoking question with no definitive answer: We have 3 different svg logos for phone, tablet, and desktop. Each has a hover effect, but there is one special logo that requires a unique hover effect and is wrapped in a link.
I'd like to achieve something similar using CSS and HTML:
<a class="logo" href="index.html">
<picture>
<source media="(min-width:1200px)" srcset="logotype-desktop.svg">
<source media="(min-width:660px)" srcset="logotype-tablet.svg">
<img class="logo__img" src="logotype-mobile.svg">
</picture>
</a>
What do you think about this approach?
My initial idea was to use svgsprite and dynamically load the appropriate resource based on media queries, but I couldn't find any solutions or documentation on how to do this.
The simplest solution would be to have 3 inline svg inserts with display:none
for unused svgs. This may not be elegant, but it could work. However, updating the logo.svg would require re-inserting the code.
If we consider using an object tag, it's unclear how to set media queries for it. It might also require styling inside the svg itself (hover effects didn't work for me).
<svg width="74" height="23" viewBox="0 0 74 23" fill="none" xmlns="http://www.w3.org/2000/svg">
<style>
<![CDATA[
path:hover {
fill: red;
}
]]>
</style>
<path d="M.005 1.549A1.286 1.286 0 011.257.262H8.39c5.057 0 8.385 2.703 8.385 7.13v.06c0 4.846-4.033 7.358-8.802 7.358H2.538v6.801a1.28 1.28 0 01-.754 1.28 1.269 1.269 0 01-1.717-.758 1.28 1.28 0 01-.062-.522V1.549zm8.067 10.968c3.706 0 6.14-1.994 6.14-4.986v-.06c0-3.21-2.394-4.875-5.961-4.875H2.588v9.881l5.484.04zM21.593 1.389a1.282 1.282 0 01.755-1.28 1.268 1.268 0 011.717.758c.056.168.077.346.062.522v20.222a1.28 1.28 0 01-.754 1.28 1.27 1.27 0 01-1.717-.758 1.282 1.282 0 01-.063-.522V1.389zm9.081.03A1.278 1.278 0 0131.926.173h.348a1.535 1.535 0 011.281.737l13.6 17.4V1.36A1.239 1.239 0 0148.389.122c.327 0 .64.13.871.362.231.232.36.546.36.874V21.65a1.14 1.14 0 01-.703 1.092 1.13 1.13 0 01-.449.085h-.129a1.695 1.695 0 01-1.311-.778L33.117 4.24v17.41a1.258 1.258 0 01-1.191 1.247 1.238 1.238 0 01-1.212-1.247V1.42h-.04zm25.264-.03a1.282 1.282 0 01.755-1.28 1.268 1.268 0 011.717.758c.056.168.077.346.062.522v12.295L71.228.55c.262-.271.618-.432.994-.448a1.27 1.27 0 011.222 1.256 1.29 1.29 0 01-.418.898l-7.947 7.897 8.573 10.53c.224.26.348.593.348.937a1.312 1.312 0 01-.838 1.191 1.228 1.228 0 01-1.497-.493L63.3 11.859l-4.828 4.846v4.886a1.27 1.27 0 01-.78 1.17 1.259 1.259 0 01-1.648-.685 1.27 1.27 0 01-.096-.485V1.389h-.01z" fill="#fff"/>
</svg>
Another option is to link external stylesheets, although I encountered issues with this. Simply referencing style.css in the object tag did not apply the styles as expected.
I've tried various methods to find the best solution, but so far nothing seems to be ideal or functional:
<a class="logo" href="index.html">
<picture>
<source media="(min-width:1200px)" srcset="logotype-desktop.svg">
<source media="(min-width:660px)" srcset="logotype-tablet.svg">
<object class="logo__img" data="logotype-mobile.svg" type="image/svg+xml">
</picture>
</a>
If pure CSS and HTML isn't the way to go, perhaps implementing this through JavaScript could be a viable option. If you have any resources or solutions related to this, please share them.