viewBox is for drawing whats inside the SVG,
height tell at what size to display it.
The viewBox defines the canvas and coordinate system you are drawing your SVG into. If it says
viewBox="10 50 100 100", and you want to draw a square that covers the upper left quarter of your image, you'll write:
<svg viewBox="10 50 100 100" xmlns="http://www.w3.org/2000/svg"> <rect x="10" y="50" width="50" height="50" /> </svg>
Then, when you want to display your SVG, for example embeded into a html page, width and height tell at what size to display them, just like you would for an image tag:
<div> <svg width="250" height="250" viewBox="10 50 100 100" xmlns="http://www.w3.org/2000/svg"> <rect x="10" y="50" width="50" height="50" /> </svg> </div>
If your SVG was saved as an external file
drawing.svg, you could express the same effect as
<div> <img src="drawing.svg" width="250" height="250" /> </div>
In both cases, the square you have drawn would cover the upper left quarter of the image, but since the SVG is drawn at an overall size of 250px ✕ 250px, the square has a side length of 125px ✕ 125px.
In other words, if the width defined in the viewBox is
a, and the width defined on the element is
b, the effect is the same as scaling everything inside the SVG (horizontally) with a factor of
b / a.
(That obviously opens up the question what happens when viewBox height is
c, element height is
d, and the ratios
d / c ≠ b / a don't match? That is what the
preserveAspectRatio attribute describes.)
Formally, every combination of
preserveAspectRatio can be written equivalently as a
transform attribute. The algorithm for this is described here. (Note that for the
<svg> element as used here in the example, the
y attributes must always be 0.)
- viewBox attribute with svg height and width attributes set
- Dynamically set the width and height of SVG based on data
- D3 SVG with finite height and width
- how to use getElementById with getBBox to determine the svg width and height
- How to set the width and height of svg to be equal to its g?
- Responsive SVG with d3.js and height attribute
- set width and height of an SVG element - d3.js
- d3.js brush() inherits incorrect height and width From svg parent using viewBox
- Clojurescript integrating with datamaps sets svg width and height 0
- Auto width and height for SVG image
- Making SVG container 100% width and height of parent container in D3 v4 (instead of by pixels)
- IE11 does not accept SVG height and width when using D3
- Is it possible to set custom attributes of SVG objects as number and not as string?
- D3 Zoom with Scrollbars used as panning, Scrollbar width and height adjusts to zoom scale
- How to maintain the SVG marker width and height ?
- How can I get the natural width and height of an svg image element?
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- Get attributes of existing SVG elements and bind as data with d3.js
- How to get the width and height of an image? ( getBBox() size of SVG return 0 )
- D3 is attempting to set attributes of SVG as NaN and I can't figure out why
- D3 Bar Chart with Variable X and Y(variable width and height for bars)
- Distribute nodes evenly around central node using a relative SVG width and height
- how can I set a height and width for this map in d3.js
- bubble chart width and height set dynamically from json data in d3.js
- trouble with D3js world map and svg group element width
- Change height and width of the path tag in SVG Image
- Why is this SVG Path showing incorrect width and height on Firefox?
- viewBox doesn't appear to work when width & height is set
- D3.js: How to get the computed width and height for an arbitrary element?
- How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?
More Query from same tag
- nvd3.js rendering solution for Internet Explorer
- Show social graph with D3 force layout
- D3.js - Date ticks don't align with data path - offset to the right
- Deselect/Select all checkboxes in Class for D3
- Understanding MERGE in context of force graph
- D3 line graph getting error and not getting plotted
- Combining Bivariate Graph with Line Graph in a single chart
- D3 Pie Error: Invalid value for <text> attribute transform="translate(NaN,NaN)"
- D3.js selectors and functions on a jQuery object
- Accessing SVG path length in d3
- C3 Tick marks not lining up with data after API Load call
- What is Null doing when it is inserted into an attribute function in d3?
- ScatterChart in NVD3 – Reading the data from csv file
- D3 stacked bar chart not able to see full x axis lables
- Legend in force-directed graph
- Switch JSON file for a visualisation using a dropdown
- d3: flatten nested data?
- How to fix the d3 object?
- D3: am I using scale.invert() wrong?
- d3.js - apply exit after append to simulate a animation
- Making Cubism.js work with Rails app
- dc.js pieChart set specific x axis value
- How to add marking in bar chart using d3
- nvd3.js ugly labels in pieChart
- D3.js linear scale [0,1] with a specific number of string tick values [A,B,C,...]
- d3.join() enter called instead of update?
- How to use Map.get() with array of arrays
- How do I change color of path element when its value is selected from dropdown