SVG小记——SVG Sprites

<!doctype html>
<html lang="en">
  <head>
      <meta charset="utf-8" />
      <title>David Darnes - Web Designer &amp; Front-end Developer</title>
      <style>
      .icon {
            max-width: 40px;
            max-height: 40px;}
      </style>
      <svg display="none" version="1.1" xmlns="http://www.w3.org/2000/svg">
          <symbol id="icon1" fill="#55ACEE">
              <title>icon1</title>
              <desc>icon1</desc>
              <path d="..." id="bird"></path>
          </symbol>
          <symbol id="icon2">
              <title>icon2</title>
              <desc>icon2</desc>
              <path d="..." id="ball" fill="#EA4C89"></path>
              <path d="..." id="ball-lines" fill="#C32361"></path>
          </symbol>
          <symbol id="icon3" fill="#161614">
              <title>icon3</title>
              <desc>icon3</desc>
              <path d="..." id="octocat"></path>
          </symbol>
      </svg>
  </head>
  <body>
      <div class="section">
       <a href="#">
           <svg class="icon" viewBox="0 0 50 41">
               <switch>
                   <use xlink:href="#icon1"></use>
                   <foreignObject>
                       <img class="icon" src="img/icon1.png" alt="Twitter">
                   </foreignObject>
               </switch>
           </svg>
       </a>
       <a href="#">
           <svg class="icon" viewBox="0 0 50 50">
               <switch>
                   <use xlink:href="#icon2"></use>
                   <foreignObject>
                       <img class="icon" src="img/icon2.png" alt="Dribbble">
                   </foreignObject>
               </switch>
           </svg>
       </a>
       <a href="#">
           <svg class="icon" viewBox="0 0 50 50">
               <switch>
                   <use xlink:href="#icon3"></use>
                   <foreignObject>
                       <img class="icon" src="img/icon3.png" alt="GitHub">
                   </foreignObject>
               </switch>
           </svg>
       </a>
    </div>
</body>

使用<symbol>元素不仅仅更加地语义化,至少在我的例子中,我们还可以避免<svg>元素上的viewbox属性的重复出现。
另外我们现在还可以把之前移除的title和desc元素放回来,用他们来提高我们图标的可访问性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值