简体   繁体   English

为什么SVG图片的CSS大小难以调整(已通过Chrome,Firefox的测试)

[英]Why SVG image gets ugly resize with CSS (Chrome, Firefox tested)

I can reproduce this issue in both Chrome and Firefox. 我可以在Chrome和Firefox中重现此问题。

This is SVG image in question: https://www.iconfinder.com/icons/284101/editor_hambuger_list_menu_view_icon#size=512 这是有问题的SVG图片: https//www.iconfinder.com/icons/284101/editor_hambuger_list_menu_view_icon#size=512

And this is the minimum code which reproduces the problem: 这是重现该问题的最少代码:

<img 
style="width: 15px; vertical-align: middle;"
src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjxzdmcgaGVpZ2h0PSIxMnB4IiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCAyMiAxMiIgd2lkdGg9IjIycHgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6c2tldGNoPSJodHRwOi8vd3d3LmJvaGVtaWFuY29kaW5nLmNvbS9za2V0Y2gvbnMiIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48dGl0bGUvPjxkZWZzPjxwYXRoIGQ9Ik0wLjUwMDgzNjEzNCwwIEMwLjIyNDIzMTk3NSwwIDAsMC4yMTQwMzUwMzQgMCwwLjUwNDY4NDQ0OCBMMCwxLjQ5NTMxNTU1IEMwLDEuNzc0MDQ1MDggMC4yMTc3NDE3MjgsMiAwLjUwMDgzNjEzNCwyIEwyMS40OTkxNjM5LDIgQzIxLjc3NTc2OCwyIDIyLDEuNzg1OTY0OTcgMjIsMS40OTUzMTU1NSBMMjIsMC41MDQ2ODQ0NDggQzIyLDAuMjI1OTU0OTI0IDIxLjc4MjI1ODMsMCAyMS40OTkxNjM5LDAgTDAuNTAwODM2MTM0LDAgWiBNMC41MDA4MzYxMzQsNSBDMC4yMjQyMzE5NzUsNSAwLDUuMjE0MDM1MDMgMCw1LjUwNDY4NDQ1IEwwLDYuNDk1MzE1NTUgQzAsNi43NzQwNDUwOCAwLjIxNzc0MTcyOCw3IDAuNTAwODM2MTM0LDcgTDIxLjQ5OTE2MzksNyBDMjEuNzc1NzY4LDcgMjIsNi43ODU5NjQ5NyAyMiw2LjQ5NTMxNTU1IEwyMiw1LjUwNDY4NDQ1IEMyMiw1LjIyNTk1NDkyIDIxLjc4MjI1ODMsNSAyMS40OTkxNjM5LDUgTDAuNTAwODM2MTM0LDUgWiBNMC41MDA4MzYxMzQsMTAgQzAuMjI0MjMxOTc1LDEwIDAsMTAuMjE0MDM1IDAsMTAuNTA0Njg0NCBMMCwxMS40OTUzMTU2IEMwLDExLjc3NDA0NTEgMC4yMTc3NDE3MjgsMTIgMC41MDA4MzYxMzQsMTIgTDIxLjQ5OTE2MzksMTIgQzIxLjc3NTc2OCwxMiAyMiwxMS43ODU5NjUgMjIsMTEuNDk1MzE1NiBMMjIsMTAuNTA0Njg0NCBDMjIsMTAuMjI1OTU0OSAyMS43ODIyNTgzLDEwIDIxLjQ5OTE2MzksMTAgTDAuNTAwODM2MTM0LDEwIFoiIGlkPSJwYXRoLTEiLz48L2RlZnM+PGcgZmlsbD0ibm9uZSIgZmlsbC1ydWxlPSJldmVub2RkIiBpZD0ibWl1IiBzdHJva2U9Im5vbmUiIHN0cm9rZS13aWR0aD0iMSI+PGcgaWQ9ImVkaXRvcl9saXN0X3ZpZXdfaGFtYnVnZXJfbWVudV9nbHlwaCI+PHVzZSBmaWxsPSIjMDAwMDAwIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiIHhsaW5rOmhyZWY9IiNwYXRoLTEiLz48dXNlIGZpbGw9Im5vbmUiIHhsaW5rOmhyZWY9IiNwYXRoLTEiLz48L2c+PC9nPjwvc3ZnPg==" /> Menu

You can play with it and see it in action at: http://jsfiddle.net/adamovic/s3dZ2/1/ 您可以使用它并在以下位置查看它的运行情况: http : //jsfiddle.net/adamovic/s3dZ2/1/

Anyone has an idea why this Scalable Vector Graphics gets resize where lines becomes unproportional and also any idea how to fix this good? 任何人都知道为什么可缩放矢量图形会在行变得不成比例的情况下调整大小,并且也知道如何解决此问题?

BTW, in production I'm resizing this image to 1EM, to appear next to text "Menu" but to reproduce this issue it is the simplest way. 顺便说一句,在生产中,我正在将该图像调整为1EM,以显示在文本“菜单”旁边,但是要重现此问题,这是最简单的方法。

UPDATE: Updated example from 11.5px to 15px, reproducing same issue. 更新:示例从11.5px更新为15px,重现相同的问题。

In production I'm using width: 1EM; 在生产中,我使用width:1EM; or something like that for responsive design, any idea how to responsive scale this image so that lines are proportional? 或类似的响应式设计,有什么主意如何响应缩放此图像以使线条成比例?

Maybe some fix like min-width and max-width could work, but I couldn't make it work ever with some mozilla image specific commands. 也许最小宽度和最大宽度之类的修复程序可以工作,但是我无法使其与某些mozilla图像特定命令一起工作。

At 11.5 pixels in with the height of the image should be 6.3 pixels. 在11.5像素处,图像的高度应为6.3像素。 Of those, 1.1 pixels should be the height of each black line, and 1.5 pixels the height of each white line. 其中,每条黑线的高度应为1.1像素,每条白线的高度应为1.5像素。 And on top of that the browser resizes the picture to 6 pixels height. 最重要的是,浏览器将图片调整为6像素高。

If the image had 1 pixels for each line (both white and black) and the size multiple of 5, it would look great. 如果图像每行有1个像素(白色和黑色)并且尺寸为5的倍数,则看起来不错。

Later edit In the given picture a black line is 16.67% of image size(all lines reprezent 50%) and a white line is 25%. 后期编辑在给定的图片中,黑线占图像尺寸的16.67%(所有线代表50%),白线占25%。 So... for a height of 8 pixels the a black line has a height of 1.3 pixels and aa white line 2 pixels. 因此...对于8像素的高度,黑线的高度为1.3像素,而白线的高度为2像素。 On paper the smallest image that looks good and unaltered has 2 pixels for black line and 3 for the white line, meaning an image with 12 pixels height. 在纸上,看起来良好且不变的最小图像的黑线为2像素,白线为3,这意味着图像高度为12像素。

Basically if one pixel has to share both white and black lines the browser will create a shade of gray that is the average of the two as it can only display one color . 基本上,如果一个像素必须同时共享白线和黑线,则浏览器将创建灰色阴影,该阴影是两者的平均值,因为它只能显示一种颜色

EX : a pixel has to show 0.67 white(#FFFFFF or 255,255,255) and 0.33(#000000 or 0,0,0) black: 0.67*255 + 0.33*0 = 170.85 (aproximatly 171) so the color displayed is (#ABABAB or 171,171,171) EX :像素必须显示0.67白色(#FFFFFF或255,255,255)和0.33(#000000或0,0,0)黑色:0.67 * 255 + 0.33 * 0 = 170.85(大约171),因此显示的颜色是(#ABABAB或171,171,171)

Theoretically, SVG image is infinitely scalable. 从理论上讲,SVG图像可以无限扩展。 In practice however, the screen has limited resolutions, so if you scale an SVG too small, it won't look very good due to pixelations. 但是实际上,屏幕的分辨率有限,因此,如果将SVG缩放得太小,由于像素问题,它的外观将不会很好。

To avoid this problem, you need to set a minimum size for the icon at the point where it still looks good. 为避免此问题,您需要在图标看起来还不错的地方设置一个最小尺寸。 High quality small-sized vector iconsets would usually be designed to have in such a way that their major lines lie in a grid of integer proportion for many different sizes, so that they will look crisp on different sizes; 通常,高质量小尺寸矢量图标集的设计方式是,它们的主线位于许多不同大小的整数比例的网格中,因此它们在不同大小上看起来会很清晰。 some icon designers might also provide a separately-drawn raster icons for low resolutions. 一些图标设计人员可能还会提供单独绘制的栅格图标以降低分辨率。

Inferring from the size declaration in the SVG, the icon you linked seems to have been designed for 22x12 or multiples of it. 根据SVG中的尺寸声明推断,您链接的图标似乎是为22x12或倍数设计的。

On small sizes, you probably should also use media-queries so that small icons are scaled in a step-ladder of sizes with sizes that are known to look good rather than strictly depending on the viewport size. 在小尺寸上,您可能还应该使用媒体查询,以便将小图标按梯级缩放,以使其看起来不错,而不是严格取决于视口大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM