SVG图标无法正确调整大小

时间:2018-12-04 19:50:19

标签: 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>

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

4 个答案:

答案 0 :(得分:3)

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

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>

答案 1 :(得分:3)

在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 :(得分:2)

要调整图标的大小,请调整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>

答案 3 :(得分:1)

通过在以下位置更改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/

相关问题