Bram Vanroy Bram Vanroy - 3 months ago 11
CSS Question

Understanding viewBox: how to use the correct size

Have a look at this SVG. As you can see I provide it as a base64 data uri. The reason for this is that I want to use it as a background image. However, I am experiencing some difficulty getting it to work the way I want to and I fear I am misunderstanding something about the

viewbox
attribute. But let me start by giving an example of what I want to accomplish.



html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
body::after {
content: "";
display: block;
height: 0;
padding: 11%;
width: 76%;
background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0MDYuNTMgNDAuNzgiIGJhc2VQcm9maWxlPSJiYXNpYyIgdmVyc2lvbj0iMS4xIiB5PSIwcHgiIHg9IjBweCIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiPgo8Zz4KCTxwYXRoIGZpbGw9InJnYigxMCwgNTQsIDgzKSIgZD0ibTE5NiAxNS4yYzYuMS00LjYgMTAuNy01IDEwLjctNSAwLjggMC41IDMuMSAxLjYgNS41IDIuMyA3LjggMi40IDEzLjUgMS45IDE0LjQtMC4xIDAuNy0xLjUtMC44LTMuNi0zLjQtNC43LTguNi0zLjgtMjAuNS0wLjctMjAuNS0wLjdzLTQuNC0zLjEtNy40LTQuNGMtMTAuMS00LjUtMjEuNS0wLjItMjYuMSAxMC4yLTEuNCAzLjEtMS44IDYtMS43IDguNGwwLjUtMC4xYzAtMi40IDAuMy01LjEgMS42LTguMSA0LjYtMTAuNSAxNS44LTE0LjIgMjUuNC0xMCAyLjggMS4yIDUuMyAzLjIgNi43IDQuMi04IDMuNC0xMS40IDYuNS0xMy41IDkuN2gyLjIgMTYwLjV2LTEuOGgtMTU0Ljl6bTI3LjgtNmMxLjYgMC43IDIuNSAyLjEgMi4xIDMtMC44IDEuOS04LjQgMS40LTEzLjQtMC4zLTEuOS0wLjYtNS4yLTIuMS01LjItMi4xIDktMi4yIDEzLjctMS44IDE2LjUtMC42em0tNDEuNCA5Yy00LjYgMTAuNS0xNS44IDE0LjItMjUuNCAxMC0yLjgtMS4yLTUuMy0zLjItNi43LTQuMiA4LTMuNCAxMS40LTYuNSAxMy41LTkuN2gtMi4yLTE2MC42djEuOGgxNTUuMWMtNi4xIDQuNi0xMC43IDUtMTAuNyA1LTAuOC0wLjUtMy4xLTEuNi01LjUtMi4zLTcuOC0yLjQtMTMuNS0xLjktMTQuNCAwLjEtMC43IDEuNSAwLjggMy42IDMuNCA0LjcgOC42IDMuOCAyMC41IDAuNyAyMC41IDAuN3M0LjQgMy4xIDcuNCA0LjRjMTAuMSA0LjUgMjEuNSAwLjIgMjYuMS0xMC4yIDEuNC0zLjEgMS44LTYgMS43LTguNGwtMC41IDAuMWMwIDIuMy0wLjQgNS0xLjcgOHptLTU0LjEgMy45Yy0xLjYtMC43LTIuNS0yLjEtMi4xLTMgMC44LTEuOSA4LjQtMS40IDEzLjQgMC4zIDEuOSAwLjYgNS4yIDIuMSA1LjIgMi4xLTkgMi4zLTEzLjcgMS45LTE2LjUgMC42eiIvPgo8L2c+Cjwvc3ZnPgo=");
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
margin: 24px auto;
}





If you look closely you can see that the background image is not centered. (When you open your dev tools and highlight the box of the pseudo element it is very clear.) At first I thought you couldn't center data URIs, but then I noticed that when you hover over the data URI in devtools, it says the dimensions of the "image" are 300x30. Chrome also shows a small preview of the image. It is clear from this that the dimensions are not correct. The 10-to-1 ratio is not correct.

The data URI is generated from the following SVG, in which I specified values for the viewBox attribute. Even though it is close to 10-1, it isn't. So I am thinking something goes wrong because of which the embedded data URI doesn't respect the proposed dimensions.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 406.53 40.78" baseProfile="basic" version="1.1" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink">
<g>
<path fill="rgb(10, 54, 83)" d="m196 15.2c6.1-4.6 10.7-5 10.7-5 0.8 0.5 3.1 1.6 5.5 2.3 7.8 2.4 13.5 1.9 14.4-0.1 0.7-1.5-0.8-3.6-3.4-4.7-8.6-3.8-20.5-0.7-20.5-0.7s-4.4-3.1-7.4-4.4c-10.1-4.5-21.5-0.2-26.1 10.2-1.4 3.1-1.8 6-1.7 8.4l0.5-0.1c0-2.4 0.3-5.1 1.6-8.1 4.6-10.5 15.8-14.2 25.4-10 2.8 1.2 5.3 3.2 6.7 4.2-8 3.4-11.4 6.5-13.5 9.7h2.2 160.5v-1.8h-154.9zm27.8-6c1.6 0.7 2.5 2.1 2.1 3-0.8 1.9-8.4 1.4-13.4-0.3-1.9-0.6-5.2-2.1-5.2-2.1 9-2.2 13.7-1.8 16.5-0.6zm-41.4 9c-4.6 10.5-15.8 14.2-25.4 10-2.8-1.2-5.3-3.2-6.7-4.2 8-3.4 11.4-6.5 13.5-9.7h-2.2-160.6v1.8h155.1c-6.1 4.6-10.7 5-10.7 5-0.8-0.5-3.1-1.6-5.5-2.3-7.8-2.4-13.5-1.9-14.4 0.1-0.7 1.5 0.8 3.6 3.4 4.7 8.6 3.8 20.5 0.7 20.5 0.7s4.4 3.1 7.4 4.4c10.1 4.5 21.5 0.2 26.1-10.2 1.4-3.1 1.8-6 1.7-8.4l-0.5 0.1c0 2.3-0.4 5-1.7 8zm-54.1 3.9c-1.6-0.7-2.5-2.1-2.1-3 0.8-1.9 8.4-1.4 13.4 0.3 1.9 0.6 5.2 2.1 5.2 2.1-9 2.3-13.7 1.9-16.5 0.6z"/>
</g>
</svg>


So I am at loss. Why doesn't the SVG/data URI background respect the ratio/box that is specified in viewBox when it is used as a background? And how can I fix this?

Answer

I believe your original SVN was not in the proportions that you thought it was, so the change in your width-height in the viewbox affected your overall view.

