CodexBloom - Programming Q&A Platform

jQuery .css() not applying styles for SVG elements in Chrome but works in Firefox

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-08-23
jquery svg cross-browser JavaScript

I've encountered a strange issue with I'm relatively new to this, so bear with me. Hey everyone, I'm running into an issue that's driving me crazy. I'm working with an scenario where using jQuery's `.css()` method to apply styles to SVG elements works perfectly in Firefox but fails silently in Chrome. My code looks something like this: ```javascript $(document).ready(function() { $('#mySvg rect').css({ fill: 'red', stroke: 'black', 'stroke-width': 2 }); }); ``` In Firefox, the rectangle within my SVG turns red with a black border as expected. However, in Chrome, the rectangle remains unchanged. I’ve verified that the SVG is correctly referenced and that there are no console errors. When I inspect the element in Chrome, it does not show the new styles being applied. I even tried using plain JavaScript to apply the styles: ```javascript var rect = document.getElementById('myRect'); rect.setAttribute('fill', 'red'); rect.setAttribute('stroke', 'black'); rect.setAttribute('stroke-width', '2'); ``` This approach works fine in both browsers, but I'd prefer to stick with jQuery for consistency across my codebase. I’ve also checked that jQuery is up to date (version 3.6.0) and that there are no conflicting CSS rules. Are there known compatibility issues with jQuery and SVG styling in Chrome? Is there a best practice for ensuring styles are applied correctly across different browsers? Any help would be greatly appreciated! Am I missing something obvious? For context: I'm using Javascript on Windows. Any feedback is welcome!