[英]Stylizing SVG element using ReactJS with CSS-Modules
I have an app which is worked on ReactJS
. 我有一个适用于ReactJS
的应用程序。 I'm using CSS-Modules for inline styles. 我正在使用CSS-Modules来实现内联样式。 I have a separate svg
file with icon and I want to stylize it in my component's css
file. 我有一个单独的带有图标的svg
文件,我想在我的组件的css
文件中对其进行样式化。
Here is an icon MyComponent/add.svg : 这是一个图标MyComponent / add.svg :
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 533.333 533.333" style="enable-background:new 0 0 533.333 533.333;" xml:space="preserve;">
<g>
<path d="M516.667,200H333.333V16.667C333.333,7.462,325.871,0,316.667,0h-100C207.462,0,200,7.462,200,16.667V200H16.667 C7.462,200,0,207.462,0,216.667v100c0,9.204,7.462,16.666,16.667,16.666H200v183.334c0,9.204,7.462,16.666,16.667,16.666h100 c9.204,0,16.667-7.462,16.667-16.666V333.333h183.333c9.204,0,16.667-7.462,16.667-16.666v-100 C533.333,207.462,525.871,200,516.667,200z" />
</g>
</svg>
Here is my MyComponent/MyComponent.css 这是我的MyComponent / MyComponent.css
.iconButton {
}
.icon {
height: 50px;
width: 50px;
margin-left: 25px;
margin-top: 25px;
margin-right: 25px;
margin-bottom: 5px;
}
.iconAdd {
composes: icon;
background: url('./add.svg');
}
And it's a piece of code from my MyComponent/MyComponent.jsx 它是我的MyComponent / MyComponent.jsx中的一段代码
import React, { Component } from 'react';
import styles from './MyComponent.css';
export default class MyComponent extends Component {
render() {
return (
<div className={ styles.iconButton }>
<div className={ styles.iconAdd }></div>
<div>add</div>
</div>
);
}
}
It works OK. 它运作正常。 But I've faced the issue when I try to apply style for svg
item (for example: fill) it doesn't work and nothing changes. 但是当我尝试为svg
项目应用样式时我遇到了这个问题(例如:fill)它不起作用而且没有任何变化。
Should you recommend a way to implement this case? 你应该推荐一种实现这种情况的方法吗?
Thanks for watching this question. 谢谢你看这个问题。
I was need to somehow manipulate with SVG
icons in my app. 我需要以某种方式操纵我的应用程序中的SVG
图标。 That's why I've made use of @robjez advice and inline them first. 这就是我使用@robjez建议并首先内联它们的原因。
I've implemented a few React
components for every icon that I need to stylize. 我已经为每个我需要样式化的图标实现了一些React
组件。 A little sample is below: 下面是一个小样本:
export default class FolderIcon extends Component {
render() {
return (
<svg viewBox="0 0 475.082 475.082">
<g>
<path d="M456.239,128.475c-12.56-12.562-27.597-18.842-45.11-18.842h-191.86v-9.136c0-17.511-6.283-32.548-18.843-45.107 c-12.562-12.562-27.6-18.846-45.111-18.846H63.953c-17.515,0-32.551,6.283-45.111,18.846C6.28,67.949,0,82.986,0,100.497v274.088 c0,17.508,6.28,32.545,18.842,45.104c12.562,12.565,27.6,18.849,45.111,18.849h347.175c17.514,0,32.551-6.283,45.11-18.849 c12.566-12.56,18.843-27.597,18.843-45.104V173.59C475.082,156.078,468.805,141.042,456.239,128.475z" />
</g>
</svg>
);
}
}
After that I have a possibility to use CSS Modules and stylize SVG
from code of a Component
. 之后我有可能使用CSS模块并从Component
代码中设置SVG
样式 。
.icon svg {
fill: #FFFFFF;
}
That's the sample of using: 这是使用的样本:
import FolderIcon from '../FolderIcon.jsx';
import styles from './Folder.css';
export default class Folder extends Component {
render() {
return <FolderIcon className={styles.icon} />;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.