Check here (width/height changed from 406.53 40.78 to 352 31):

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}
body::after {
  content: "";
  display: block;
  height: 0;
  padding: 11% 0;
  width: 100%;
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR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IwLjUgMC43IDIwLjUgMC43czQuNCAzLjEgNy40IDQuNGMxMC4xIDQuNSAyMS41IDAuMiAyNi4xLTEwLjIgMS40LTMuMSAxLjgtNiAxLjctOC40bC0wLjUgMC4xYzAgMi4zLTAuNCA1LTEuNyA4em0tNTQuMSAzLjljLTEuNi0wLjctMi41LTIuMS0yLjEtMyAwLjgtMS45IDguNC0xLjQgMTMuNCAwLjMgMS45IDAuNiA1LjIgMi4xIDUuMiAyLjEtOSAyLjMtMTMuNyAxLjktMTYuNSAwLjZ6Ii8+DQo8L2c+DQo8L3N2Zz4=");
  background-repeat: no-repeat;
  background-position: center center;
}
Original SVG:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 406.53 40.78" baseProfile="basic" version="1.1" y="0px" x="0px" xmlns:xlink="http://www.w3.org/1999/xlink">
<g>
    <path fill="rgb(10, 54, 83)" d="m196 15.2c6.1-4.6 10.7-5 10.7-5 0.8 0.5 3.1 1.6 5.5 2.3 7.8 2.4 13.5 1.9 14.4-0.1 0.7-1.5-0.8-3.6-3.4-4.7-8.6-3.8-20.5-0.7-20.5-0.7s-4.4-3.1-7.4-4.4c-10.1-4.5-21.5-0.2-26.1 10.2-1.4 3.1-1.8 6-1.7 8.4l0.5-0.1c0-2.4 0.3-5.1 1.6-8.1 4.6-10.5 15.8-14.2 25.4-10 2.8 1.2 5.3 3.2 6.7 4.2-8 3.4-11.4 6.5-13.5 9.7h2.2 160.5v-1.8h-154.9zm27.8-6c1.6 0.7 2.5 2.1 2.1 3-0.8 1.9-8.4 1.4-13.4-0.3-1.9-0.6-5.2-2.1-5.2-2.1 9-2.2 13.7-1.8 16.5-0.6zm-41.4 9c-4.6 10.5-15.8 14.2-25.4 10-2.8-1.2-5.3-3.2-6.7-4.2 8-3.4 11.4-6.5 13.5-9.7h-2.2-160.6v1.8h155.1c-6.1 4.6-10.7 5-10.7 5-0.8-0.5-3.1-1.6-5.5-2.3-7.8-2.4-13.5-1.9-14.4 0.1-0.7 1.5 0.8 3.6 3.4 4.7 8.6 3.8 20.5 0.7 20.5 0.7s4.4 3.1 7.4 4.4c10.1 4.5 21.5 0.2 26.1-10.2 1.4-3.1 1.8-6 1.7-8.4l-0.5 0.1c0 2.3-0.4 5-1.7 8zm-54.1 3.9c-1.6-0.7-2.5-2.1-2.1-3 0.8-1.9 8.4-1.4 13.4 0.3 1.9 0.6 5.2 2.1 5.2 2.1-9 2.3-13.7 1.9-16.5 0.6z"/>
</g>
</svg>
New proportion SVG:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 352 31" baseProfile="basic" version="1.1" y="0" x="0" xmlns:xlink="http://www.w3.org/1999/xlink">
<g>
    <path fill="rgb(10, 54, 83)" d="m196 15.2c6.1-4.6 10.7-5 10.7-5 0.8 0.5 3.1 1.6 5.5 2.3 7.8 2.4 13.5 1.9 14.4-0.1 0.7-1.5-0.8-3.6-3.4-4.7-8.6-3.8-20.5-0.7-20.5-0.7s-4.4-3.1-7.4-4.4c-10.1-4.5-21.5-0.2-26.1 10.2-1.4 3.1-1.8 6-1.7 8.4l0.5-0.1c0-2.4 0.3-5.1 1.6-8.1 4.6-10.5 15.8-14.2 25.4-10 2.8 1.2 5.3 3.2 6.7 4.2-8 3.4-11.4 6.5-13.5 9.7h2.2 160.5v-1.8h-154.9zm27.8-6c1.6 0.7 2.5 2.1 2.1 3-0.8 1.9-8.4 1.4-13.4-0.3-1.9-0.6-5.2-2.1-5.2-2.1 9-2.2 13.7-1.8 16.5-0.6zm-41.4 9c-4.6 10.5-15.8 14.2-25.4 10-2.8-1.2-5.3-3.2-6.7-4.2 8-3.4 11.4-6.5 13.5-9.7h-2.2-160.6v1.8h155.1c-6.1 4.6-10.7 5-10.7 5-0.8-0.5-3.1-1.6-5.5-2.3-7.8-2.4-13.5-1.9-14.4 0.1-0.7 1.5 0.8 3.6 3.4 4.7 8.6 3.8 20.5 0.7 20.5 0.7s4.4 3.1 7.4 4.4c10.1 4.5 21.5 0.2 26.1-10.2 1.4-3.1 1.8-6 1.7-8.4l-0.5 0.1c0 2.3-0.4 5-1.7 8zm-54.1 3.9c-1.6-0.7-2.5-2.1-2.1-3 0.8-1.9 8.4-1.4 13.4 0.3 1.9 0.6 5.2 2.1 5.2 2.1-9 2.3-13.7 1.9-16.5 0.6z"/>
</g>
</svg>
Background SVG: