Can I customize the color of an SVG image with CSS (jQuery SVG image substitution)?

This is my innovative solution for easily embedding and styling SVG images using CSS.

Traditionally, accessing and manipulating SVG elements with CSS has been a challenge without the use of complex JS frameworks. My method simplifies this process, making it straightforward to create simple icons with hover effects.

Inspired by text-to-image replacement techniques, I developed a unique approach specifically for SVG files that streamlines the customization process.

Here's the question:

What is the simplest way to embed an SVG image and apply CSS styling without relying on a JS-SVG framework?

Answer №1

To begin, incorporate an SVG graphic using an IMG tag within your HTML code. I personally created the graphic using Adobe Illustrator.

<img id="facebook-logo" class="svg social-link" src="/images/logo-facebook.svg"/>

This process is similar to embedding a regular image. Do remember to assign the IMG element a class of svg. The 'social-link' class is just for demonstration purposes. While not mandatory, the ID attribute can be quite useful.

Next, utilize the following jQuery script (either in a separate file or directly in the HEAD section).

    /**
     * Replace all SVG images with inline SVG
     */
        jQuery('img.svg').each(function(){
            var $img = jQuery(this);
            var imgID = $img.attr('id');
            var imgClass = $img.attr('class');
            var imgURL = $img.attr('src');

            jQuery.get(imgURL, function(data) {
                // Extract the SVG tag and disregard the rest
                var $svg = jQuery(data).find('svg');

                // Assign the replaced image's ID to the new SVG
                if(typeof imgID !== 'undefined') {
                    $svg = $svg.attr('id', imgID);
                }
                // Transfer the replaced image's classes to the new SVG
                if(typeof imgClass !== 'undefined') {
                    $svg = $svg.attr('class', imgClass+' replaced-svg');
                }

                // Eliminate any invalid XML tags as recommended by http://validator.w3.org
                $svg = $svg.removeAttr('xmlns:a');

                // Substitute the original image with the new SVG
                $img.replaceWith($svg);

            }, 'xml');

        });

The objective of the above code snippet is to search for all IMG elements with the 'svg' class and substitute them with inline SVG content retrieved from the linked file. This approach has the major advantage of enabling you to alter the color of the SVG utilizing CSS, such as:

svg:hover path {
    fill: red;
}

Moreover, the jQuery code ensures that the initial images' ID and classes are preserved, hence enabling CSS rules similar to these:

#facebook-logo:hover path {
    fill: red;
}

Or:

.social-link:hover path {
    fill: red;
}

You can observe a live demonstration of this technique here:

A more intricate version incorporating caching can be found here: https://github.com/funkhaus/style-guide/blob/master/template/js/site.js#L32-L90

Answer №2

Design

svg path {
    fill: #000;
}

Customization

$(document).ready(function() {
    $('img[src$=".svg"]').each(function() {
        var $img = jQuery(this);
        var imgURL = $img.attr('src');
        var attributes = $img.prop("attributes");

        $.get(imgURL, function(data) {
            // Fetch the SVG tag, exclude everything else
            var $svg = jQuery(data).find('svg');

            // Eliminate any invalid XML tags
            $svg = $svg.removeAttr('xmlns:a');

            // Go through IMG attributes and apply them on SVG
            $.each(attributes, function() {
                $svg.attr(this.name, this.value);
            });

            // Substitute IMG with SVG
            $img.replaceWith($svg);
        }, 'xml');
    });
});

Answer №3

Now, with the emergence of modern browsers (excluding IE11), you have the capability to utilize the CSS filter property. This feature extends its functionality beyond regular elements by also supporting SVG images. When it comes to color modifications, options like hue-rotate or invert are at your disposal, although adjusting individual colors separately is not possible. For instance, a CSS class can be applied to display an "disabled" version of an icon, turning the original saturated SVG picture into a light grey appearance:

.disabled {
    opacity: 0.4;
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
}

This alteration presents a light grey visualization in most browsers, while in IE and possibly Opera Mini, the fading effect due to the opacity remains noticeable, offering a different but satisfactory result that may not exactly align with greyscale.

Below showcases four distinct CSS classes applied to a bell icon from Twemoji: the original (yellow), the aforementioned "disabled" class, a hue-rotate transformed variant (green), and an invert manipulated one (blue).

