CSS - 在悬停时更改填充颜色 - SVG路径

时间:2013-10-03 10:44:07

标签: html css html5 css3 svg

使用svg路径在我的项目中使用现代ui图标包。

我尝试做的是在悬停时更改填充颜色。

但我没有成功..

希望有人帮我解决这个问题。

提前致谢。

代码:

<div id="Main">
   <ul>
      <li>
          <form>
             <button>
                <div class="inner">
                   <svg>
                     <path d="M35.......etc..">
                     </path>
                   </svg>
                </div>
             </button>
          </form>
      </li>
   </ul>
</div

3 个答案:

答案 0 :(得分:32)

您可以按以下方式覆盖svg css属性。

#Main svg:hover {
    fill: #fce57e;
}

答案 1 :(得分:3)

很抱歉回答了一篇非常老的帖子。

我认为最好的方法是查看<svg>内包含的标记。

例如:

<svg>
    <path>...</path>
</svg>

正如您所看到的,<path>标记由<svg>包装。然后你可以在css中添加<path>

svg:hover path { 
    fill: #fce57e;
}

以下是工作片段:

&#13;
&#13;
svg:hover path {
  fill: black;
  transition: all ease 0.3s;
}

svg path {
  transition: all ease 0.3s;
}
&#13;
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33.19 50.36" width="15%" class="go-location-all" id="go-location-icon"><defs><style>.cls-1{fill:#DF1E34;}</style></defs><title>find-location</title><g id="Layer_2" data-name="Layer 2"><g id="artwork"><path class="cls-1" d="M20.75,22.56a1.09,1.09,0,1,0-1-1.93,6.71,6.71,0,1,1,3.56-5.92,1.1,1.1,0,0,0,2.19,0,8.88,8.88,0,1,0-4.73,7.85Z"/><path class="cls-1" d="M15.88,44.28a1.07,1.07,0,0,0,.71.27,1.09,1.09,0,0,0,.67-.23,37.31,37.31,0,0,0,7-7.8,38.45,38.45,0,0,0,7-21.82,14.71,14.71,0,1,0-29.41,0c0,9,3.8,16.58,7,21.33A44.52,44.52,0,0,0,15.88,44.28Zm.71-42.09A12.53,12.53,0,0,1,29.11,14.71a36.16,36.16,0,0,1-6.57,20.5A38.39,38.39,0,0,1,16.63,42a46.29,46.29,0,0,1-6-7.22c-3-4.49-6.58-11.62-6.58-20.06A12.52,12.52,0,0,1,16.59,2.19Z"/><path class="cls-1" d="M24.51,40.69a1.1,1.1,0,0,0-.32,2.17C29.59,43.67,31,45,31,45.25s-.79,1-3.94,1.83a45.38,45.38,0,0,1-10.47,1.09A45.29,45.29,0,0,1,6.13,47.08C3,46.3,2.22,45.44,2.19,45.25S3.59,43.67,9,42.86a1.09,1.09,0,0,0-.32-2.16C4.7,41.29,0,42.57,0,45.25c0,1.66,1.8,3,5.36,3.9a46.84,46.84,0,0,0,11.23,1.21,47,47,0,0,0,11.24-1.21c3.55-.92,5.36-2.24,5.36-3.9C33.19,42.56,28.47,41.29,24.51,40.69Z"/></g></g></svg>
&#13;
&#13;
&#13;

答案 2 :(得分:3)

在某些情况下,svg使用stroke而不是fill,所以使用此

svg:hover path { 
    stroke: #fce57e;
}

或不涉及任何路径:

svg:hover { 
    stroke: #fce57e;
}