CSS SVG elements and their compatibility with the "non-scaling-stroke" vector effect in different web browsers

After researching from various sources, I have found that the vector-effect="non-scaling-stroke" attribute in SVG should work in the latest versions of Opera, Firefox, and Chrome (though unsure about IE10).

However, despite this information, I have only been able to get it to work in Opera and Firefox. Interestingly, it works when directly embedded as an image but fails when used as a background image in CSS.

Some questions arise:

Why does it not work in Chrome?

Why is it ineffective in background images?

Is there a standardized method to utilize this feature across all modern browsers?

Check out a fiddle example here.

HTML :

<div><img src="http://dl.dropbox.com/u/60476509/close.svg" /></div>
<div><img id="one" src="http://dl.dropbox.com/u/60476509/close.svg" /></div>
<div id="two"></div>
<div id="three"></div>

CSS :

#one {
    width: 200px;
    height: 200px;
}

#two {
    background-image: url("http://dl.dropbox.com/u/60476509/close.svg");
    height: 100px;   /* native size */
    width: 100px;
    background-size: contain;        
}
#three {
    background-image: url("http://dl.dropbox.com/u/60476509/close.svg");
    height: 200px;
    width: 200px;
    background-size: cover;        
}    

Answer №1

Although I may not understand the technical aspects, there is a simpler solution for working with inline SVGs (unsure about backgrounds). Instead of:

<img src="svg-source.svg"/>

You can use:

<object type="image/svg+xml" data="svg-source.svg"></object>

It's also important to have the viewBox defined for the svg document.

As mentioned in this answer, using object is considered best practice. For better cross-browser compatibility, you can check out this helpful blog post:

<object type="image/svg+xml" data="svg-source.svg">
    <img src="png-version.png"/>
</object>

While I haven't tested the latter option, if it proves successful, it could simplify the process compared to this solution.

Update: After encountering issues with "hover" and "click" events due to embedded SVG objects, I opted to fully embed SVGs. To streamline the process, I included the following snippet at the beginning of my javascript file (leveraging JQuery):

$(document).ready(function()
{
    $('.deferred-load').deferredLoad();
});

$.fn.deferredLoad = function()
{
    $(this).each(function()
    {
        $(this).load($(this).attr('data-load'));
    });
};

Now, instead of <img src="svg.svg"/>, I can utilize

<div class="deferred-load" data-load="svg.svg"></div>
.

Note that this method relies on Javascript and may not function without it. Nevertheless, it offers a cleaner alternative to manually inserting lengthy XML code.

Answer №2

When rendering SVG images, browsers need to convert them into bitmaps in order to display them properly, including any background images. The usual method involves reusing code for non-image rendering and then scaling the bitmap accordingly. However, this process often leads to blurred images and issues with scaling strokes. The ideal approach is to determine the final size needed for the image and instruct the SVG drawing code to render it at that size from the beginning.

A recent fix has been implemented in Firefox to address this issue. You can test it out using a Nightly build today or simply wait for the official release scheduled for September.

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

I'm looking for a solution to implement a vertical carousel in Google's Materialize CSS without the need for custom development

Looking to create a unique vertical scrolling "carousel" using Google's Materialize CSS library. I have a good understanding of the steps required to construct a carousel. # haml %ul.carousel %li.carousel-item Some Content %li.carousel-item ...

Having trouble creating space between two aligned elements?

I have created two buttons in the code snippet below with the same width, but I'm struggling to add a gap between them. This issue persists in the mediaquery version for mobile as well. How can I solve this? .flex-container { display: flex; justi ...

Form Rolling Over

I recently updated the navigation bar on our pending site by incorporating styled hyperlinks. Initially, one of these hyperlinks directed to PayPal, but due to security concerns, I had to replace it with an encrypted button instead. The challenge I' ...

Vue's smooth scrolling in Nuxt.js was not defined due to an error with the Window

There seems to be an issue with adding vue smooth scroll to my nuxt.js project as I'm encountering the "window is not defined error". The steps I followed were: yarn add vue2-smooth-scroll Within the vue file, I included: import Vue from 'vue ...

eliminate gaps between hyperlinks using cascading style sheets

I developed a webapp for iOS which has a specific page containing a centered div with 3 buttons. Inside this div, there is a total of 460px with each link measuring as follows: the first and last are 153px while the middle one is 154px. The main issue ar ...

How can I make a CSS transition activate only on width increases, not decreases?

My element's width changes under different conditions, and I've implemented CSS with a transition for the width: .element-class { transition: width 500ms ease 0ms; } However, I only want the transition to occur when the width is decreasing. ...

Creating personalized CSS for Sharepoint online site customization

While looking into supported and unsupported customization options and custom CSS for branding in SharePoint online, I found myself perplexed by the vague and confusing information provided by Microsoft's articles. Specifically, I was studying the ap ...

Eliminate the golden hue from the input field when autofill is activated

Can anyone help me figure out how to get rid of this unsightly chrome background that appears when autofill is enabled? (See below for reference.) I've attempted the following solutions: *:focus { outline: 0; } input:-webkit-autofill { -webk ...

Various lists do not appear directly under each other

Hello everyone, I recently created a webpage displaying different lists of football players. My goal is to keep the lists stacked vertically without any floats that would make them appear side by side. The separate lists are essential for properly categori ...

Set the line height to a minimum to prevent any text from being cropped

Is there a recommended line height standard to prevent text from being cropped at the top or bottom regardless of font style, weight, and size? One might initially consider using 100% or 1, but setting line-height: 100% or line-height: 1 can result in let ...

Simplifying the process of importing SCSS files in Angular 5

After extensive searching, I've come up empty-handed in finding a solution to my project dilemma. Let's say I have various .scss files within my project such as mixins.scss, vars.scss, and base.scss. Naturally, I would like to use these files in ...

Improving the layout and size of text within input fields

How can we adjust the input text positioning to the right so it's not directly against the edge? Is there a way to move 'searching' 5px to the right? ...

CSS is not referred to as animation

Every time we hit a key, the dinosaur receives a jump class that should activate an animation. Unfortunately, the animation does not play. animation not functioning <!DOCTYPE html> <html lang="en"> <head> <meta c ...

Unable to execute multiple backstretch instances

I am having trouble adding two different backgrounds to two different divs or on the body and a div simultaneously. Only one of the two backgrounds is displayed. For example, on this page: The background shows up, but not the #sliders div. P.S. The Java ...

Fizzy beverage with right alignment

Why are the sprites in my #tbr div aligning to the left even though its align is set to right? Any suggestions? Regular text, links, and images seem to be working correctly (aligned to the right with a 20px right margin as intended). HTML: <div id="t ...

Get rid of the rollover effect on the <a> tag when hovering over an image

I have a snippet of code that is fully functional. <div class="user-pic round-pic"> <a href="<?php echo $userLoggedIn; ?>"><img src="<?php echo $user['profile_pic']; ?>"></a> </div> The issue lies in i ...

Is it possible to modify Bootstrap 4 variables using its own variables?

I am currently utilizing the latest Bootstrap 4 beta 2 and attempting to customize it by altering the variables to suit my preferences. Within my styles.scss file, I have included all necessary bootstrap imports: // Custom variables @import 'helpers ...

Save PHP webpage that displays CSS styles

After finding a solution here on SO, I am attempting to create a dynamic stylesheet using PHP. The Issue... I want the stylesheet (named css.php) to be cached in the user's browser to avoid loading it with each page visit. I've set the necessar ...

Tips on accessing the left value of a absolutely positioned object that transitions on click using getComputedStyle or a similar method

Question: How can I ensure that when the target is clicked, the correct and updated value of the left property is returned? For example, I want it to return 120px if --moved is applied, and 100px if it is not applied. Background: Utilizing getComputedStyl ...

Customizing the appearance of all Angular components with styles.scss

Is there a way to create a universal style in styles.scss that can be applied to all Component selectors (e.g. app-componentA, app-componentB ...)? I understand that I could manually add the style to each selector, but I am concerned that it may be forgot ...