Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

svg background image position is always centered in internet explorer, despite background-position: left center;

Tags:

html

css

svg

I'm using a SVG logo as a background image and I can't seem to get it to align correctly to the left in Internet Explorer (edit: and Safari).

The containers look like so:

<div id="header">     <div id="logo"></div> </div> 

With the styles:

#header{     width: 100%;     max-width: 1200px;      height: 100%;}  #logo{     background: url(../images/ss_logo.svg);     background-position: left center;     width: 100%;     height: 100%;     float: left;} 

You can see that the <div> should span 100% of its parent but display the logo to the left of the element. This works fine in Chrome and Safari, but the logo is always centered inside the <div id="logo"> in IE.

Information seems to be really hard to find on this, has anyone else had the same problem?

Here's a link to an example version of the problem, the green box is the SVG.

like image 233
bluefantail Avatar asked Jul 30 '13 10:07

bluefantail


People also ask

Can you set an SVG as a background image?

SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. All the same awesomeness of SVG comes along for the ride, like flexibility while retaining sharpness. Plus you can do anything a raster graphic can do, like repeat.

How do I keep the background of a picture in the center?

First, set a background image using the background-image property. Next, set the background-repeat property to no-repeat . Then, write the fixed option for the background-attachment property. After that, apply the background-position property to the center center option and the background-size property to cover .

Which property is used for controlling the image position in the background?

The background-position property sets the starting position of a background image.

Which background property is used to set where background images will be positioned in the box model?

The background-position CSS property sets the initial position for each background image.


1 Answers

The problem is not with your CSS but with your SVG. The SVG will grow to fill the entire element box’s background (as expected). How your SVG scale then becomes the controlling factor:

Set a viewBox="0 0 width height" (in pixels) attribute on your <svg> element and remove its width and height attributes. You also need to set preserveAspectRatio="xMinYMid" (x/vertically left-aligned, y/horizontally middle-aligned) on the svg element. This works with Internet Explorer 10 and 11, at least.

<svg viewbox="0 0 64 64"     preserveAspectRatio="xMinYMid">     … </svg> 

Learn more about the preserveAspectRatio and viewBox attributes. Source, “Getting started with SVG” in the IEblog.

like image 194
Daniel Avatar answered Oct 01 '22 17:10

Daniel