.twa-bell {
  background-image: url("https://twemoji.maxcdn.com/svg/1f514.svg");
  display: inline-block;
  background-repeat: no-repeat;
  background-position: center center;
  height: 3em;
  width: 3em;
  margin: 0 0.15em 0 0.3em;
  vertical-align: -0.3em;
  background-size: 3em 3em;
}
.grey-out {
  opacity: 0.4;
  filter: grayscale(100%);
  -webkit-filter: grayscale(100%);
}
.hue-rotate {
  filter: hue-rotate(90deg);
  -webkit-filter: hue-rotate(90deg);
}
.invert {
  filter: invert(100%);
  -webkit-filter: invert(100%);
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <span class="twa-bell"></span>
  <span class="twa-bell grey-out"></span>
  <span class="twa-bell hue-rotate"></span>
  <span class="twa-bell invert"></span>
</body>

</html>

Answer №4

Another option is to implement CSS clip-path. Although browser support may not be 100%, you can always provide a fallback solution.

.container {
    background: green;
    clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}

Answer №5

By adding files using PHP include or including via your preferred CMS, you can easily insert SVG code into your webpage without cluttering the markup. This method is just as effective as directly embedding the SVG source.

An added advantage is being able to style different parts of your SVG with CSS for hover effects, eliminating the need for any JavaScript.

To see an example, check out this link: http://codepen.io/chriscoyier/pen/evcBu

To apply a hover effect, simply use a CSS rule like the following:

#pathidorclass:hover { fill: #303 !important; }

Remember to include the !important declaration to ensure the fill color override takes effect.

Answer №6

TL/DR: Visit this link for a quick solution -> https://codepen.io/sosuke/pen/Pjoqqp

Here's the breakdown:

If you have an html structure like this:

<img src="/img/source.svg" class="myClass">

It's best to go with the filter approach, especially if your SVG is black or white. You can apply a filter to change its color to whatever you desire. For instance, if you have a black SVG and want it to be mint green, you can first invert it to white (which encompasses all RGB colors at full intensity) and then adjust the hue, saturation, etc. Here's an example of how you can do it:

filter: invert(86%) sepia(21%) saturate(761%) hue-rotate(92deg) brightness(99%) contrast(107%);

An even easier option is to use a tool that converts the hex code of your desired color into a suitable filter for you. Check out this tool here: https://codepen.io/sosuke/pen/Pjoqqp

Answer №7

@John Doe provided an excellent solution for resolving the issue at hand. The code is functioning correctly, but there may be excessive reliance on jQuery for users of AngularJS. As a result, it seems beneficial to offer an alternative code snippet tailored specifically for AngularJS users, inspired by @John Doe's solution.

Implementation in AngularJS

1. HTML: Include the following tag in your HTML file:

<svg-image src="/icons/my.svg" class="any-custom-class"></svg-image>

2. Directive: Define a directive to identify and handle the above tag:

'use strict';
angular.module('myApp')
  .directive('svgImage', ['$http', function($http) {
    return {
      restrict: 'E',
      link: function(scope, element) {
        var imgURL = element.attr('src');
        
        var request = $http.get(
          imgURL,
          {'Content-Type': 'application/xml'}
        );

        scope.manipulateImgNode = function(data, elem){
          var $svg = angular.element(data)[4];
          var imgClass = elem.attr('class');
          
          if(typeof(imgClass) !== 'undefined') {
            var classes = imgClass.split(' ');
            for(var i = 0; i < classes.length; ++i){
              $svg.classList.add(classes[i]);
            }
          }
          $svg.removeAttribute('xmlns:a');
          return $svg;
        };

        request.success(function(data){
          element.replaceWith(scope.manipulateImgNode(data, element));
        });
      }
    };
  }]);

3. CSS:

.any-custom-class{
    border: 1px solid red;
    height: 300px;
    width:  120px
}

4. Unit Testing with Karma-Jasmine:

'use strict';

describe('Directive: svgImage', function() {

  var $rootScope, $compile, element, scope, $httpBackend, apiUrl, data;

  beforeEach(function() {
    module('myApp');

    inject(function($injector) {
      $rootScope = $injector.get('$rootScope');
      $compile = $injector.get('$compile');
      $httpBackend = $injector.get('$httpBackend');
      apiUrl = $injector.get('apiUrl');
    });

    scope = $rootScope.$new();
    element = angular.element('<svg-image src="/icons/icon-man.svg" class="svg"></svg-image>');
    element = $compile(element)(scope);

    spyOn(scope, 'manipulateImgNode').andCallThrough();
    $httpBackend.whenGET(apiUrl + 'me').respond(200, {});

    data = '<?xml version="1.0" encoding="utf-8"?>' +
      '<!-- Generator: Adobe Illustrator 17.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->' +
      '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' +
      '<!-- Obj -->' +
      '<!-- Obj -->' +
      '<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"' +
      'width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve">' +
        '<g>' +
          '<path fill="#F4A902" d=""/>' +
          '<path fill="#F4A902" d=""/>' +
        '</g>' +
      '</svg>';
    $httpBackend.expectGET('/icons/icon-man.svg').respond(200, data);
  });

  afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
  });

  it('should call manipulateImgNode at least once', function () {
    $httpBackend.flush();
    expect(scope.manipulateImgNode.callCount).toBe(1);
  });

  it('should return correct result', function () {
    $httpBackend.flush();
    var result = scope.manipulateImgNode(data, element);
    expect(result).toBeDefined();
  });

  it('should define classes', function () {
    $httpBackend.flush();
    var result = scope.manipulateImgNode(data, element);
    var classList = ["svg"];
    expect(result.classList[0]).toBe(classList[0]);
  });
});

Answer №8

While I understand your preference for using CSS to achieve this, it's worth noting that for smaller, simpler images, you can always open them in Notepad++ and adjust the path or fill attribute:

<path style="fill:#050102;" d="M694.228,309.890c13.897-18.326,28.750-35.245,21.103-57.803
    ...
    C712.105,342.689,702.466,323.754,694.228,309.890z"/>

This approach could potentially save a lot of unnecessary scripting. It might seem overly simplistic, but sometimes the most straightforward solutions are overlooked.

In fact, replacing multiple SVG images might result in a smaller file size compared to some elaborate code snippets provided for similar queries.

Answer №9

To address this issue within AngularJS, I created a custom directive. You can find the solution here - ngReusableSvg.

This directive swaps out the SVG element once it's rendered, placing it within a div element to allow for easy CSS customization. This enables using the same SVG file in multiple locations with different sizes and colors.

Using the directive is straightforward:

<object oa-reusable-svg
        data="my_icon.svg"
        type="image/svg+xml"
        class="svg-class"
        height="30"  // added to prevent UI issues during switching
        width="30">
</object>

With this setup, you can easily apply styles like:

.svg-class svg {
    fill: red; // choose your desired color
}

Answer №10

Check out this implementation of knockout.js that is based on the accepted solution:

Note: Please keep in mind that this code does rely on jQuery for the replacement process, although it could be beneficial for some users.

ko.bindingHandlers.svgConvert = {
    'init': function () {
        return { 'controlsDescendantBindings': true };
    },

    'update': function (element, valueAccessor, allBindings, viewModel, bindingContext) {
        var $img = $(element);
        var imgID = $img.attr('id');
        var imgClass = $img.attr('class');
        var imgURL = $img.attr('src');

        $.get(imgURL, function (data) {
            // Extract the SVG tag from the data
            var $svg = $(data).find('svg');

            // Add the original image's ID to the new SVG
            if (typeof imgID !== 'undefined') {
                $svg = $svg.attr('id', imgID);
            }
            // Add the original image's classes to the new SVG
            if (typeof imgClass !== 'undefined') {
                $svg = $svg.attr('class', imgClass + ' replaced-svg');
            }

            // Remove any invalid XML tags as suggested by http://validator.w3.org
            $svg = $svg.removeAttr('xmlns:a');

            // Replace the image with the new SVG
            $img.replaceWith($svg);

        }, 'xml');

    }
};

To use this, simply add data-bind="svgConvert: true" to your img tag.

This method completely swaps out the img tag with an SVG, meaning any additional bindings will not apply.

Answer №11

Discover SVGInject, an open-source library that utilizes the onload attribute to trigger injection. The GitHub project can be found at https://github.com/iconfu/svg-inject

Check out this simple example using SVGInject:

<html>
  <head>
    <script src="svg-inject.min.js"></script>
  </head>
  <body>
    <img src="image.svg" onload="SVGInject(this)" />
  </body>
</html>

Once the image is loaded, the onload="SVGInject(this) function will initiate injection where the <img> element will be replaced by the content of the SVG file specified in the src attribute.

