【发布时间】:2015-07-17 01:30:58
【问题描述】:
我尝试将 css 样式应用于 svg 元素。我想在没有任何脚本的情况下执行此操作。
虽然样式似乎不适用于<defs>:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="300" height="80">
<style>
rect:hover {
opacity: 0.5;
}
</style>
<defs>
<rect id="box" x="0" y="0" width="30" height="20" rx="3" ry="3" style="fill:green;stroke-width:1;stroke:rgb(0,0,0)"/>
<g id="month">
<!-- first row -->
<g transform="translate(50 40)">
<use xlink:href='#box'/>
<text x="5" y="15" fill="grey">Mon</text>
</g>
<g transform="translate(80 40)">
<use xlink:href='#box'/>
<text x="5" y="15" fill="grey">Thu</text>
</g>
</g>
</defs>
<use xlink:href='#month'/>
</svg>
这行得通:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="300" height="80">
<style>
rect:hover {
opacity: 0.5;
}
</style>
<rect id="box" x="0" y="0" width="30" height="20" rx="3" ry="3" style="fill:green;stroke-width:1;stroke:rgb(0,0,0)"/>
</svg>
有没有办法应用css?是否有其他方法可以将其应用于<defs>-元素?或者有什么解决方法?
【问题讨论】: