首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用`fill=“<linearGradient> (#a)”`时,如何使用带有Tailwind CSS的SVG url样式?

在使用`fill=“<linearGradient> (#a)”`时,如何使用带有Tailwind CSS的SVG url样式?
EN

Stack Overflow用户
提问于 2021-03-13 21:03:39
回答 2查看 967关注 0票数 4

我看过@adamwathan的现场直播&他用className="w-5 h-5 text-white" fill="currentColor"通过Tailwind设计了一个SVG的样式。

如何对linearGradient执行相同的操作

我有以下SVG:

代码语言:javascript
复制
import React from 'react'

export const LinearGradient = () => (
    <svg className="w-5 h-5" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg">
        <defs>
            <linearGradient x1="50%" y1="92.034%" x2="50%" y2="7.2%" id="a">
                <stop offset="0%" />
                <stop stopOpacity="0" offset="100%" />
            </linearGradient>
        </defs>
        <circle
            className="text-white"
            stroke="currentColor"
            fill="url(#a)"
            cx="8.5"
            cy="8.5"
            r="6"
            fillRule="evenodd"
            fillOpacity=".8"
        />
    </svg>
)

如何在完美使用fill="url(#a)"的SVG中设计linearGradient样式?我无法更改fill="currentColor",因为它将丢失对id="a"的引用。

最初的SVG在https://www.sketch.com/images/icons/mac/monochrome/17x17/circle.gradient.linear.svg

有什么解决方案吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-13 22:06:49

要设置linearGradient颜色的样式,可以在<stop>元素上使用stop-color属性。

代码语言:javascript
复制
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">

<svg class="w-32 h-32 text-blue-500" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg">
    <defs>
      <linearGradient x1="50%" y1="92.034%" x2="50%" y2="7.2%" id="a">
        <stop offset="0%" stop-color="currentColor" />
        <stop stop-opacity="0" offset="100%" stop-color="white" />
      </linearGradient>
    </defs>
    <circle stroke="currentColor" fill="url(#a)" cx="8.5" cy="8.5" r="6" fill-rule="evenodd" fill-opacity=".8" />
</svg>

票数 4
EN

Stack Overflow用户

发布于 2021-05-11 23:45:35

您还可以从您的tailwind.config.js创建可在SVG中使用的变量。

下面是一个如何在Laravel 8项目中执行此操作的示例。

tailwind.config.js

代码语言:javascript
复制
const colors = require('tailwindcss/colors');

module.exports = {
    theme: {
        colors: {
            blue: {
                300: colors.blue[300],
                500: colors.blue[500],
            },
            ...

resources/css/ariables.css

代码语言:javascript
复制
:root {
    --color-blue-300: theme('colors.blue.300');
    --color-blue-500: theme('colors.blue.500');
}

resources/css/app.css

代码语言:javascript
复制
@import './variables.css';

@import 'tailwindcss/base';
...

resources/views/svg/my-svg.blade.php

在这里,我在另一个视图(例如: my-layout.blade.php)中使用@include("svg.my-svg")。使用它而不是<img src="my-svg.svg"...,允许顺风的类影响svg。

代码语言:javascript
复制
...
<defs>
    <linearGradient id="grad1" x1="0%" y1="100%" x2="100%" y2="0%">
        <stop offset="0%" style="stop-color:var(--color-blue-300);" />
        <stop offset="100%" style="stop-color:var(--color-blue-500);" />
    </linearGradient>
</defs>
...
<path style="fill: url(#grad1);" ...
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66613843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档