Hello,
I have anchor links that represent tags, and I want to display them inside a colored box. Inside the box, I would like to place a small SVG icon on the left side, next to the links, like so:
I’m using inline-flex on the anchor element, but I’m not sure if that’s the right approach. Also, I’m not sure if repeating the svg markup for each tag would be an issue.
I appreciate any suggestions you might have. Thank you.
Thank you both! I really appreciate your time. I tried moving the text up by about 2px using line-height, but it didn’t work. Is there a way to nudge it up just a bit? If not, I’ll leave it as is.
Also, I have multiple SVGs, but not all of them are on the same page. For example, I have an SVG representing a clock or a small calendar, which are used for blog posts to display the publication date. I was wondering if I could define all the SVGs in index.html, even if some of them aren’t used on index.html. Would that be an issue? Normally, I don’t think it should cause performance problems since they are just definitions at the bottom of the page (before the footer element), right? The thing is, the definitions will be loaded on every page, but at least I’ll know that they are available.
The easiest way wold be to put a span around it and nudge it up a bit.
.tag span {
transform: translateY(-2px);
}
<a class="tag" href="">
<!-- Reuse the same symbol -->
<svg class="icon-tag" width="24" height="24">
<use href="#icon-tag"></use>
</svg>
<span>tag3</span>
</a>
However, they seemed to have been perfect before they were moved to me
(Note: I removed the xlink as that’s deprecated in favour of link in the use element)
To reuse the svg you have to have the target on the same page so as you suggest you would have a lot of extra html if you include all your sites svg definitions on every page.
I suppose it depends on how many you actually have and whether it makes it easier for you to manage like that.
Yes, but using a <defs> element like Paul suggested is probably a better idea. Last time I checked Google PageSpeed Insights, it said having too many SVGs put more workload on the DOM. This might be because of the many attributes they have. When I got that warning, I had copied and pasted the same SVGs several times instead of using a separate definition.