css - 如何正确地将样式应用于 SVG 类?

标签 css

我有这个 SVG:

#lockSVG {
  display: block;
  width: 50px;
  height: 65px;
}

#lockSVG .lock-outer-line {
  stroke-dasharray: 20 !important;
}

#lockSVG .lock-circle {}

#lockSVG .lock-upper-line {}
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="lockSVG" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 25 35" style="enable-background:new 0 0 25 35;" xml:space="preserve">
    <style type="text/css">
        .lock-outer-line {
            fill:none;
            stroke:#686868;
            stroke-width:2;
            stroke-linecap:round;
            stroke-linejoin:round;
            stroke-miterlimit:10;
        }
        .lock-circle {
            fill:none;
            stroke:#686868;
            stroke-width:2;
            stroke-linecap:round;
            stroke-miterlimit:10;
        }
        .lock-upper-line {
            fill:none;
            stroke:#686868;
            stroke-width:2;
            stroke-linecap:round;
            stroke-miterlimit:10;
        }
    </style>
    <path class="lock-outer-line" d="M4.4,13.5c-1.2,0.8-2,2.1-2,3.6v4c0,2.8,1.1,5.4,3.1,7.4c1.9,1.9,4.5,2.9,7.2,2.9c0.1,0,0.2,0,0.3,0
        c5.5-0.1,10-4.9,10-10.5v-3.8c0.1-1.8-0.9-3.3-2.4-4l-6.5-2.7c-0.8-0.3-1.8-0.4-2.6,0L10.1,11"/>
    <circle class="lock-circle" cx="12.7" cy="21.9" r="2.9"/>
    <path class="lock-upper-line" d="M7.1,15.1V9.9c0-3.1,2.5-5.6,5.6-5.6h0c3.1,0,5.6,2.5,5.6,5.6v8"/>
</svg>

我想设计/玩它的一些台词。片段中的代码有效,stroke-dasharray: 20 !important;部分已渲染,但在我这边,即使我要粘贴 <style></style>在 SVG 本身旁边,在它之后,甚至在我原来的 .css 中文件,它只是不生效。

我的标记是:<img id="lock" src="<?php echo esc_url( ../../lock_animatable.svg'); ?>"> ,所以我将我的 SVG 包装在 img 中标签。

我做错了什么?就像文档没有单独加载此 SVG 的 CSS。

最佳答案

我在将 CSS 声明添加到 SVG 文件时通常使用的标准语法是:

<defs>
<style type="text/css"><![CDATA[

[... STYLES HERE...]

]]></style>
</defs>

工作示例:

<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     viewBox="0 0 25 35">

<defs>
<style type="text/css"><![CDATA[

svg {
  display: block;
  width: 50px;
  height: 65px;
}

.lock-outer-line {
  fill: none;
  stroke: #686868;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-miterlimit: 10;
  animation: strokeDashArray 6s linear forwards;
}

@keyframes strokeDashArray {
  0% {stroke-dasharray: 0;}
  100% {stroke-dasharray: 20;}
}

.lock-circle {
  fill: none;
  stroke: #686868;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-miterlimit: 10;
}

.lock-upper-line {
  fill: none;
  stroke: #686868;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-miterlimit: 10;
}

]]></style>
</defs>


<path class="lock-outer-line" d="M4.4,13.5c-1.2,0.8-2,2.1-2,3.6v4c0,2.8,1.1,5.4,3.1,7.4c1.9,1.9,4.5,2.9,7.2,2.9c0.1,0,0.2,0,0.3,0 c5.5-0.1,10-4.9,10-10.5v-3.8c0.1-1.8-0.9-3.3-2.4-4l-6.5-2.7c-0.8-0.3-1.8-0.4-2.6,0L10.1,11" />

<circle class="lock-circle" cx="12.7" cy="21.9" r="2.9" />

<path class="lock-upper-line" d="M7.1,15.1V9.9c0-3.1,2.5-5.6,5.6-5.6h0c3.1,0,5.6,2.5,5.6,5.6v8" />
</svg>

关于css - 如何正确地将样式应用于 SVG 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089353/

相关文章:

javascript - 如何创建带有水平滚动条的选择元素?

jquery - 带有固定标题和第一列的 Twitter Bootstrap 表

css - 创建一个 div 正方形,并使用 less 将其相应地居中于视口(viewport)

html - 绝对定位的过渡

javascript - 范围输入以更改文本和图像

html - 将 border-bottom 添加到嵌套列表项

html - 如何创建自定义形状

css - 如何更改mat-select的背景颜色并将选择框的边缘更改为圆形样式?

html - 根据上下文使用 CSS 显示不同的背景图像(无需明确声明每个单独的背景图像)

jquery - 替换 Div 内容 onclick