-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[svg] Support for passing CSS styles to an SVG used via img[src] #8634
Comments
UPDATE: I closed the other duplicate issue in favor of this one, as it feels more in line with the realm of the CSSWG. |
I would love this, I'm tired of tricky |
With CSS Linked Parameters there is already an approach to pass parameters to an SVG or other external resources. It provides three ways to style SVGs via specific parameters. Sebastian |
@SebastianZ Thanks! I hadn't heard of that yet. TL;DR — I think that CSS Linked Parameters draft looks really cool, though I don't think it removes the need/value in a proposal like Some key differences between the two proposals are…
More rambling thoughts…That draft looks rad, and I definitely could have benefitted from it on a handful of projects of my own over the years, though, the real value in this I certainly see icon libraries adopting the features outlined in the CSS Linked Parameters draft, though there are certainly bound to be instances where you want to style/manipulate an SVG in a way that the SVG author did not foresee. In cases like these, something like I think both this proposal and the CSS Linked Parameters spec would be super useful to the community at large, and that they may often be used together. For common, native resources like SVGs housed in your codebase and even some 3rd-party libs, CSS Linked Parameters would be very useful and provide a more uniform and controlled method for styling SVGs dynamically. For other cases where you want to style an SVG but don't want to host it in your codebase and where the author isn't adding CSS Linked Parameters-support, |
@brandonmcconnell I actually also like your approach, just wanted to outline that there is already something targetting the SVG styling issues we currently have. You outlined the differences quite well. And those differences indicate their advantages and disadvantages. From a CSS author's perspective, the big advantage of this proposal is that they are in full control of styling the SVGs. And the approach here is (at least currently) limited to SVGs referenced in Also, this approach is limited to resources referenced via HTML. And because it uses a pseudo-element, it is limited to styling one resource. Though in CSS you can reference different resources like in the following example: .container-with-border {
border-image: src('border.svg') 30;
background: src('background.svg') center / contain no-repeat;
} With Linked Parameters you can style both images individually. With the For what it's worth, the approach might also target Sebastian |
@SebastianZ Thanks! I really appreciate your insight, and that helps me to better understand the reaches of the Linked Parameters draft as well as better understand the differences between both approaches and their advantages/disadvantages. Super exciting stuff 👏🏼 I totally agree with you btw. I'm sure this approach will be more useful than just for SVGs on |
I would call the proposed pseudo-element
|
@Crissov I really like that idea. We can certainly bikeshed on the name a bit more and do something other than I like the |
@Crissov Note that background-image can take a list of images, so Having said that, this would allow to "pierce into" the contents of individual images. Though I am not a big fan of introducing a new pseudo-element for each property that can take an Sebastian |
I think the immediate and most useful case would be for |
Table of Contents
Description
Currently, it is not possible to apply CSS styles to an SVG if it is used via
img[src]
.This proposal adds a new CSS syntax that would enable developers to target the root SVG element in such cases and apply CSS styles to it and its descendants.
Proposal
To achieve this, a pseudo-element can be added to the img src link that acts as the root for the SVG, allowing styles to be targeted for both the SVG and its child elements. The syntax for this can use
::src
as it is tied to thesrc
for the image.Syntax
To target the root SVG element, the syntax can be as follows:
To target child elements within the SVG, the syntax can follow standard CSS selector patterns:
All usual CSS selector patterns, properties, rules, and other conventions would work here as if the SVG had been embedded directly on the page, under the
img
element.Architecture
Any styles applied to
img::src
would not conflict with styles set on theimg
itself. Rather, think of it almost as if thesvg
element is a direct child of theimg
element, ifimg
supported such a thing. This is very similar to how the ShadowDOM and shadow roots already work. In fact, the ShadowDOM may be the ideal implementation/solution for this proposal.Consider this example:
This above code would be treated similarly to a ShadowDOM tree where
img::src
exposes thesvg
element as a shadow-root under the image itself. So in this example, the twoborder
styles would not conflict but rather, theimg
element would receive its assignedborder
styles, and then thesvg
would receive itsborder
styles applied viaimg::src
as if it had been assigned to thesvg
element itself.Examples
SVG source for below examples (expand/collapse)
For completeness, here is example HTML/SVG source that can be used with the below examples:
This looks like this:
…and now for the actual examples:
To demonstrate the proposed syntax, here are two examples targeting the root SVG and a child element within it:
1️⃣ Example 1: Targeting the root SVG
In this example, the
fill
property is applied to the rootsvg
element:2️⃣ Example 2: Targeting a child element within the SVG
In this example,
fill
andstroke
-related properties are applied to therect
element, andfont
-related properties are applied to thetext
element within the SVG:3️⃣ Example 3: Applying a rotation animation to the
rect
elementIn this example, an animation is applied to the
rect
element within theg
group:4️⃣ Example 4: Applying a transition to the
text
element on:hover
In this example, a transition is applied to the
fill
property of thetext
element within theg
group. When thetext
element is hovered, itsfill
property is changed:5️⃣ Example 5: Styling all icons from a specific directory to be a default color and then change colors on `:hover` and `:active` states, respectively
In this more practical example, any
svg
icons pulled from a specific directory (e.g./my-icons/
) will receive a default color and then a different color and transition on hover, only when displayed under the.gallery
section:I'm not sure how feasible this example is, depending on if we can granularly pass user events like a hover state into the image's source. If so, this would be great.
Other thoughts & gotchas
In terms of security, I don't foresee too much if any risk in this feature. That said, I can imagine situations where someone may not want their SVG meddled with and would want a way to prevent style injections like this.
With that in mind, it could be helpful to add support for an attribute to
svg
to enable disallowing style injection, likedisallow-external-styles
:Notably, while we could add an attribute like
allow-external-styles
to instead allow such styling on a per-case basis, I think the better default would be to allow styling unless specifically disallowed.The text was updated successfully, but these errors were encountered: