For example, I'd like to do something like the following:
.myRedPath {
fillcolor: red;
}
...
<v:path class="myRedPath" v="..."/>
to fill my paths with a red color. Is this possible with the color and opacity of the fill and stroke attributes for VML elements? If so, how?
The stroke-opacity attribute is a presentation attribute defining the opacity of the paint server (color, gradient, pattern, etc.) applied to the stroke of a shape. Note: As a presentation attribute stroke-opacity can be used as a CSS property.
The fill property in CSS is for filling in the color of a SVG shape.
To specify fill color or stroke color, you can use color names, RGB or RGBA values, HEX values, HSL or HSLA values. Also, you can take gradients and patterns (see the Text Color section or the SVG Filters and Gradients article).
As mentioned in other answers, you may use DHMTL behaviors to apply any style specified in your style sheet to your VML element as behaviors are supported from IE5 to IE9.
Start by creating a HTC file, eg: vmlcss.htc:
<PUBLIC:COMPONENT>
<PUBLIC:ATTACH EVENT="onpropertychange" ONEVENT="onpropertychange()" />
<PUBLIC:METHOD NAME="refresh" />
<SCRIPT LANGUAGE="JScript">
function onpropertychange()
{
if (event.propertyName == "className")
{
refresh();
}
}
function refresh()
{
// Set any VML attribute you may define in your stylesheet
element.fillcolor = element.currentStyle["fillcolor"];
element.strokecolor = element.currentStyle["strokecolor"];
// etc.
}
refresh();
</SCRIPT>
</PUBLIC:COMPONENT>
Then apply it to your VML elements. For your particular example, you would use:
<style>
v\:path
{
behavior: url(vmlcss.htc);
}
</style>
Finally, specify the styles as shown in your example:
.myRedPath
{
fillcolor: red;
strokecolor: yellow;
}
You may want to modify the behavior file to add support for all VML attributes.
One could use such a technique to write a library that draws shapes using VML or SVG (depending on the browser support) and allows styling through CSS. Support for SVG styles could then be added to the VML objects using such a behavior file by mapping each SVG style to the corresponding VML attributes.
In IE7, you can do following:
vml\:polyline
{
strokecolor: expression(this.strokecolor = "red");
fillcolor: expression(this.fillcolor = "green");
}
But it doesn't work in IE8+ Standards mode, so not really that much useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With