ChatGPT解决这个技术问题 Extra ChatGPT

SVG icons vs. PNG icons in modern web sites

I'm wondering why so few modern web sites still use only PNGs for icons (but this assumption is just based my observation). So far I know, main reasons for using PNGs over SVGs are IE8 and that SVG uses more CPU power (but I don't believe this is any issue for simple 1K icons). I can see (and we currently use) many advantages in using SVGs, either when it's used as sprites, as images, or as inline SVG.

(Question Looking for a research: PNG Sprite vs SVG sprite vs Icon fonts focuses on performance and doesn't have relevant answer, Icon Font vs. SVG caching and network concern focuses on network traffic, but it's easily solvable by e.g. templating.)

If new web site supports only modern browsers, is there any reason for not using SVGs (or - is there any reason for using PNGs for icons)? If we don't care about IE8 and the use of SVG is backed up by templating and/or caching, is there any catch to rely only on SVGs?

Dear down-voter, would you please let me know what's specifically opinion-based on this question, where I asked for specific - technical - reasons? I'd appreciate such feedback, so I can improve my further questions.
Robert, it doesn't seem opinion based, but people sometimes go into zombie mode and just skim questions without actually reading them.
I prefer png for simplicity and a cleaner DOM. One point to add to the answers below is that with svg, you can change the color dynamically. If an icon has three colors: regular,active&hover, that's 3 png images but only one svg.
@Robert [ZOMBIE MODE] is excellent man :) :D

J
Jeff Widman

Reasons SVG may be a good choice:

it seamlessly supports browsers of any size, especially with css's background-size

you can scale them up/down will, such as to to a hover effect

you can embed SVGs and do real-time modifications to them with JavaScript and the DOM

you can style SVGs and parts of SVGs with CSS (changing colors, outlines, etc.)

you can generate SVGs dynamically, either on the client or server. Due to their text based nature, you don't need low-level libraries or powerful servers to create them.

Reasons PNG may be a good choice:

browser support

existing tooling for creating PNG spritesheets

most people have a PNG compatible editor on their computer

your graphics are photos or other difficult to vectorize images

Other concerns:

some SVG editors may store metadata in your SVGs, increasing file size and possibly unintentionally exposing data e.g. when you export a PNG in Inkscape it did/does save the absolute path to this directory in the SVG when you save SVG compressors may remove this, but I haven't tested it (feel free to edit if you have)

e.g. when you export a PNG in Inkscape it did/does save the absolute path to this directory in the SVG when you save

SVG compressors may remove this, but I haven't tested it (feel free to edit if you have)


Thanks, our graphic people create SVGs in Illustrator, and then edit them by hand, so its usually with few elements and attributes, so these concerns are mute - my concern was that I overlooked something serious, since I see almost only benefits over classical PNGs - but it probably really boils down to <= IE8 or > IE8. E.g., standard icon with circle with plus sign is even more compact in SVG than in PNG.
Yeah, some people get their SVGs from third parties, and don't open them in a text editor, or ensure they're compressed. Just didn't want to leave that out. There's the same issues with PNGs now that I think about it; often useless data (in the context of web) is stored in them. This data is used by editors, file browsers, and other programs. This is more common with jpegs, though (cameras store brand, model, lens, settings, etc.).
Thanks, so I guess there really isn't any disadvantage for using SVGs in our projects and we'll integrate them even deeper. Thank you for your insights.
The right way to go for vector icons are Icon Fonts. For me the biggest disadvantage with SVG icons is, that the data attribute in CSS doesn't work for IE10/11.
@Gerfried Icon fonts have many accessibility issues though. As soon as a user decides to block Web fonts or override all fonts with their own (for whatever reason it may be), all those pretty icons instantly break. There are fallbacks but I myself have not seen a 100% working solution yet. This is not the case with SVG images.
S
Steven Garcia

Unless you are showing very simple shapes/designs or specifically need to modify parts of the graphic with the app, there is not a whole lot of incentive to use SVG. You can produce a PNG at twice the original size (for retina displays) and still have the filesize be an order of magnitude smaller - not to mention better coverage for legacy browsers (no need for javascript or polyfills).

I say this as someone who builds UIs with vector graphics. It's an awesome design tool, but for delivery/bandwidth/reach it's hard to top PNG. Just last night I tested out a well known logo. CocaCola.svg was almost 20K. Since it was a solid/flat color I exported as PNG-8 with 12-color palette compression and got it down to 1.6K (!!!) For just a few logos it's not a big deal, but when you have to show 40 of them.. well, you get the picture.

