Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change SVG Viewbox size with CSS

Tags:

css

svg

The Question: Is there a way to change the size of the SVG viewbox with CSS, but preserve the aspect ratio? OR is there another way to preserve the aspect ratio of the SVG without a view box.

The Problem: I want to responsively adjust the size of the SVG, but keep the aspect ratio. The width of the viewbox adjusts with the page, but the height doesn't adjust with the width. The problem with that is that the height of the container div is dependent on the height of the viewbox. So the container div may be really tall even if there's nothing showing in the bottom half of the viewbox.

Fiddle: http://jsfiddle.net/hT9Jb/1/

<style>     div{         width: 100%;         height: 500px;         background-color: #00ffff;     }      svg{         width: 50%;         background-color: #ffff00;     } </style>  <div>     <svg version="1.1" id="Layer_1" x="0px" y="0px" width="250px" height="400px" viewBox="0 0 250 400" enable-background="new 0 0 250 400" aspect-ratio="XminYmin">         <rect x="0" y="0" fill="#FF0000" width="250" height="400"/>     </svg> </div> 

(Object SVGs and img SVGs wont work for my purposes. It has to be inline. The solution doesn't have to be CSS...javascript is definitely an acceptable solution.)

like image 301
Geoffrey Burdett Avatar asked Jun 28 '14 02:06

Geoffrey Burdett


People also ask

Can you change SVG viewBox with CSS?

In order to change the value of the SVG viewport's width and height, we can use CSS. Simple. But to change the value of the viewBox , we currently need to use JavaScript. Not all SVG presentation attributes are available as CSS properties; only the set of attributes that have CSS property equivalents can be set in CSS.

How do I change the size of an SVG in CSS?

Any height or width you set for the SVG with CSS will override the height and width attributes on the <svg> . So a rule like svg {width: 100%; height: auto;} will cancel out the dimensions and aspect ratio you set in the code, and give you the default height for inline SVG.

How do I scale SVG viewBox?

We use viewBox to make the SVG image scalable. viewBox = “0 0 100 100”: defines a coordinate system having x=0, y=0, width=100 units and height=100 units. Thus, the SVG containing a rectangle having width=50px and height=50px will fill up the height and width of the SVG image, and all the dimensions get scaled equally.

How do I make SVG viewBox smaller?

Values of width and height: With the width and height values you can change the size of the SVG vector. So, if we want to change the size and make it larger, then set the value for width and height, in viewBox, smaller then the width and height properties of the SVG element.


2 Answers

You could use a transform:

transform: scale(0.75); // 75% of original size 
like image 57
pruhter Avatar answered Sep 20 '22 12:09

pruhter


I haven't yet found a way to change the viewBox property with CSS. However this Javascript solution works well:

var mySVG = document.getElementById('svg'); mySVG.setAttribute("viewBox", "0 0 100 100"); 

Also remove any references to width and height from the SVG and set them in CSS. Even though your working with an SVG, it's inline so cascading rules apply.

like image 23
sansSpoon Avatar answered Sep 20 '22 12:09

sansSpoon