This method addresses various issues related to SVG injection including:

  1. Ability to hide SVGs until injection completion to prevent initial styling causing a brief flash of unstyled content.

  2. Automatic self-injection for <img> elements, eliminating the need to manually call the injection function when adding SVGs dynamically.

  3. Inclusion of a unique string to each ID in the SVG to avoid duplication in the document if the same SVG is injected multiple times.

Utilizing plain Javascript, SVGInject is compatible with all SVG-supporting browsers.

Note: I am one of the co-authors of SVGInject

Answer №12

Check out this code snippet without using any frameworks, just pure JavaScript:

document.querySelectorAll('img.svg').forEach(function(element) {
            var imgID = element.getAttribute('id')
            var imgClass = element.getAttribute('class')
            var imgURL = element.getAttribute('src')

            xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function() {
                if(xhr.readyState == 4 && xhr.status == 200) {
                    var svg = xhr.responseXML.getElementsByTagName('svg')[0];

                    if(imgID != null) {
                         svg.setAttribute('id', imgID);
                    }

                    if(imgClass != null) {
                         svg.setAttribute('class', imgClass + ' replaced-svg');
                    }

                    svg.removeAttribute('xmlns:a')

                    if(!svg.hasAttribute('viewBox') && svg.hasAttribute('height') && svg.hasAttribute('width')) {
                        svg.setAttribute('viewBox', '0 0 ' + svg.getAttribute('height') + ' ' + svg.getAttribute('width'))
                    }
                    element.parentElement.replaceChild(svg, element)
                }
            }
            xhr.open('GET', imgURL, true)
            xhr.send(null)
        })

Answer №13

In the case where we have a large number of svg images, using font-files can be beneficial as well.
We can utilize websites such as to generate a font file from our svgs.


For example:

@font-face {
    font-family: 'iconFont';
    src: url('iconFont.eot');
}
#target{
    color: white;
    font-size:96px;
    font-family:iconFont;
}

Answer №14

To work with SVG, all you need is the content since it's essentially code. I personally used PHP to retrieve the content, but feel free to use any other method.

<?php
$content    = file_get_contents($pathToSVG);
?>

After obtaining the content, I displayed it as it is within a div container.

<div class="fill-class"><?php echo $content;?></div>

Next, I applied styles to the SVG elements inside the container using CSS.

.fill-class > svg {
    fill: orange;
}

This approach worked well when dealing with a material icon SVG:

  1. Mozilla Firefox 59.0.2 (64-bit) Linux

https://i.stack.imgur.com/hny3K.jpg

  1. Google Chrome66.0.3359.181 (Official Build) (64 bits) Linux

https://i.stack.imgur.com/shBNk.jpg

  1. Opera 53.0.2907.37 Linux

https://i.stack.imgur.com/bcnwH.jpg

Answer №15

Utilize the data-image attribute to access SVG inline by providing a data-URI.

Implement a rollover effect using CSS and SVG exclusively.

This method may seem a bit disorganized, but it gets the job done.

 .action-btn {
    background-size: 20px 20px;
    background-position: center center;
    background-repeat: no-repeat;
    border-width: 1px;
    border-style: solid;
    border-radius: 30px;
    height: 40px;
    width: 60px;
    display: inline-block;
 }

.delete {
     background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg version='1.1' id='Capa_1' fill='#FB404B' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='482.428px' height='482.429px' viewBox='0 0 482.428 482.429' style='enable-background:new 0 0 482.428 482.429;' xml:space='preserve'%3e%3cg%3e%3<g%3e%3cpath d='M381.163,57.799h-75.094C302.323,25.316,274.686,0,241.214,0c-33.471,0-61.104,25.315-64.85,57.799h-75.098 c-30.39,0-55.111,24.728-55.111,55.117v2.828c0,23.223,14.46,43.1,34.83,51.199v260.369c0,30.39,24.724,55.117,55.112,55.117 h210.236c30.389,0,55.111-24.729,55.111-55.117V166.944c20.369-8.1,34.83-27.977,34.83-51.199v-2.828 C436.274,82.527,411.551,57.799,381.163,57.799z M241.214,26.139c19.037,0,34.927,13.645,38.443,31.66h-76.879 C206.293,39.783,222.184,26.139,241.214,26.139z M375.305,427.312c0,15.978-13,28.979-28.973,28.979H136.096 c-15.973,0-28.973-13.002-28.973-28.979V170.861h268.182V427.312z M410.135,115.744c0,15.978-13,28.979-28.973,28.979H101.266 c-15.973,0-28.973-13.001-28.973-28.979v-2.828c0-15.978,13-28.979,28.973-28.979h279.897c15.973,0,28.973,13.001,28.973,28.979 V115.744z'/%3e%3cpath d='M171.144,422.863c7.218,0,13.069-5.853,13.069-13.068V262.641c0-7.216-5.852-13.07-13.069-13.07 c-7.217,0-13.069,5.854-13.069,13.07v147.154C158.074,417.012,163.926,422.863,171.144,422.863z'/%3e%3cpath d='M241.214,422.863c7.218,0,13.07-5.853,13.07-13.068V262.641c0-7.216-5.854-13.07-13.07-13.07 c-7.217,0-13.069,5.854-13.069,13.07v147.154C228.145,417.012,233.996,422.863,241.214,422.863z'/%3e%3cpath d='M311.284,422.863c7.217,0,13.068-5.853,13.068-13.068V262.641c0-7.216-5.852-13.07-13.068-13.07 c-7.219,0-13.07,5.854-13.07,13.07v147.154C298.213,417.012,304.067,422.863,311.284,422.863z'/%3e%3c/g%3e</g>%3c/svg%3e ");
     border-color:#FB404B;
     
 }
 
 .delete:hover {
     background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg version='1.1' id='Capa_1' fill='#fff' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='482.428px' height='482.429px' viewBox='0 0 482.428 482.429' style='enable-background:new 0 0 482.428 482.429;' xml:space='preserve'%3e%3cg%3e%3<g%3e%3cpath d='M381.163,57.799h-75.094C302.323,25.316,274.686,0,241.214,0c-33.471,0-61.104,25.315-64.85,57.799h-75.098 c-30.39,0-55.111,24.728-55.111,55.117v2.828c0,23.223,14.46,43.1,34.83,51.199v260.369c0,30.39,24.724,55.117,55.112,55.117 h210.236c30.389,0,55.111-24.729,55.111-55.117V166.944c20.369-8.1,34.83-27.977,34.83-51.199v-2.828 C436.274,82.527,411.551,57.799,381.163,57.799z M241.214,26.139c19.037,0,34.927,13.645,38.443,31.66h-76.879 C206.293,39.783,222.184,26.139,241.214,26.139z M375.305,427.312c0,15.978-13,28.979-28.973,28.979H136.096 c-15.973,0-28.973-13.002-28.973-28.979V170.861h268.182V427.312z M410.135,115.744c0,15.978-13,28.979-28.973,28.979H101.266 c-15.973,0-28.973-13.001-28.973-28.979v-2.828c0-15.978,13-28.979,28.973-28.979h279.897c15.973,0,28.973,13.001,28.973,28.979 V115.744z'/%3e%3cpath d='M171.144,422.863c7.218,0,13.069-5.853,13.069-13.068V262.641c0-7.216-5.852-13.07-13.069-13.07 c-7.217,0-13.069,5.854-13.069,13.07v147.154C158.074,417.012,163.926,422.863,171.144,422.863z'/%3e%3cpath d='M241.214,422.863c7.218,0,13.07-5.853,13.07-13.068V262.641c0-7.216-5.854-13.07-13.07-13.07 c-7.217,0-13.069,5.854-13.069,13.07v147.154C228.145,417.012,233.996,422.863,241.214,422.863z'/%3e%3cpath d='M311.284,422.863c7.217,0,13.068-5.853,13.068-13.068V262.641c0-7.216-5.852-13.07-13.068-13.07 c-7.219,0-13.07,5.854-13.07,13.07v147.154C298.213,417.012,304.067,422.863,311.284,422.863z'/%3e%3c/g%3e</g>%3c/svg%3e ");        
     background-color: #FB404B;
    }
<a class="action-btn delete">&nbsp;</a>

Generate a data URL for your SVG image conversion here

  1. https://codepen.io/elliz/full/ygvgay

Answer №16

If you have a large DOM and need to update SVG elements dynamically without rescanning the entire document, consider using a jQuery plugin like the one below:

/**
 * A custom jQuery plugin that loads an SVG file and replaces the targeted element with its contents.
 *
 * The path to the SVG file is specified in the src attribute of the element.
 * Width, height, and class attributes from the loaded SVG will replace those of the target element.
 */
