Is it possible to include an element id in a CSS URL request?

I have an SVG file containing a series of <symbol> elements, each with unique ids. What I am trying to achieve is to use the background-image property in an HTML file to set the element with the id I choose, like this:

background-image: url(“sprite.svg#symbol_1”);

This syntax works as "src" in an <img> or as "href" in a <use> , but for some reason, I cannot get it to work in CSS. If I place the symbols in a <defs> container and then set a <use> element in the SVG file, the symbol displays when I remove the "#symbol_1", but then I lose the ability to change it.

Answer №1

I recently managed to figure this out with the help of a useful link shared by @enxaneta. The link provided detailed instructions on how to achieve this, but here's a simplified version. By adding a style block like the following in your svg file:

<style>
  g {display: none;}
  g:target {display: inline;}
</style>

You can easily group the components of each image (paths, shapes, etc), assign an id to the group, and set the root svg element to the viewport size. Then, you can use the id reference for the group in your css.

The most challenging part for me was ensuring that all images fit within a single viewport, as Inkscape sometimes generates path commands randomly as relative or absolute. However, I ended up converting all paths to "d" values for path classes in the style block and removing any unnecessary transforms, which significantly reduced the file size by approximately 40%.

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

Emphasize sections of text within a chart

Looking for a Specific Solution: I've encountered similar problems before, but this one has a unique twist. What I'm trying to achieve is to search for a substring within a table, highlight that substring, and hide all other rows (tr's) th ...

Use Vue.js to shorten the number of decimal places and include commas in numerical values

How can I best parse a long number retrieved from a JSON using VueJS within a for loop? The JSON data is used in a v-for prop to create Li elements for a side navigation. I attempted to use vue-numeric without success since the project does not utilize ECM ...

A workaround for background-size in Internet Explorer 7

Currently, I am working on a project at this link: Everything seems to be functioning well in Firefox and Chrome, however, I have encountered an issue with the background-size property not being supported in IE7. I heavily rely on this property throughout ...

The submission of the HTML Form is experiencing difficulties in Internet Explorer 9

Having an issue with my basic HTML form not submitting in IE9, although it works fine in Chrome, FF, and IE10. I've attempted to submit the form in IE9 on Windows 7 and IE10 on Windows 8. In IE10, everything is functional. However, if I switch the b ...

Bootstrap - the ability to resize elements dynamically

I'm currently working on a layout using Bootstrap and I could use some guidance to achieve the desired outcome. Essentially, what I am trying to accomplish is having resizable elements on the screen. When there's only one element, it should take ...

Interactive button with jQuery that toggles between clicked and unclicked states, automatically reverting to unclicked when clicked elsewhere on the page

I am trying to create a button that changes its appearance when clicked, and returns to its normal state if clicked again or if the user clicks outside of it. Here is the JavaScript code I have written: if($('#taskbar-start').hasClass('ta ...

The art of linking JavaScript events

I'm encountering some challenges with linking events together. I've set up an eventListener on a variety of links that, when hovered over, trigger a drop-down menu to appear. Everything is functioning properly, but I also want triangular shapes ...

When the browser screen is minimized, the menu bar toggler in my Angular project stops functioning properly. I rely on Bootstrap features within Angular to handle this

When the browser is at full-screen mode, HTML works perfectly. However, when I resize the browser to a smaller size, the navbar disappears and a toggle is shown - which is the expected behavior. If I click on the toggle, the menu list should be displayed ...

Unable to cycle through an array of objects in JavaScript. Only receiving output for the initial element

var people = new Array(); var individual = { first_name: "Padma", identification_number: 1, region: "India" }; people.push(individual); people.push([individual = { first_name: "Balaji", identification_number: 3, region: "India" }]); people. ...

Using CSS nth-child to create a two-column checkers layout

I have formulated a form on my website that divides into two columns by using floats. I am interested in creating a "checkered" effect for these columns. Can anyone guide me on how to utilize nth-child to achieve this design layout? For reference, please c ...

Executing a JavaScript function using document.write()

When I try to click on the SWF part1 links within the document.write() function in order to call the openswf function, nothing happens. Here is my code: <html> <a href="#" onclick="Popup();">show popup</a> <script> functio ...

How can you prevent PHP from continuing execution after receiving a false return value in Javascript?

I have implemented a JavaScript validator for phone numbers and it is functioning correctly. However, even after returning false, the PHP code continues to execute. How can I prevent this from happening? Below is my JavaScript function: function validate ...

What methods can be used to prevent divs from overlapping when the window size is reduced?

Creating a login page with two main content divs in a section layout. The first div contains the logo, input prompts, and login/password reset buttons. The second div serves as a footer with social media links. On a full-size window, the logo is positioned ...

Is it possible to implement addEventListener.delay() functionality?

Hello, I am working on a Qualtrics code that utilizes jQuery. I am looking to incorporate a delay function for event listening in my code, which currently allows key presses as inputs. Specifically, I need to disable the keys for the initial 2000ms before ...

Is it beneficial to modify the title/alt attribute of an image for SEO purposes?

In my application, I utilize Angular and have implemented the ng-repeat directive for images. This means that in my HTML, I only have one img tag for all the image objects fetched from the server. Should I also include unique titles and alt tags for thes ...

Stopping an endless loop in JavaScript by pressing a key on the keyboard can be a useful

I've been working on creating a JavaScript game and am currently tackling the challenge of implementing gravity. One crucial aspect I need to address is creating an infinite loop without causing the browser to crash. Is there a way for me to include a ...

Combining Bootstrap Vue: utilizing class names alongside HTML tags

(Bootstrap-Vue 2.0, Vue.js 2.5) Is it possible to combine traditional CSS Bootstrap 4 classes with Bootstrap-Vue? For example, can I use the following code snippet: <section id="introduction"> <b-container class="h-100"> & ...

Both the attributes `name="button"` and `name="submit" are functioning correctly within the form

While working with Django framework, Interestingly, both name="button" and name="submit" function correctly upon submitting the form. {% buttons %} <button name="button" class='btn btn-primary'>Save</button> {% endbu ...

Stylish CSS: Jagged 3-dimensional rotated text

Have a look at this Codepen to see the issue. As the text moves into the background, more aliasing appears. A screenshot taken in Chrome on macOS (similar appearance in FF & Safari): https://i.sstatic.net/n746I.png I've experimented with different ...