You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 the src for the image.
Syntax
To target the root SVG element, the syntax can be as follows:
img::src {
/* CSS styles to be applied to the root SVG element */
}
TotargetchildelementswithintheSVG,thesyntaxcanfollowstandardCSSselectorpatterns:
```css
img::src>path {
/* CSS styles to be applied to the selected child element within the SVG */
}
Examples
SVG Source (expand/collapse)
For completeness, here is example HTML/SVG source that can be used with the below examples:
To demonstrate the proposed syntax, here are two examples targeting the root SVG and a child element within it:
Example 1: Targeting the root SVG
In this example, the fill property is applied to the root svg element:
img::src {
fill: red;
border:10px solid blue;
}
Example 2: Targeting a child element within the SVG
In this example, fill and stroke-related properties are applied to the rect element, and font-related properties are applied to the text element within the SVG:
Example 3: Applying a rotation animation to the rect element
In this example, an animation is applied to the rect element within the g#myGroup group:
img::src#myGrouprect {
animation: rotate 3s linear infinite;
}
@keyframes rotate {
from { transform:rotate(0deg); }
to { transform:rotate(360deg); }
}
Example 4: Applying a transition to the text element on :hover
In this example, a transition is applied to the fill property of the text element within the g#myGroup group. When the text element is hovered, its fill property is changed:
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, like disallow-external-styles:
<svgdisallow-external-styles>...</svg>
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:
Also posted this same proposal to w3c/csswg-drafts#8634. It's a clear duplicate, so whichever place is better suited for this proposal, let's keep it there and close the other.
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:
Examples
SVG Source (expand/collapse)
For completeness, here is example HTML/SVG source that can be used with the below examples:
This looks like this:
To demonstrate the proposed syntax, here are two examples targeting the root SVG and a child element within it:
Example 1: Targeting the root SVG
In this example, the
fill
property is applied to the rootsvg
element: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:Example 3: Applying a rotation animation to the
rect
elementIn this example, an animation is applied to the
rect
element within theg#myGroup
group: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#myGroup
group. When thetext
element is hovered, itsfill
property is changed: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: