The x
and y
attributes are only valid for an inner (nested) SVG. They have no effect on top level SVGs like this.
If you want to arrange the SVGs on your HTML page, you need to use CSS to do it.
By default SVGs are effectively display: inline-block
, like <img>
elements, so they will normally be arranged next to each other as if they were sitting on a baseline of a piece of text.
var svg = d3.select("body").append("svg")
.attr("width", 900)
.attr("height", 900);
var innerSVG = d3.select("body").append("svg")
.attr('width',400)
.attr('height',400);
svg {
border: solid 1px blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
If your SVGs are not next to one another, the probable reason is that your page is not wide enough and the second one is wrapping to the next line. You could fix that by putting them in a parent container that is wide enough to take both SVGs.
var svg = d3.select("div").append("svg")
.attr("width", 900)
.attr("height", 900);
var innerSVG = d3.select("div").append("svg")
.attr('width',400)
.attr('height',400);
svg {
border: solid 1px blue;
}
div {
width: 1305px;
border: solid 1px green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div></div>
You might now look at this and be wondering why the two SVGs are both at the bottom of the parent <div>
. The reason goes back to what I said earlier about SVGs being inline-block
and being positioned like they are on the baseline of some text.
There are a few ways you can fix that. One way would be to make the SVGs display: block
instead, then have them float: left
.
var svg = d3.select("div").append("svg")
.attr("width", 900)
.attr("height", 900);
var innerSVG = d3.select("div").append("svg")
.attr('width',400)
.attr('height',400);
svg {
border: solid 1px blue;
display: block;
}
svg:nth-child(1) {
float: left;
}
div {
width: 1305px;
border: solid 1px green;
overflow: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div></div>