The best part is that you can turn a PNG into a base64 data uri and embed them right in your stylesheet. This is not recommended with SVG - a format which is already famous for performance and compatibility issues, especially on mobile browsers.

In closing I have to say there are strengths and use cases for both, but PNG has blazed a lot more trails and you face less resistance when you go with the current. For those odd cases where SVG does make a better fit, I highly recommend this article and this learning resource

Happy Designing!


IMO, this is the best answer.
You should specify which Coca Cola logo you worked with. The current logo from 2007 is very simple and is around 8KB. It's still nothing compared to PNG-8, but much better than 20KB.
At first I thought this answer was dated but then I saw 2018. I also ran a test with a 'well-known' coca cola logo and put the actual results here codepen.io/JHeth/pen/XWeWZpO unless you're showing a ton of tiny thumbnails on your site or you need raster graphics (photos) then it makes far more sense to use SVG, and it did in 2018 as well. As far as being famous for performance and compatibility issues... maybe this chart had more red in 2018 but not much, and I have never heard anyone mention performance issues with properly made SVG images.
T
Tanveer Alam

SVG is cool (how FakeRainBrigand nicely described) and renders beautifully but can be pretty complex. A browser has more work to do when dealing with vector data instead of pixel based images. An image is one element, a SVG may have lots of children that can even be added to the DOM when used inline.

I did not do any valuable performance tests but from a logical point of view SVG should be used carefully when it comes to performance especially when dealing with middle aged mobile browsers (CPU-stress). Would be very useful to have a chart where you can see the CPU power consumed by 100 SVG images vs 100 PNG images on different Android and IOS devices ...

Another bugging thing with SVG is that the Tag needs a width and height attribute for some Android/Samsung whatever browsers and our good old IE. And most modern SVG Editors like A***e Illustrator just add the "viewBox" attribute.

The coolest thing about SVG is that it renders nice and crisp in any pixel density.


J
Joe7

Let's note that performancewise SVG can become horrible. Even on modern browsers, like Chrome (writing this in 2019 !), a CMS-like page with a few 100s (practically 3-800) of svg icons literally hangs the browser for 5+ seconds to finish rendering. Maxing out CPU meanwhile.

As noted elsewhere, the count of SVGs embedded in the page expontentially increases the load on the browser, so if you happen to face such situation

Option #1: convert svgs into webfont (See performance chart here:http://frozeman.de/blog/2013/08/why-is-svg-so-slow/ )

Option #2: fall back to plain old PNGs

In such situations where you ~never wanna do anything fancy like on-the-fly color modification, and you have MANY instances of SVGs, the old PNG does the job and saves the day!


i don't think so, SVG has better performance . would recommend to read this : vecta.io/blog/comparing-svg-and-png-file-sizes
SVG can reduce bandwidth true, but it needs computations via CPU! So Joe7 is true, SVG can consume so many CPU usages for processing it, but in case of downloading images, yes definitely SVG images have lower size.
b
barbaanto

It's true, png is used almost everywhere. I think it's because the SVG, in the most of cases, is pretty useless, the image should be bigger (i think) and the computer have to regenerate the image whenever you zoom it (because you always zoom the images, don't you?) I think this is the most important reason.


SVG's are VERY useful for icons, which are ubiquitous in modern (mobile) websites. They offer scaling and css-styling with no quality penalties, two important features not present with png's.
From the user perspective, it is pointless, but as a multimedia developer, it is great to use only one file that can be used in any screen and will always preserve the same quality.
O
Olivia Welsh

SVG icons are significantly more popular than PNG icons. Why is this the case? The main reason is that they are easier to create and can be processed by most web browsers. The disadvantage is that SVG icons do not have the same level of quality as their PNG counterparts. They also do not support CSS 3D transforms and can cause problems in some apps, such as when used in conjunction with a button. Modern browsers now support Free SVG icons, which can be easily saved on the server and loaded into client-side code.


S
SIMBIOSIS surl

Well, as this is an old question I just will upgrade the fact that in modern browsers; mobile or desktop, in modern devices; phones or PCs, svg is the best option whenever you do not to deal with real fotos, then I choose .webp. I have decreased in many megabytes the size of an app by just using optimized svgs for all images but fotos, and using webp for the last.

Of course, it's 2021 and devices and browsers are much more better in performance and support. So, nowadays it's more convenient to use svgs, and lately it is even a better solution to use the svg icons provided by many icon fonts than using the font itself due to the fact that maybe you loading and processing a whole font from which you are only using 10 to 20 icons.