Style SVG circle with CSS
As per the SVG 1.1 specification you can't style the r
attribute of an SVG circle using CSS https://www.w3.org/TR/SVG/styling.html#SVGStylingProperties. But you can do:
<circle cx="168" cy="179" r="59"
fill="white" stroke="black"
onmouseover="evt.target.setAttribute('r', '72');"
onmouseout="evt.target.setAttribute('r', '59');"/>
In SVG 2, which is partially supported by some modern browsers, you can style the r
attribute of circles using CSS. https://www.w3.org/TR/SVG2/styling.html#PresentationAttributes
Resizing SVG Circle Radius Using CSS Animation
In SVG 1.1 the radius of a circle was an attribute and not a CSS property. SVG 2 changes this and instead makes the circle's radius a CSS property that's mapped to an attribute.
CSS animations animate CSS properties and do not animate attributes.
Firefox has now implemented this part of the SVG 2 specification so the testcase in the question will work now although it didn't when the question was written.
SMIL animations will work on attributes (and CSS properties).
<html>
<head>
<link href = "styling.css" rel = "stylesheet" type = "text/css"></link>
</head>
<body>
<svg class = "button" expanded = "true" height = "100px" width = "100px">
<circle cx = "50%" cy = "50%" r = "35%" stroke = "#000000" stroke-width = "10%" fill = "none"/>
<circle class = "innerCircle" cx = "50%" cy = "50%" r = "25%" fill = "#000000">
<animate attributeName="r" begin="0s" dur="1s" repeatCount="indefinite" from="5%" to="25%"/>
</circle>
</svg>
</body>
</html>
how do i set fill color of a svg circle with the help of thymeleaf
Thanks to @enxaneta. solved it using this
<svg id="svg" height="50" width="50">
<circle cx="50%" cy="50%" r="50%" stroke="" stroke-width="3"
th:style="'fill:'+@{${results.borderColor}}+';'" />
<text x="50%" y="50%" text-anchor="middle" fill="white" font-family="Arial, Helvetica, sans-serif" font-size="25px" alignment-baseline="central" th:text="${results.shortName}">OF</text>
</svg>
Styling of SVG circle doesn´t work in Firefox, browser removes radius property
Seems like you found an inconsistent implementation of the SVG 2.0 spec in Firefox. Or, maybe Firefox does not fully support SVG 2.0 yet. The specification states that:
Some styling properties can be specified not only in style sheets and ‘style’ attributes, but also in presentation attributes.
So, both style sheets and attributes should work.
The quick fix is to add r
, cx
and cy
presentation attributes to your circle
element (as suggested here):
<circle _ngcontent-c1="" class="progress" style="stroke-dasharray: 131.947; stroke-dashoffset: 32.987;" cx="50%" cy="50%" r="21" fill="transparent" ng-reflect-ng-style="[object Object]"></circle>
SVG - circle rotating around circle
You could also use a svg SMIL
animation like <animateTransform>
<svg width="300" height="300" style="border: 1px solid black">
<circle stroke="black" stroke-width="1" r="100" cx="150" cy="150" fill="white" />
<circle id="dot" r="10" cx="50" cy="150" />
<animateTransform href="#dot" attributeName="transform" type="rotate" from="0 150 150" to="360 150 150" dur="3s" repeatCount="indefinite" />
</svg>
SVG radius or position with CSS variables
According to the MDN Docs "Starting with SVG2, r is a Geometry Property meaning this attribute can also be used as a CSS property for circles."
There are three ways to set the radius value
- as attribute
<circle ... r=10>
- via class and stylesheet
circle {
r: 10px;
}
- inline in 'style' attribute
<circle... style="r: 10px;" ></circle>
The last way has the greates presedence. Take a look at this example where all circle elements have r set as attribute, which is overridden by the stylesheet (2nd circle), which is overridden again by the inline style attribute (3rd circle)
(These three ways don't have to be used together, but are only combined to demontrate which one has a higher presedence and overwrites the already set values)
:root {
--dark-text-clr: purple;
--radius: 20px;
}
.circle {
r: 10px;
}
<svg xmlns="http://www.w3.org/2000/svg" width="300px" height="300px" viewBox="0 0 300 300">
<circle cx="10" cy="10" fill="var(--dark-text-clr)" mask="url(#moon-mask)" r=5></circle>
<circle cx="30" cy="30" fill="var(--dark-text-clr)" mask="url(#moon-mask)" r=5 class="circle"></circle>
<circle cx="60" cy="60" fill="var(--dark-text-clr)" mask="url(#moon-mask)" r=5 class="circle" style="r: var(--radius);" ></circle>
</svg>
How to style SVG with external CSS?
Your main.css file would only have an effect on the content of the SVG if the SVG file is included inline in the HTML:
https://developer.mozilla.org/en/docs/SVG_In_HTML_Introduction
<html>
<body>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 56.69 56.69">
<g>
<path d="M28.44......."/>
</g>
</svg>
</html>
If you want to keep your SVG in files, the CSS needs to be defined inside of the SVG file.
You can do it with a style tag:
http://www.w3.org/TR/SVG/styling.html#StyleElementExample
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
width="50px" height="50px" viewBox="0 0 50 50">
<defs>
<style type="text/css"><![CDATA[
.socIcon g {
fill:red;
}
]]></style>
</defs>
<g>
<path d="M28.44......./>
</g>
</svg>
You could use a tool on the server side to update the style tag depending on the active style. In ruby you could achieve this with Nokogiri. SVG is just XML. So there are probably many XML libraries available that can probably achieve this.
If you're not able to do that, you will have to just have to use them as though they were PNGs; creating a set for each style, and saving their styles inline.
SVG fade color of object
You will want to use the radial gradient to have this work. You can play around with the offset numbers for the desired effect.
<svg height="100" width="100">
<defs>
<radialGradient id="myGradient">
<stop offset="10%" stop-color="red" />
<stop offset="95%" stop-color="black" />
</radialGradient>
</defs>
<!-- using my radial gradient -->
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="url('#myGradient')"/>
</svg>
Related Topics
How to Add Compass Syntax Support to Jetbrains PHPstorm
Negative Top Margin Not Working in Ie 8 or 9
How to Define a Less Mixin to Generate a Transition-Property with a Variable Number of Parameters
Facebook Like Widget on Fan Page, Comment Area Out of Visible Area
How to Make a Fluid Sticky Footer
Bootstrap 3: Can the Glyphicons Be Stacked Like Font Awesome's Icons
Iframe and Conflicting Absolute Positions
Textarea::Selection and ::-Moz-Selection
Can You Use If/Else Conditions in Css
All About Choosing the Right Font for a Website
Pure CSS Solution - Square Elements
How to Use Google Web Fonts in an HTML Email
Gradient Effect for Triangle Shape Borders
Why Do 'Foo Bar' and 'Foo > Bar' Have the Same Specificity in CSS
How to Change the Button Color When It Is Active Using Bootstrap