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.

Add ID attribute to root element

See original GitHub issue

Would there be any support for adding an ID with the icon name to the root <svg> element?

This would allow for referencing the icons in an SVG <use> element. For example, adding id="hi-archive" would allow importing via:

<svg class="h-6 w-6 text-red-500">
  <use xlink:href="/img/heroicons/outline/archive.svg#hi-archive"/>
</svg>

This would be ideal for things like menu icons that are used on every page, eliminating the need to embed the full SVG each time while still allowing them to have the color styled.

It would increase the file size of the SVGs, but tools like svgo’s cleanupIDs can be used to mitigate this if you’re not using them.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
Th3S4mur41commented, Nov 11, 2020

@davecardwell wouldn’t it make more sense to use both folder and filename as id?

e.g. heroicons/outline/archive.svg should get the ID outline-archive That way you can also combine them in a sprite, which would not work with the same hardcoded ID for each image.

0reactions
Th3S4mur41commented, Nov 19, 2020

Found a solution through webpack configuration that doesn’t require editing the assets… The following config in my Vue project add automatically the file name as ID when creating the sprite

configureWebpack: {
		module: {
			rules: [
				{
					test: /\.svg$/,
					use: ['svg-sprite-loader', 'svgo-loader']
				}
			]
		}
	},
chainWebpack: (config) => {
		config.module
			.rule('svg')
			.test(() => {
				return false;
			})
			.use('file-loader');
	}

That makes using any icon from the sprite possible with just <svg><use href="#icon_name" /></svg>

Read more comments on GitHub >

github_iconTop Results From Across the Web

XSLT -- generate unique id for root element and append that ...
I need to: (1) Generate a unique id attribute for the root element. (2) Append ...
Read more >
Adding an id-attribute to element - Oxygen XML Forum
Hello, I've got another newbie question. How can I add an id-attribute with consecutive numbers in it in my xml.file.
Read more >
There was need for the id attribute on the root element
Since we style the root element in XML , there was a certain need for the id attribute on the html element in...
Read more >
The HTML Document / Root element - MDN Web Docs - Mozilla
The HTML element represents the root (top-level element) of an HTML document, so it is also referred to as the root element.
Read more >
Identity attribute - OASIS Open
Within the same document, the topic id alone suffices (as in #topicid ). Where the topic element is the root element of the...
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