使用CSS来设置SVG中Use元素的样式

转自:http://blog.csdn.net/xiaozhu2hao/article/details/47443031

参考:

用CSS给SVG <use>的内容添加样式

http://www.w3cplus.com/svg/styling-svg-use-content-css.html



今天看到技术群中有人问svg中use元素可以设置样式属性不?因为他的symbol有200多种,但是只是颜色不一样。如果挨个定义,那svg文件就会很长。自己也思考过这个问题后来看到http://tympanus.net/codrops/2015/07/16/styling-svg-use-content-css/这篇文章,收益匪浅。这篇文章讲的比较详细。就不在解释原理了。直接上自己测试例子的代码,比较直观。

      1 单纯的svg文件,将style直接写在<svg>标签元素下

        

  1. <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"  xmlns:xlink="http://www.w3.org/1999/xlink" width="2000" height="2000">  
  2.    <style type="text/css">  
  3.      svg path {  
  4.           fill: inherit;  
  5.      }   
  6.      .codrops-1 {  
  7.         fill: #4BC0A5;  
  8.         color: #A4DFD1;  
  9.       }  
  10.     .codrops-2 {  
  11.       fill: #0099CC;  
  12.        color: #7FCBE5;  
  13.     }  
  14.     .codrops-3 {  
  15.        fill: #5F5EC0;  
  16.        color: #AEAFDD;  
  17.     }  
  18.   </style>  
  19.   <def>  
  20.     <symbol id="codrops" viewBox="0 0 23 30">  
  21.         <path class="back" fill="#aaa" d="M22.63,18.261c-0.398-3.044-2.608-6.61-4.072-9.359c-1.74-3.271-3.492-5.994-5.089-8.62l0,0   c-1.599,2.623-3.75,6.117-5.487,9.385c0.391,0.718,0.495,1.011,0.889,1.816c0.143,0.294,0.535,1.111,0.696,1.43   c0.062-0.114,0.582-1.052,0.643-1.162c0.278-0.506,0.54-0.981,0.791-1.451c0.823-1.547,1.649-2.971,2.469-4.33   c0.817,1.359,1.646,2.783,2.468,4.33c0.249,0.47,0.513,0.946,0.791,1.453c1.203,2.187,2.698,4.906,2.96,6.895   c0.292,2.237-0.259,4.312-1.556,5.839c-1.171,1.376-2.824,2.179-4.663,2.263c-1.841-0.084-3.493-0.887-4.665-2.263   c-0.16-0.192-0.311-0.391-0.448-0.599c-0.543,0.221-1.127,0.346-1.735,0.365c-0.56-0.019-1.095-0.127-1.599-0.313   c1.448,3.406,4.667,5.66,8.447,5.78C19.086,29.537,23.469,24.645,22.63,18.261z"/>  
  22.         <path class="front" fill="#ddd" d="M6.177,11.659c0.212,0.367,0.424,0.747,0.635,1.136c0.164,0.303,0.333,0.606,0.512,0.927   c0.683,1.225,1.618,2.898,1.755,3.937c0.144,1.073-0.111,2.056-0.716,2.769c-0.543,0.641-1.315,1.014-2.186,1.067   c-0.87-0.054-1.643-0.43-2.186-1.067c-0.604-0.713-0.858-1.695-0.715-2.771c0.137-1.036,1.072-2.712,1.755-3.936   c0.18-0.32,0.349-0.623,0.513-0.927C5.752,12.404,5.964,12.026,6.177,11.659 M6.177,5.966L6.177,5.966   c-1.02,1.649-2.138,3.363-3.247,5.419c-0.932,1.728-2.344,3.967-2.598,5.88c-0.535,4.014,2.261,7.09,5.846,7.203   c3.583-0.113,6.379-3.189,5.845-7.203c-0.255-1.912-1.666-4.152-2.598-5.88C8.314,9.329,7.196,7.617,6.177,5.966L6.177,5.966z"/>  
  23.     </symbol>  
  24.   </def>  
  25.   <use x="0" y="0" width="100" height="100" xlink:href="#codrops" class="codrops-1"/>  
  26.   <use x="100" y="0" width="100" height="100"   xlink:href="#codrops" class="codrops-2"/>  
  27.   <use x="200" y="0" width="100" height="100"  xlink:href="#codrops" class="codrops-3"/>  
  28. </svg>  
     2  如果用在Html中如下

    

  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.        <style type="text/css">  
  5.         svg path {  
  6.            fill: inherit;  
  7.         }   
  8.         use.codrops-1 {  
  9.           fill: #4BC0A5;  
  10.           color: #A4DFD1;  
  11.         }  
  12.         use.codrops-2 {  
  13.          fill: #0099CC;  
  14.          color: #7FCBE5;  
  15.         }  
  16.         use.codrops-3 {  
  17.           fill: #5F5EC0;  
  18.           color: #AEAFDD;  
  19.         }  
  20.       </style>  
  21.    </head>  
  22.       
  23.    <body>  
  24.        <div style="width:1000px;height:1000px;border:2px solid #F00;overflow-y:scroll;">  
  25.         <svg style="display: none;">  
  26.           <symbol id="codrops" viewBox="0 0 23 30">  
  27.              <path class="back" fill="#aaa" d="M22.63,18.261c-0.398-3.044-2.608-6.61-4.072-9.359c-1.74-3.271-3.492-5.994-5.089-8.62l0,0   c-1.599,2.623-3.75,6.117-5.487,9.385c0.391,0.718,0.495,1.011,0.889,1.816c0.143,0.294,0.535,1.111,0.696,1.43   c0.062-0.114,0.582-1.052,0.643-1.162c0.278-0.506,0.54-0.981,0.791-1.451c0.823-1.547,1.649-2.971,2.469-4.33   c0.817,1.359,1.646,2.783,2.468,4.33c0.249,0.47,0.513,0.946,0.791,1.453c1.203,2.187,2.698,4.906,2.96,6.895   c0.292,2.237-0.259,4.312-1.556,5.839c-1.171,1.376-2.824,2.179-4.663,2.263c-1.841-0.084-3.493-0.887-4.665-2.263   c-0.16-0.192-0.311-0.391-0.448-0.599c-0.543,0.221-1.127,0.346-1.735,0.365c-0.56-0.019-1.095-0.127-1.599-0.313   c1.448,3.406,4.667,5.66,8.447,5.78C19.086,29.537,23.469,24.645,22.63,18.261z"/>  
  28.              <path class="front" fill="#ddd" d="M6.177,11.659c0.212,0.367,0.424,0.747,0.635,1.136c0.164,0.303,0.333,0.606,0.512,0.927   c0.683,1.225,1.618,2.898,1.755,3.937c0.144,1.073-0.111,2.056-0.716,2.769c-0.543,0.641-1.315,1.014-2.186,1.067   c-0.87-0.054-1.643-0.43-2.186-1.067c-0.604-0.713-0.858-1.695-0.715-2.771c0.137-1.036,1.072-2.712,1.755-3.936   c0.18-0.32,0.349-0.623,0.513-0.927C5.752,12.404,5.964,12.026,6.177,11.659 M6.177,5.966L6.177,5.966   c-1.02,1.649-2.138,3.363-3.247,5.419c-0.932,1.728-2.344,3.967-2.598,5.88c-0.535,4.014,2.261,7.09,5.846,7.203   c3.583-0.113,6.379-3.189,5.845-7.203c-0.255-1.912-1.666-4.152-2.598-5.88C8.314,9.329,7.196,7.617,6.177,5.966L6.177,5.966z"/>  
  29.           </symbol>  
  30.         </svg>  
  31.         <svg height="90px" width="69px">  
  32.             <use xlink:href="#codrops" class="codrops-1"/>  
  33.         </svg>  
  34.         <svg height="90px" width="69px">  
  35.             <use xlink:href="#codrops" class="codrops-2"/>  
  36.         </svg>  
  37.         <svg height="90px" width="69px">  
  38.             <use xlink:href="#codrops" class="codrops-3"/>  
  39.         </svg>  
  40.         </div>  
  41.     </body>  
  42. </html>  

效果如下图


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Vue 3使用SVG图标,可以采取以下几种方法: 1. 使用Vue插件:可以使用一些常见的Vue插件,如vue-svg-icon和vue-fontawesome,通过npm安装插件,然后在Vue项目引入插件并使用SVG图标组件。具体的使用方法可以参考插件文档。例如,可以使用以下代码安装并使用vue-svg-icon插件: ```javascript npm install vue-svg-icon ``` ```javascript import { createApp } from 'vue'; import App from './App.vue'; import VueSvgIcon from 'vue-svg-icon'; const app = createApp(App); app.use(VueSvgIcon); app.mount('#app'); ``` 然后,在Vue模板使用SVG图标组件,并指定图标名称: ```html <template> <div> <svg-icon name="icon-name" /> </div> </template> ``` 2. 使用CSS背景图:可以直接在项目使用SVG图标的代码,并通过CSS控制图标的大小和样式。例如: ```html <svg style="width: 10px; height: 10px"> <use href="#icon-shanchu"></use> </svg> ``` 3. 直接在Vue模板插入SVG图标代码:可以将SVG图标代码直接复制到Vue模板,并根据需要添加样式和属性。例如: ```html <template> <div> <svg class="icon" viewBox="0 0 24 24"> <path d="M12 2L1 21h22L12 2zm0 4.88L18.12 19H5.88L12 6.88z" /> </svg> </div> </template> <style> .icon { width: 24px; height: 24px; fill: currentColor; } </style> ``` 4. 封装为Vue组件:可以将SVG图标封装为Vue组件,以便在应用程序多次使用。创建一个新的Vue组件,并在模板使用`<svg>`标签来插入SVG图标代码。例如,创建一个名为`Icon`的Vue组件: ```html <template> <div> <Icon /> </div> </template> <script> import Icon from './Icon.vue'; export default { components: { Icon } }; </script> <!-- Icon.vue --> <template> <svg class="icon" viewBox="0 0 24 24"> <path d="M12 2L1 21h22L12 2zm0 4.88L18.12 19H5.88L12 6.88z" /> </svg> </template> <style> .icon { width: 24px; height: 24px; fill: currentColor; } </style> ``` 以上就是在Vue 3使用SVG图标的几种方法。具体选择哪种方法取决于你的项目需求和个人偏好。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值