TailwindCSS:SVG 图标无法正确调整大小

san*_*ers 4 html css svg tailwind-css

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

not*_*tch 8

在 TailwindCSS 中更改 SVG 宽度和高度

Adam Wathan(TailwindCSS 创建者)在此处的视频中介绍了使用 SVG 的解决方案。他的解决方法如下:

更改 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>
Run Code Online (Sandbox Code Playgroud)
   <!--      \/---- 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>
Run Code Online (Sandbox Code Playgroud)

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


Ale*_*_TT 6

要调整图标大小,请调整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>
Run Code Online (Sandbox Code Playgroud)


Ted*_*Ted 5

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

function changeHandler(event){
  var svg = document.getElementById('svg');
  svg.setAttribute("width", event.target.value + 'px');
  svg.setAttribute("height", event.target.value + 'px');
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)