【问题标题】:TailwindCSS: SVG icon won't resize properlyTailwindCSS:SVG 图标无法正确调整大小
【发布时间】:2019-05-06 07:42:40
【问题描述】:

我一直在尝试调整此示例中的图标大小。

<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/0.7.2/tailwind.min.css" rel="stylesheet"/>
<div class="bg-indigo-dark">
  <div class="flex text-grey-lighter">
    <svg class="flex-no-shrink fill-current" fill="none" xmlns="http://www.w3.org/2000/svg"
         viewBox="0 0 100 100" width="100px" height="100px">
      <path d="M5 5a5 5 0 0 1 10 0v2A5 5 0 0 1 5 7V5zM0 16.68A19.9 19.9 0 0 1 10 14c3.64 0 7.06.97 10 2.68V20H0v-3.32z"/>
    </svg>
    <div class="leading-normal">
      Some demo text
    </div>
  </div>
</div>

但是,无论我尝试什么,我都无法调整 svg 的大小。我做错了什么?

【问题讨论】:

  • svg 元素上有 width 和 height 属性。你可以改变那些...

标签: html css svg tailwind-css


【解决方案1】:

在 TailwindCSS 中更改 SVG 的宽度和高度

Adam Wathan (TailwindCSS creator) solution for working with SVGs is presented in a video here.他的解决方法如下:

更改 SVG 元素的步骤

  1. 移除宽度和高度属性
  2. 添加 TailwindCSS 宽度和高度类

有问题的 SVG 示例

我在问题中使用了 SVG,并将其与 SVG 配对并缩写了路径。然后进行上述更改。

<svg class="flex-no-shrink fill-current"
     fill="none" 
     xmlns="http://www.w3.org/2000/svg"
     viewBox="0 0 100 100"
     width="100px" height="100px">  <!-- 1. Remove width and height attributes -->
  <path d="M5 5a5 5 0 0 1 10 0v2A5..."/>
</svg>
   <!--      \/---- 2. Add tailwindcss width and height classes -->
<svg class="h-4 w-4 flex-no-shrink fill-current"
     fill="none" 
     xmlns="http://www.w3.org/2000/svg"
     viewBox="0 0 100 100">
  <path d="M5 5a5 5 0 0 1 10 0v2A5..."/>
</svg>

他没有提到视图框,只是说如果你保持正方形会更容易。

【讨论】:

    【解决方案2】:

    您需要先修复视图框——它太大了。这就是为您提供额外填充的原因。然后根据需要设置宽度和高度。请参阅下面的 sn-p:

    function changeHandler(event){
      var svg = document.getElementById('svg');
      svg.setAttribute("width", event.target.value + 'px');
      svg.setAttribute("height", event.target.value + 'px');
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/0.7.2/tailwind.min.css" rel="stylesheet" />
    <input type="number" value="20" onchange="changeHandler(event)" />
    <div class="bg-indigo-dark">
      <div class="flex text-grey-lighter">
        <svg id="svg" class="flex-no-shrink fill-current" fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="20px" height="20px">
          <path d="M5 5a5 5 0 0 1 10 0v2A5 5 0 0 1 5 7V5zM0 16.68A19.9 19.9 0 0 1 10 14c3.64 0 7.06.97 10 2.68V20H0v-3.32z"/>
        </svg>
        <div class="leading-normal">
          Some demo text
        </div>
      </div>
    
    </div>

    【讨论】:

      【解决方案3】:

      要调整图标大小,请调整viewBox的大小

      <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/0.7.2/tailwind.min.css" rel="stylesheet"/>
      
      <div class="bg-indigo-dark">
        <div class="flex text-grey-lighter">
      
          <svg 
            class="flex-no-shrink fill-current" 
            fill="none" 
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 80 80"  
            width="100px" 
            height="100px"
          >
            <path d="M5 5a5 5 0 0 1 10 0v2A5 5 0 0 1 5 7V5zM0 16.68A19.9 19.9 0 0 1 10 14c3.64 0 7.06.97 10 2.68V20H0v-3.32z"/>
          </svg>
      
          <div class="leading-normal">
            Some demo text
          </div>
        </div>
      </div>

      【讨论】:

      【解决方案4】:

      通过更改 widthheight 对我有用:

      <svg class="flex-no-shrink fill-current" fill="none" xmlns=
      "http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="50px" height="50px">
      

      https://jsfiddle.net/facechomp/58Lbqzh1/

      【讨论】:

      • 是的,但看起来还是有问题。当我将其更改为例如 width="200px" height="200px" 时,svg 变得更大,但我也看到底部和右侧有一个大的填充。这是哪里来的?
      • @sanders 我明白你的意思。这与 SVG 有关,因为当我尝试 jpg 时,不会发生这种情况。
      • @sanders 当我更改 viewBoxwidthheight settings 并同时将 scale() 应用于 SVG 中的路径时,我的运气会更好:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多