【问题标题】:Change SVG stroke color in CSS [duplicate]在 CSS 中更改 SVG 笔触颜色 [重复]
【发布时间】:2022-01-21 21:16:06
【问题描述】:

由于某种原因,在 CSS 中设置 color: #ffffff 不适用于我的 SVG。

下面的代码看起来如何

我希望它看起来如何

有趣的是,如果我在 SVG 本身中将 currentColor 替换为 #ffffff,它会起作用,但在我的 CSS 中设置它不会做任何事情。我还尝试在 HTML (style="color:#ffffff") 中将其设置为内联,并设置stroke: #ffffff,但这也没有任何作用。

我没有想法,所以如果有人知道如何做到这一点,以及为什么在 css 中设置它不起作用,请告诉我。

这是我的代码:

下载.svg:

<svg 
    xmlns="http://www.w3.org/2000/svg" 
    width="24" 
    height="24" 
    viewBox="0 0 24 24" 
    fill="none" 
    stroke="currentColor" 
    stroke-width="2" 
    stroke-linecap="round" 
    stroke-linejoin="round" 
    class="feather feather-download"
  >
    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
    <polyline points="7 10 12 15 17 10"></polyline>
    <line x1="12" y1="15" x2="12" y2="3"></line>
</svg>

我的头像:

<button type='button' class='btn btn-primary' on:click={ExportXlsx}>
  Last ned
  <img class='test' alt='download' src='/images/download.svg' />
</button>

我的CSS:

.test{
  color: #ffffff !important;
}

【问题讨论】:

标签: html css svg svelte bootstrap-5


【解决方案1】:

方法#1

您需要为stroke 属性指定颜色:

stroke="rgb(255, 255, 255)"

工作示例:

button {
  height: 48px;
  padding: 0 12px;
  font-size: 18px;
  line-height: 48px;
  font-weight: 900;
  color: rgb(255, 255, 255);
  background-color: rgb(0, 0, 63);
  border: none;
  border-radius: 9px;
  box-sizing: border-box;
}

button svg {
  margin-left: 4px;
  transform: translateY(4px);
}
<button type="button">
Eksporter

<svg 
    xmlns="http://www.w3.org/2000/svg" 
    width="24" 
    height="24" 
    viewBox="0 0 24 24" 
    fill="none" 
    stroke="rgb(255, 255, 255)" 
    stroke-width="2" 
    stroke-linecap="round" 
    stroke-linejoin="round" 
    class="feather feather-download"
  >
    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
    <polyline points="7 10 12 15 17 10"></polyline>
    <line x1="12" y1="15" x2="12" y2="3"></line>
</svg>

</button>

方法#2

您可以使用 CSS 代替 SVG 属性。


工作示例:

button {
  height: 48px;
  padding: 0 12px;
  font-size: 18px;
  line-height: 48px;
  font-weight: 900;
  color: rgb(255, 255, 255);
  background-color: rgb(0, 0, 63);
  border: none;
  border-radius: 9px;
  box-sizing: border-box;
}

button svg {
  width: 24px;
  height: 24px;
  margin-left: 4px;
  transform: translateY(4px);
  stroke-width: 2px; 
  stroke-linecap: round;
  stroke-linejoin: round;
}

button.red svg {
  stroke: rgb(255, 0, 0);
}

button.yellow svg {
  stroke: rgb(255, 255, 0);
}

button.green svg {
  stroke: rgb(0, 255, 63);
}
<button type="button" class="red">
Eksporter

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
    <polyline points="7 10 12 15 17 10"></polyline>
    <line x1="12" y1="15" x2="12" y2="3"></line>
</svg>

</button>

<button type="button" class="yellow">
Eksporter

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
    <polyline points="7 10 12 15 17 10"></polyline>
    <line x1="12" y1="15" x2="12" y2="3"></line>
</svg>

</button>

<button type="button" class="green">
Eksporter

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
    <polyline points="7 10 12 15 17 10"></polyline>
    <line x1="12" y1="15" x2="12" y2="3"></line>
</svg>

</button>

【讨论】:

  • 没有内联 svg 有没有办法做到这一点?我在几个地方使用相同的 svg,所以如果可能的话我不想使用内联。
  • @Hackiss 是的,您可以直接在 SVG 文件中更改 stroke
  • @maiakd 是的,但是如果我想在其他地方使用不同的颜色,我必须制作另一个 SVG。有什么方法可以在不制作新 SVG 的情况下从 HTML 或 CSS 文件中设置颜色?
  • 您可以使用包含(部分)将您的 svg 放在单独的文件中,因此您不必将 svg 代码放在要显示它的每个地方,但内联 SVG 是用 css 改变颜色的最佳方法
  • Re: “有没有什么方法可以在不制作新 SVG 的情况下从 HTML 或 CSS 文件中设置颜色?” 是的。您可以用 CSS 替换 SVG 属性。请参阅上面的方法 #2
猜你喜欢
  • 2016-10-22
  • 2019-09-08
  • 2019-11-03
  • 1970-01-01
  • 2022-11-17
  • 2019-05-09
  • 2021-05-16
  • 2016-08-02
相关资源
最近更新 更多