Comments (7)
As of v2.1.0 you can also now nest <use>
elements arbitrarily, so you can style imported symbols with a container, e.g.:
<svg>
<g fill="red"><use xlink:href="sprite.svg#symbol"/></g>
<g fill="blue"><use xlink:href="sprite.svg#symbol"/></g>
</svg>
FWIW, I've got another update in the works that replaces the <use>
element with a <g>
element that has the former's attributes, so in the future you'll be able to simply do this:
<svg>
<use fill="red" xlink:href="sprite.svg#symbol"/>
<use fill="blue" xlink:href="sprite.svg#symbol"/>
</svg>
from svg4everybody.
icon-arrow-link != icon-arrow-menu maybe?
from svg4everybody.
sorry, copied the wrong part here.
from svg4everybody.
Pretty sure this will only work if the IE fallback is in use. If the <use xlink:href="iconset.svg#iconname"></use>
syntax can be used, then the elements within the SVG cannot be targeted by your CSS, i.e. you will only be able to style the entire SVG element from the page's CSS.
from svg4everybody.
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="icon-arrow-link" viewBox="0 0 10 5">
<polygon fill="currentColor" points="10,5 0,5 5,0 "/>
<polygon fill="blue" points="10,5 0,5 5,0 "/>
</symbol>
</svg>
would work on:
<svg role="img"> <use xlink:href="path/Sprite.svg#icon-arrow-link"></use> </svg>
if you set:
svg { color: red }
from svg4everybody.
Chrome is correct here (Safari too). This is not supposed to work.
It works in IE because the script changes the DOM of your icon instances to:
<svg …>
<!-- content of your symbol -->
<polygon class="red" … />
<polygon class="blue" … />
</svg>
It works in Firefox because behind the scenes Firefox generates a shadow dom that looks like this:
<svg …>
<use>
<svg>
<!-- content of your symbol -->
<polygon class="red" … />
<polygon class="blue" … />
</svg>
<use>
</svg>
AND Firefox has a bug where ordinary selectors can pierce through the shadow dom (at least in this case). It's likely that this bug will be fixed in the future.
Bottom line:
- If you're using an external SVG sprite, don't expect to be able to style specific elements from your symbols. You can, however, style the parent
<svg>
element (the one referencing the symbol) with SVG properties such asfill
andstroke
, and these will be inherited by the symbol's child and descendant elements (unless they're hardcoded to different values). You also do not need to addfill="currentColor"
to every path and polygon, since you can just set it for allsvg
elements. (I usesvg.icon { fill: currentColor; }
in my main stylesheet.) Finally, you can try to be creative with what values you hardcode in the symbol and what values you let it inherit from the "instance", which allows things like optional strokes or two-color fills, but it's still rather complex and limited. If you need a few multicolor icons and illustrations, consider putting them in an inline sprite in addition to your external sprite (for monochrome icons). - If you're using an inline sprite (e.g. placed after
<body>
; at the end of the document would be an option if Safari supported it, but it doesn't), you will be able to target individual paths and polygons (and other shapes or groups) using regular CSS selectors, but note that it will impact all instances of the icon on the page.
from svg4everybody.
To add to @fvsch comments - this is working as intended - see: http://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/
from svg4everybody.
Related Issues (20)
- Not working for MS Edge HOT 3
- can't make href work using svg4everybody HOT 1
- svg4everybody is not defined
- Add workaround for SVG print bug in Firefox
- svg-chrom HOT 1
- SVG transform animateTransform not working in IE11 and Edge 42
- linearGradient for svg4everybody HOT 2
- run the polyfill only on selected dom element and its children
- Validate Not Running After Page Start
- Inlining External SVG
- Inlining External SVG from sprite, SVG doesn't display correctly HOT 3
- PreserveAspectRatio attribute is stripped out on IE11
- Add option to always replace contents ignoring browser support HOT 2
- Empty XHR embeds after splice(0)
- import does not seem to work properly
- Generate png from svg
- Svg4everybody not working on all svgs in IE11 HOT 2
- Suggestion: Suggest loading the script with the nomodule attribute HOT 2
- Issue with iPhone5 / Safari6
- Link to kartikprabhu.com "Inline SVG Icons" is broken; Archive.org link provided
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svg4everybody.