2015-05-06 48 views
1

我嘗試將CS​​S樣式應用於svg元素。我想要這樣做沒有任何腳本。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>元素?或者任何解決方法?

回答

1

編輯 - 這是對原來的問題的答案,現在已經更新並使響應無效。

當然,只是給use類或ID ..

.box { 
 
    fill: green; 
 
    stroke-width: 1; 
 
    stroke: rgb(0, 0, 0); 
 
} 
 
.box:hover { 
 
    fill: red; 
 
}
<?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"> 
 
    <defs> 
 
    <rect id="box" x="0" y="0" width="30" height="20" rx="3" ry="3" /> 
 
    </defs> 
 
    <use xlink:href='#box' class="box" /> 
 
</svg>

+0

的問題是,我有多個''在'內'' -group ',我想單獨應用懸停效果。 (我編輯的問題,使其更具體) – Stefan

+0

不..你不能這樣做。你不能在所使用的子元素中「使用」內部......他們實際上並不存在於「使用」中......只是像「href」一樣引用。查看你的重複鏈接問題。 –