(function ($) {
    $.fn.svgLoader = function () {
        var src = $(this).attr("src");
        var width = this.attr("width");
        var height = this.attr("height");
        var cls = this.attr("class");
        var ctx = $(this);

        // Load the SVG file and replace the current element with its contents
        $.ajax({
            url: src,
            cache: false
        }).done(function (html) {
            let svg = $(html);
            svg.attr("width", width);
            svg.attr("height", height);
            svg.attr("class", cls);
            var newHtml = $('<a></a>').append(svg.clone()).html();
            ctx.replaceWith(newHtml);
        });

        return this;
    };

}(jQuery));

To use the plugin, define an SVG element in your HTML like so:

<svg src="images/someSvgFile.svg" height="45" width="45" class="mySVGClass"/>

Then apply the plugin to elements with a specific class:

$(".mySVGClass").svgLoader();

Answer №17

One way to handle animations for the :hover event is by keeping the styles within the SVG file, such as:

<svg xmlns="http://www.w3.org/2000/svg">
<defs>
  <style>
  rect {
    fill:rgb(165,225,75);
    stroke:none;
    transition: 550ms ease-in-out;
    transform-origin:125px 125px;
  }
  rect:hover {
    fill:rgb(75,165,225);
    transform:rotate(360deg);
  }
  </style>
</defs>
  <rect x='50' y='50' width='150' height='150'/>
</svg>

You can view the SVG animation on svgshare here.

Answer №18

Since 2015, there has been a simple way to achieve this using plain JavaScript with the JavaScript Fetch API

"Gone are the days when XMLHttpRequest was relied upon for making API requests. With jQuery, one could use the more streamlined syntax of jQuery.ajax(). However, JavaScript now boasts its own built-in mechanism for making API requests." ~ Digital Ocean

  1. HTML: Apply a class of "svg" to all <img> tags that source SVG files.
<img class="svg" src="/images/logo-facebook.svg"/>
  1. JavaScript: Include this code either in a separate file or inline at the end of the <body> or within the <head>.
window.onload = () => {
    // Query all <img> elements with the class "svg"
    const imgSVGs = document.querySelectorAll("img.svg");

    // Iterate over each found element
    imgSVGs.forEach((img) => {

        // Retrieve the SVG file
        fetch(img.getAttribute("src"))
            .then(response => response.text())
            .then(svgContent => {
                // Parse SVG file's XML data into a DOM object for easier handling
                const parser = new DOMParser();
                const svgXML = parser.parseFromString(svgContent, 'text/xml');

                // Generate a new <svg> element
                const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");

                // Transfer attributes from <img> to <svg>: class, alt, id
                // Eliminate the temporary class "svg"
                svg.setAttribute("class", img.getAttribute("class"));
                svg.classList.remove("svg");
                if (img.getAttribute("id"))
                    svg.setAttribute("id", img.getAttribute("id"));
                if (img.getAttribute("alt"))
                    svg.setAttribute("alt", img.getAttribute("alt"));

                // Copy content from SVG file to <svg>: viewBox attribute, SVG path
                svg.setAttribute("viewBox", svgXML.getElementsByTagName("svg")[0].getAttribute("viewBox"));
                const path = svgXML.getElementsByTagName("svg")[0].querySelector("path");
                svg.appendChild(path);

                // Replace <img> with <svg>
                img.replaceWith(svg);
            })
            .catch(error => { console.log("Error fetching SVG: ", error); });

    });
};

This concise script converts all <img> elements marked with the svg class into actual <svg> components, which can then be designed using CSS:

svg:hover {
    fill: red;
}

It transfers the attributes id, alt, and class from <img> to <svg> (if they exist), and extracts the <path> element along with the viewBox attribute from the original SVG file.

✔️ Delivers the same functionality as the approved jQuery code, solely utilizing pure JavaScript without any frameworks.

Answer №19

.carousel-control-prev-icon {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='rgb(135,206,235)' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e");
}

change color : fill='rgb(135,206,235)'

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

The JSON response appears to be jumbled when making a jQuery Ajax request

