question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add custom style at svg element

See original GitHub issue

I want add custom style into svg element when compile it. Because svg is not aligned with text by default

My config file like this

module.exports = {
    outDir: 'src/components',
    ref: true,
    icon: true,
    typescript: true,
    svgProps: {
        focusable: { },  // ✅ this attruibute add correctly
        style: { verticalAlign: '-0.125em' },   // ❌ align svg element with text or inline element
    },
};

Does svgr support passing object as svg props?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
raooooolcommented, Aug 18, 2022

Can be added with svgo’s addAttributesToSVGElement plugin. Like this:

module.exports = {
  svgoConfig: {
    plugins: [
      { name: "removeAttrs", params: { attrs: "(fill|stroke|height|width)" } },
      {
        name: "addAttributesToSVGElement",
        params: {
          attributes: [
            {
              style: "vertical-align:-0.125em;",
            },
          ],
        },
      },
    ],
  },
};

0reactions
hec9527commented, Aug 19, 2022

add config as below into your svgr.config.js, you can custom style at svg element. Maybe you can try the above method,it is more perfect

    svgoConfig: {
        plugins: [
            {
                name: 'preset-default',
                params: {
                    overrides: {
                        inlineStyles: {
                            onlyMatchedOnce: false,
                        },
                        removeDoctype: false,
                    },
                },
            },
            {
                name: 'addAttributesToSVGElement',
                params: {
                    attributes: [
                        {
                            style: 'vertical-align:-0.125em;',
                        },
                    ],
                },
            },
        ],
    },
Read more comments on GitHub >

github_iconTop Results From Across the Web

<style> - SVG: Scalable Vector Graphics - MDN Web Docs
The SVG <style> element allows style sheets to be embedded directly within SVG content. Note: SVG's style element has the same attributes as...
Read more >
How to apply a style to an embedded SVG? - Stack Overflow
This way, the SVG will be inlined, and you can easily apply styles using CSS. This custom element will work just like the...
Read more >
Styling — SVG 2
The 'style' element allows style sheets to be embedded directly within SVG content. SVG's 'style' element has the same attributes as the corresponding...
Read more >
SVG Properties and CSS
Presentation attributes are used to style SVG elements and can be used as CSS properties. Some of these attributes are SVG-only while others ......
Read more >
SVG and CSS - Cascading Style Sheets - Jenkov.com
It is possible to style your SVG shapes using CSS. By styling is meant to change the looks of the shapes. This can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found