Can an SVG be referenced from another location within the HTML document?

I am dealing with extensive dynamic lists that are associated with a large number of individual (generated) SVGs, each representing stylized acronyms.

I prefer not to store the generated SVGs in separate files as it would result in numerous requests. Despite using HTTP/2, I find this solution unsatisfactory due to the unnecessary overhead it creates.

Inline inclusion of the full SVG every time is also not ideal. It leads to redundant data for identical acronyms (which may appear multiple times on the page) and given the high dynamism of the page, I believe changing a background image is more efficient than altering the DOM with an SVG.

Therefore, my query is whether it is feasible to reference an SVG defined elsewhere within the document?

I am seeking something akin to:

<!-- generated by server -->
<div style="display: hidden;"> 
    <svg id="acronym-abc">
        [...]
    </svg>
    <svg id="acronym-xyz">
        [...]
    </svg>
</div>

<!-- generated by client -->
<ul>
    <li style="background: SVG(id=acronym-abc)"></li>
    <li style="background: SVG(id=acronym-xyz)"></li>
    <li style="background: SVG(id=acronym-abc)"></li>
    [...]
</ul>

UPDATE

While I did not explicitly mention it earlier, I am specifically looking for a solution that can be used in conjunction with background-image and is compatible with IE9. Otherwise, I will consider the answer provided by @web-tiki.

Answer №1

To streamline the use of symbols, consider utilizing the <symbol> element in conjunction with the <use> tag. While this method may not function as a background image, it enables you to reference each symbol uniquely and customize its appearance as needed.

For instance:

<svg width="0" height="0">
  <symbol id="circle" viewBox="0 0 2 2">
    <circle cx="1" cy="1" r=".7" />
  </symbol>
  <symbol id="square" viewBox="0 0 2 2">
    <path fill="darkorange" d="M.5 .5 H1.5 V1.5 H.5z" />
  </symbol>
</svg>
<p>
  <svg width="50" height="50"><use xlink:href="#circle" /></svg>
  <svg width="50" height="50"><use xlink:href="#circle" fill="teal" stroke="red" stroke-width=".1"/></svg>
  <svg width="50" height="50"><use xlink:href="#square" /></svg>
  <svg width="50" height="50"><use xlink:href="#square" fill-opacity=".5"/></svg>
</p>

For additional information, refer to the following resources on MDN:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips and tricks for implementing vuetify tooltips within a list component

Looking for help with my code: <div id='app'> <v-app> <v-list-tile-content> <v-list-tile v-for="(item, index) in items" :key="item.id" > <v-list-tile-action> {{index+1}}. </v-list-tile-action> < ...

An unusual problem arose within the Django template

When I use the action method in Django, I pass a parameter called deletable_objects to the template and it works perfectly. The code for this is: deletable_objects, perms_needed, protected = get_deleted_objects( queryset, opts, request.user ...

Error encountered: $(...).pickadate is not defined as a function

I am currently working on a basic web project that involves a simple form where users enter their name, birthday, and university degree. I am utilizing Materialize and jQuery for this project. However, I have encountered a frustrating issue while trying to ...

The scroll bar is acting peculiarly when the height is adjusted

I need assistance creating a chat widget that has a maximum height for the entire chat and allows the content to fill in without specifying fixed heights within the chat itself. Below is my code: HTML <section class="boxlr-chat"> <div class= ...

The DOM lacks the assigned 'id' in the HTML

While working in the Moodle platform (version 3.5.7) using the Atto editor in both Chrome and Firefox, I have been attempting to assign an ID to a specific row class called "span9". My main goal is to give this element a unique ID and then reference it wit ...

How to centrally align text in list items using Bootstrap 5

Incorporating Bootstrap 5 into my project, I am facing a challenge with vertically aligning text in an li element. Despite trying various techniques mentioned in the Bootstrap documentation for middle/center alignment, none of them seem to be effective. I ...

Restoring list item to standard CSS styling

I am having trouble with the alignment of my links. They are currently displayed in a horizontal manner instead of vertically like this: First link Second link Third link The current layout shows the links next to each other as follows: Firs ...

Clicking on the Edit button does not result in any changes to the HTML on the page following a table

My current setup involves a button that is supposed to toggle between 'Add New User' and 'Update User' elements when clicked. However, it seems to work only when the table has not been filtered. Once I apply any kind of filtering on the ...

Enhance HTML Performance with Resource Preloading

Just a quick query I wanted to throw out there - would prefetching resources like js scripts and images actually slow down the page load time? For instance, let's say I have a page with several links such as: <link rel="prefetch" href="http://exa ...

What causes the variation in table border display between Firefox, IE, and Chromium browsers?

I am utilizing Datatables to display a data table. Although it works correctly, Firefox shows a thicker border after every n rows : Is this a "Firefox feature" designed to make tables more readable? Or could it possibly be a CSS bug? Upon inspecting wi ...

Gathering information from a website by using BeautifulSoup in Python

When attempting to scrape data from a table using BeautifulSoup, the issue I'm running into is that the scraped data appears as one long string without any spaces or line breaks. How can this be resolved? The code I am currently using to extract text ...

Generating a table with two columns utilizing ng-repeat

I am currently dealing with an array of objects that I need to showcase in a two-column layout. The challenge arises because some columns have more data than others, requiring equi-height rows. Despite utilizing Bootstrap and clearfix in my attempts to di ...

Disabling an image is similar to deactivating a button

document.getElementById("buttonName").disabled = true; I have successfully disabled a button using the code above. However, I am now facing a challenge with using pictures as buttons that have an onClick function. Is there a way to disable the onClick fun ...

What is the best way to extract data from a textarea HTML tag and store it in an array before iterating through it?

I have a project in progress where I am developing a webpage that will collect links from users and open each link in a new tab. I am using the <textarea> tag in HTML along with a submit button for link collection. Users are instructed to input only ...

AngularJS - Exchange data between controllers through shared scope

I've encountered a situation with a textarea in my HTML that looks like this: <textarea ng-model="commentBox"></textarea> In one of my controllers, I can easily access the content of the textarea using "$scope.commentBox". However, I&apo ...

Tips for adding jQuery UI styling to elements generated dynamically

I have a challenge with generating text fields using jquery while applying jquery ui styling to the form. The issue is that the dynamically created elements do not inherit the css styles from jquery ui: let index = 0; $('#btn_generate').on(& ...

Aligning an Image Vertically

I'm working with a document structure that includes an unordered list containing div elements with images inside. The images have a set height of 150px, but sometimes the images are smaller than that. Is there a way to vertically position the images w ...

Scrollable divs in a Bootstrap layout

If you take a look at this fiddle I created, you can see what I've attempted. My goal is to have a webpage without scroll bars but with the col-left and col-right divs getting a scrollbar when they overflow. Could someone provide some insight on what ...

Is it possible for images to align vertically instead of horizontally in IE?

Encountering an issue with image alignment in Internet Explorer - they are aligning vertically instead of horizontally, while functioning correctly in all other browsers. Moreover, in IE8 and 7, the images are not being displayed at all. Is there a workaro ...

Precise column measurement

I'm currently working on a jQuery project where I need to create a table with columns of exact widths. For example, let's say I want the first column to be exactly 100px wide, including padding (10px on each side). I've tried various combin ...