简体   繁体   English

水平居中 base64 背景图像不起作用

[英]horizontal centering a base64 background-image does not work

I am trying to center that base64 background-image inside an horizontal rectangle div, and it just does not work.我试图将 base64 背景图像居中在水平矩形 div 内,但它不起作用。

I know it should be pretty trivial and I looked into many almost similar questions here, but no trick worked.我知道这应该很简单,我在这里研究了许多几乎相似的问题,但没有任何技巧奏效。 The image is not centered horizontally and i do not know why.图像没有水平居中,我不知道为什么。 In fact, background-position does not seem to take any effect unless I use pixels explicitly.事实上,除非我明确使用像素,否则背景位置似乎没有任何效果。

you can see it in this playground:https://codesandbox.io/s/solitary-worker-vvrx4?file=/index.html你可以在这个操场上看到它:https://codesandbox.io/s/solitary-worker-vvrx4?file=/index.html

.container {
   background: red;
   width: 100px;
   height: 16px;
   background-image:url("data:image/svg+xml;base64,...");
   background-size: 100% 100%;
   background-repeat: no-repeat;
   background-position: center;
}


<div class="container"></div>

Could somebody please help?有人可以帮忙吗? Thank you.谢谢你。

Looks like the problem is with your SVG.看起来问题出在您的 SVG 上。 Made a fell changes on it and generated a new base64 that worked fine.对其进行了更改并生成了一个新的 base64 工作正常。 I'm not so sure why this solved, though.不过,我不太确定为什么会解决这个问题。

What I did:我做了什么:

  • Changed transform="rotate(90 8 8)" to transform="rotate(90)"transform="rotate(90 8 8)"更改为transform="rotate(90)"
  • Added specific width and height properties width="160" height="160"添加了特定的宽度和高度属性width="160" height="160"

Heres the result结果如下

 .icon { background: red; width: 100px; height: 16px; background-size: 100% 100%; background-repeat: no-repeat; background-position: center; }.icon--broken { background-image: url("data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDIxLjAuMiwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5z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4xQzkuOSwxNS4xLDksMTYsNy44LDE2cy0yLjEtMC45LTIuMS0yLjFDNS43LDEyLjcsNi43LDExLjgsNy44LDExLjh6Ii8+CjwvZz4KPGc+Cgk8Y2lyY2xlIGN4PSI3LjgiIGN5PSI3LjkiIHI9IjEuNyIvPgoJPHBhdGggZD0iTTcuOCw1LjhjMS4yLDAsMi4xLDAuOSwyLjEsMi4xQzkuOSw5LjEsOSwxMCw3LjgsMTBTNS43LDkuMSw1LjcsNy45QzUuNyw2LjgsNi43LDUuOCw3LjgsNS44eiIvPgo8L2c+CjxnPgoJPGNpcmNsZSBjeD0iNy44IiBjeT0iMi4xIiByPSIxLjciLz4KCTxwYXRoIGQ9Ik03LjgsMEM5LDAsOS45LDAuOSw5LjksMi4xYzAsMS4yLTAuOSwyLjEtMi4xLDIuMVM1LjcsMy4zLDUuNywyLjFDNS43LDAuOSw2LjcsMCw3LjgsMHoiLz4KPC9nPgo8L3N2Zz4K"); }
 <div class="icon icon--broken"></div> <div class="icon icon--fixed"></div>

You have to increase the width of the container.您必须增加容器的宽度。

You can see it in this playground: https://codesandbox.io/s/admiring-monad-lwqlk?file=/index.html你可以在这个操场上看到它: https://codesandbox.io/s/admiring-monad-lwqlk?file=/index.html

The problem is not CSS but the SVG image inside the base64 string.问题不是 CSS 而是 base64 字符串中的 SVG 图像。 When you open it, you find the following code:打开后发现如下代码:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" viewBox="0 0 16 16" style="enable-background:new 0 0 16 16;" xml:space="preserve" transform="rotate(90 8 8)" fill="#9c9c9c">
<g>
    <circle cx="7.8" cy="13.9" r="1.7"/>
    <path d="M7.8,11.8c1.2,0,2.1,0.9,2.1,2.1C9.9,15.1,9,16,7.8,16s-2.1-0.9-2.1-2.1C5.7,12.7,6.7,11.8,7.8,11.8z"/>
</g>
<g>
    <circle cx="7.8" cy="7.9" r="1.7"/>
    <path d="M7.8,5.8c1.2,0,2.1,0.9,2.1,2.1C9.9,9.1,9,10,7.8,10S5.7,9.1,5.7,7.9C5.7,6.8,6.7,5.8,7.8,5.8z"/>
</g>
<g>
    <circle cx="7.8" cy="2.1" r="1.7"/>
    <path d="M7.8,0C9,0,9.9,0.9,9.9,2.1c0,1.2-0.9,2.1-2.1,2.1S5.7,3.3,5.7,2.1C5.7,0.9,6.7,0,7.8,0z"/>
</g>
</svg>

Notice the transform="rotation(90 8 8)" attribute.注意transform="rotation(90 8 8)"属性。 It rotates the image by 90 degrees around the point [8,8].它将图像围绕点 [8,8] 旋转 90 度。 The point should be the center of the image according to viewBox="0 0 16 16" but for some reason, the dimensions get broken, and an offset of 16 pixels is allocated on the left side of the image.根据viewBox="0 0 16 16" ,该点应该是图像的中心,但由于某种原因,尺寸被破坏,并且在图像的左侧分配了 16 个像素的偏移量。

The simplest fix is to change the value of the transform attribute to rotation(90) .最简单的解决方法是将 transform 属性的值更改为rotation(90) The fixed image in base64 is: base64中的固定图像为:

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2ZXJzaW9uPSIxLjEiIGlkPSJMYXllcl8xIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD0iMCAwIDE2IDE2IiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCAxNiAxNjsiIHhtbDpzcGFjZT0icHJlc2VydmUiIHRyYW5zZm9ybT0icm90YXRlKDkwKSIgZmlsbD0iIzljOWM5YyI+CjxnPgoJPGNpcmNsZSBjeD0iNy44IiBjeT0iMTMuOSIgcj0iMS43Ii8+Cgk8cGF0aCBkPSJNNy44LDExLjhjMS4yLDAsMi4xLDAuOSwyLjEsMi4xQzkuOSwxNS4xLDksMTYsNy44LDE2cy0yLjEtMC45LTIuMS0yLjFDNS43LDEyLjcsNi43LDExLjgsNy44LDExLjh6Ii8+CjwvZz4KPGc+Cgk8Y2lyY2xlIGN4PSI3LjgiIGN5PSI3LjkiIHI9IjEuNyIvPgoJPHBhdGggZD0iTTcuOCw1LjhjMS4yLDAsMi4xLDAuOSwyLjEsMi4xQzkuOSw5LjEsOSwxMCw3LjgsMTBTNS43LDkuMSw1LjcsNy45QzUuNyw2LjgsNi43LDUuOCw3LjgsNS44eiIvPgo8L2c+CjxnPgoJPGNpcmNsZSBjeD0iNy44IiBjeT0iMi4xIiByPSIxLjciLz4KCTxwYXRoIGQ9Ik03LjgsMEM5LDAsOS45LDAuOSw5LjksMi4xYzAsMS4yLTAuOSwyLjEtMi4xLDIuMVM1LjcsMy4zLDUuNywyLjFDNS43LDAuOSw2LjcsMCw3LjgsMHoiLz4KPC9nPgoJCjwvc3ZnPg==

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

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