Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set fill and stroke colors and opacity on VML paths using CSS?

Tags:

css

vml

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?

like image 318
Josh Knauer Avatar asked Dec 28 '11 20:12

Josh Knauer


People also ask

What is stroke opacity?

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.

Which property would you use to change the Colour of an SVG using CSS?

The fill property in CSS is for filling in the color of a SVG shape.

How to fill color inside SVG?

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).


2 Answers

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.

like image 186
Gyum Fox Avatar answered Oct 08 '22 15:10

Gyum Fox


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.

like image 38
user1946745 Avatar answered Oct 08 '22 17:10

user1946745