0

Is it possible to use a css custom property within an HTML tag for an inline svg element?

Say I have an inline svg element:

<svg style="width:var(--image-width);" />

Now, this statement isn't valid, but is there a way to use --image-width in the HTML file instead of the css file?

4
  • Yes it appears to work for colors. But if I define --image-width:70; in the css file, the custom property does not work. A color does work though, like: --my-color: rgb(10,80,60,0.5) Commented Sep 28, 2020 at 3:39
  • 1
    You need to specify a unit, try 70px instead of 70. Commented Sep 28, 2020 at 3:45
  • It appears to work for just about any custom property other than width. Commented Sep 28, 2020 at 3:47
  • 1
    If you set the display property of the svg to inline it won't work, you could set it to inline-block to make it work. Commented Sep 28, 2020 at 3:50

1 Answer 1

2

It should work if you import a css file that defines the property --image-width.

/* style.css */
:root {
    --image-width: 42px;
}
<!-- Your html file -->
<link rel="stylesheet" href="style.css">
<svg style="width: var(--image-width);">
    <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.