Is it possible for jQuery to embed svg content directly into a css background image using the url

Is there a way in jQuery to extract SVG data from a div within an HTML page and insert it into a CSS background-image URL? Rather than loading an external SVG file, I want the HTML file to be completely self-contained. Here's what I have been attempting:

var svgData = $("#div-containing-svg").html()
$("other-div").css({backgroundImage:svgData});

Can this be done? And if so, will it function consistently across different browsers?

Answer №1

To accomplish this task, consider implementing the following code:

let image = $('#svg-container').html();
$('#elem').css('background-image', 'data:image/svg+xml;utf8,' + image);

Will this function work on all web browsers? Check out the compatibility chart here: http://caniuse.com/svg-css

  • Remember to properly url escape the SVG content

Answer №2

Check out this live demonstration inspired by @minko-geschev:

https://github.com/minko-geschev

This solution showcases the use of base64 encoding rather than escaping characters.

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

Style Vue slots one by one

I am a beginner delving into the world of vue (specifically, vue 2) and aiming to grasp the concept of utilizing slots in the most effective manner. Below is the code I'm working with: <template> <div> <button cla ...

Presenting field information from a MySQL table by choosing the field name from a dropdown menu

I am trying to populate a drop-down list with field names using the code below. Once a field name is selected, I want to display its contents in a table on the page. The table will have only one column as only one field name can be selected at a time. I ha ...

Ways to enhance multiple ng-repeats efficiently with css grid

Looking to create a CSS grid table with 5 columns and an undetermined number of rows. The goal is to display a pop-up element when an element in the first column is clicked, covering columns 2 through 5. This ensures that only the first column remains visi ...

Suggestions for preventing the highlighting of the space between buttons on a webpage

html: <button id='automoney' onclick='minusTen()'></button> <button id='automoney2' onclick='minusHundred()'></button> <button id='automoney3' onclick='minusFiveHundred()& ...

Troubleshooting: Difficulty with jQuery script to change images

I am trying to modify the src attribute of an img tag using the code below, but it doesn't seem to be working. Can anyone help me figure out what's wrong? <img id='logo_image'/> <span onclick='$(logo_image).attr("src", "i ...

Data-id attribute fails to appear when applied to an element hidden with display:none property

Creating a notification popup feature using bootstrap popover. Each notification is represented by a div with the same classes, but different text content. <% Loop through notifications and create a div for each %> <div class="media n-media n ...

What could be causing the variation in appearance between my website on PC and Android compared to iOS when viewing a ReactJS page?

My reactjs web page is fully responsive and displays consistently on Google Chrome for PC and Android. However, I have noticed that there are some differences when viewing it on Google Chrome for iOS. In particular, the loading animation and green line gen ...

Extract CSS from Chrome developer tools and convert it into a JavaScript object

Recently, we have started incorporating styles into our React components using the makeStyles hook from Material-UI. Instead of traditional CSS, we are now using JavaScript objects to define styles. An example of this is shown below: const useStyles = ma ...

Excessive Blank Areas

I needed to create 3 horizontal paragraphs side by side, and the only solution I found was using the position property, but it didn't turn out as expected. Despite my efforts to make it look clean, it always ends up like this. Below is the code I use ...

Enhancing Websites with the Power of Jquery and CSS Animation

I am trying to slow down the animation of increasing and decreasing height using jQuery. Currently, I am using .css() to adjust the height of a div element. Can someone please provide assistance? <script> $(document).ready(function(){ $(".st ...

Switching between React components based on a click event

I want to display a component in another component when it is clicked, similar to this scenario: The DevStatus component consists of two Bootstrap radio buttons. Meanwhile, the IssuesList component includes a Bootstrap list. You can view the code on Code ...

"Create a responsive design featuring two columns on desktop and a single column on mobile using Flexbox layout

Seeking a solution for a flexible layout using flexbox: Requirements for Desktop Layout Consist of two responsive columns 1st column should occupy 30% of the width 2nd column should occupy 70% of the width Mobile Layout Specifications Single column t ...

Rails 6 does not have support for matching multiple route parameters

routes.rb: get '/get_text_by_tablenum/:filename_id/:tablenum_id', to: 'dashboard#get_text_by_tablenum' ajax: $.ajax({ dataType: "json", cache: false, url: '/get_text_by_tablenum/' + filename + &apo ...

A problem arises when making an ajax call

I'm struggling with an Ajax call made using the jQuery .blur() method. Every time I try to execute it, an error message pops up. Any idea why this is happening? Here's my jQuery/Ajax Code: <script> $("#given_name").blur(function(){ va ...

Are there any drawbacks to utilizing data attributes for modifying content through JavaScript?

On my journey of developing a basic web app, I am faced with the challenge of updating data displayed on the screen based on certain events or conditions. The data is spread across various locations and embedded in completely different markup structures. I ...

Utilize the DataTables API by referencing the unique identifier of the table

Incorporating jQuery UI Tabs into my project has presented a challenge. I have multiple DataTables, each within its own tab. My goal is to add selected rows from a dialog box to the table in the current tab. As all of the tables are named with the same n ...

Adjusting the margin on an element causes a shift in the entire page's

Why is the margin of an h2 element on my page displacing the entire body downward? This seems abnormal since the h2 element is within a div container. Is there a way to address this issue? https://i.sstatic.net/E3CLW.png ...

Alter caret appearance in Bootstrap 4 when dropdown is clicked

Struggling with Bootstrap 4, I aim to craft a dropdown including a caret that points right by default. Upon clicking the dropdown, the caret direction should switch to pointing down. I discovered a method to achieve this /*For complete code details, plea ...

Dropdown feature within a Bootstrap panel tab

Take a look at this unique example showcasing panels with tabs and a dropdown feature: I'm curious if there's a way to customize the dropdown button to function as its own tab, where the dropdown menu only appears when clicking on the arrow sect ...

CSS Layout - Float: What's floating to the top?

Are there any CSS techniques available to make floated blocks fill in both upwards and in their float direction? For example - https://i.sstatic.net/uo06B.png Instead of - https://i.sstatic.net/oEijA.png I know this can be achieved using JavaScript li ...