My PHP script contains the following line: echo json_encode(array('success'=>'true','userid'=>$userid, 'data' => $array)); The output is as follows: { "success": "true", "userid": "1", "data": [ { ...

Issue when activating Materialize Bootstrap

I'm facing an issue with my code. I have implemented a feature where a modal should be triggered after a user successfully adds a new user. However, I am using Materialize and the modal is not being triggered. Below is a snippet of my code: <div i ...

Conditionals causing issue with React Button disabled property functionality

Having a DIV with two child elements, namely buttons that should be disabled under certain conditions. Despite correct conditions being applied, the buttons remain enabled which is causing confusion. The code snippet in question is as below : < di ...

Creating a tiny arrow using CSS

Can anyone advise on the best way to create a small arrow design using CSS? I have tried using a regular closing > arrow, but it's not quite what I'm looking for. https://i.stack.imgur.com/7uLMG.png ...

Tips on adjusting a JavaScript animation function

I'm currently working on adjusting the animation function within the "popup" class that controls the gallery section of a website. When the page loads, an image and background start expanding from zero scale in the center to 100 VIEWPORT HEIGHT (VH) a ...

I'm having trouble getting the font to display properly on Safari (mac) similar to how it appears on

Recently, I completed a website for a client at . The owner requested the footer text to be styled similarly to the footer text on . However, upon review, it seems that the font in the lists on desiringgod appears thinner compared to the site I built. Thi ...

AJAX request receives a Flask 404 response

I'm currently facing a challenge in getting my Flask application to properly handle a straightforward AJAX request. The issue seems to be related to sending the request to the correct address. Within my app.py file, I've set up a basic route and ...

Exploring the power of jQuery and Ajax together

Today seems to be one of those days where even the simplest tasks become a challenge. I'm sorry if this question has been asked before, but I'm struggling with a basic issue. I want to dynamically update text on a website using a text file, and w ...

Fixed Sidebar Position Changes when Bootstrap Modal Closes

We are facing a peculiar issue in our React application involving a sidebar and a bootstrap modal. The problem arises when we click the "X" button in the modal, causing the sidebar to momentarily drop down the page before the modal closes. This strange be ...

Displayed None instead of sending an HttpResponse

I'm encountering an error when attempting to submit a form using AJAX in combination with class-based views. Here's the template: <form method="post" id="guestform"> {% csrf_token %} {% load i18n %} ...

Utilizing Jquery and Ajax to create a dynamic dropdown selection feature based on dependencies from a JSON file

Could you assist with the code snippet below? I have a form where each dropdown list depends on the selection above it. Based on the user's selection, the appropriate data should display in the subsequent dropdown list. I am trying to make dropdown l ...

Display the background image beyond the boundaries of the div element

I am facing an issue with an image background on a website. The background consists of leaves spreading out to the middle of the page, creating a height of 600px. However, I need to divide this image into three sections: header, content, and footer. Curren ...

Issues arise with AJAX post

I have run into an issue with my jQuery and Codeigniter setup while trying to update the database via AJAX. Despite having the following code in place, nothing seems to be happening or getting sent to my controller when the button is clicked... Snippet of ...

What is the method for specifying the HTML file extension within Visual Studio?

I am having issues with my project recognizing the CSS and other files in the HTML files I have created, even though I have double-checked the extension paths. How can I resolve this problem? https://i.stack.imgur.com/85ooE.png https://i.stack.imgur.com/F ...

choose to display on mobile devices as a dropdown menu rather than a modal window

On mobile devices, my select elements are currently displayed as modals with options. However, I need to change this appearance to resemble the dropdown list view observed on desktop devices. Your assistance is greatly appreciated. ...

using laravel to make ajax requests with dynamic parameters

I'm facing an issue with an ajax call. The specific url I am trying to retrieve data from is: localhost/public/getCode?course_id=1&task_id=1 This is the ajax call I have set up: function getCode() { $.ajax({ ...

Calculate the total number of table rows added using jQuery

I am seeking help to identify the error in my code. My goal is to count the number of table rows added by the end user and display an alert box if the row count is not equal to 2. Below is my HTML code: <table width="100%" border="0" cellspacing="0" c ...

Tips for automatically adjusting the height of the footer

Is there a way to dynamically adjust the height of the footer based on the content length inside the body? I've been exploring solutions like setting the position of the footer as "fixed," but it seems to stay at the bottom of the screen, which is no ...

The submitHandler constantly refreshes the webpage

Struggling to send a form to a PHP file upon submission and retrieving the value without having to reload the page. It was working smoothly for some time but now it just won't cooperate. I have multiple pages with similar functionality, where 2 work p ...

Is there any method to determine whether a floated element has been pushed down?

Imagine a dynamic menu with floating elements, each set at a width of 150px. As the menu's width decreases, the elements progressively move to the next row. You are contemplating how to detect when an element has been moved down. One approach could b ...