nciao
nciao

Reputation: 601

Center aligning an SVG in a span tag

I'm trying to center align the bar chart in the gray box. I've tried various approaches (e.g. text-align: center, or setting display:block, margin:0 auto) from perusing related posts and none of them seem to work.

Here's the fiddle: http://jsfiddle.net/nickciao/F6R9C/93/

The html:

<div id="Tile">
<h2>
     <span id="Top">0.0%</span>
</h2>
<ul class="Middle">
    <span>2.9k</span>
         USERS
    <span>BECAME ACTIVE</span>
</ul>
<div id="Bottom" >
    <div>
    <svg id="svg">
        <g transform="translate(5,5)">
            <rect y="23.33333333333333" x="15" height="11.666666666666671" width="5">
            </rect>
            <rect y="35" x="21" height="0" width="5">
            </rect>
            <rect y="25.666666666666686" x="27" height="9.333333333333314" width="5">
            </rect>
            <rect y="35" x="33" height="4.666666666666686" width="5">
            </rect>
            <rect y="25.666666666666686" x="39" height="9.333333333333314" width="5">
            </rect>
            <rect y="25.666666666666643" x="45" height="9.333333333333357" width="5">
            </rect>
            <rect y="25.666666666666686" x="51" height="9.333333333333314" width="5">
            </rect>
            <rect y="30.333333333333343" x="57" height="4.666666666666657" width="5">
            </rect>
            <rect y="32.666666666666686" x="63" height="2.3333333333333144" width="5">
            </rect>
            <rect y="32.66666666666664" x="69" height="2.333333333333357" width="5">
            </rect>
            <rect y="16.33333333333333" x="75" height="18.66666666666667" width="5">
            </rect>
            <rect y="32.666666666666686" x="81" height="2.3333333333333144" width="5">
            </rect>
            <rect y="35" x="87" height="0" width="5">
            </rect>
            <rect y="35" x="93" height="20.999999999999993" width="5">
            </rect>
            <rect y="35" x="99" height="34.999999999999986" width="5">
            </rect>
        </g>
    </svg>
    </div>
</div>

Upvotes: 2

Views: 11806

Answers (2)

kirkyoder
kirkyoder

Reputation: 322

updated fiddle here

SVG elements are inline, by default, so first ensure that you've set it to display a block element. You can specify a margin to center and fit the element inside its parent container:

#svgContainer svg {
    display: block;
    margin: 0 auto;
}

You can then leverage the "viewbox" attribute to ensure your SVG element will scale to fit the bounds of its parent container:

<svg viewbox="0 0 120 80" height="80">

The viewbox width/height, in your case, should always correlate to the size of the graph you're displaying. (Assuming you want to show the entire graph, anyway.)

Here's a good reference for how the viewport and viewbox play together in SVG elements: http://jonibologna.com/svg-viewbox-and-viewport/

Upvotes: 7

Rachel Gallen
Rachel Gallen

Reputation: 28593

you could give it a width and then a margin left and right accordingly

e.g.

span svg {
    width:20%;
    margin-right:40%;
    margin-left:40%;}

fiddle

Upvotes: 0

Related Questions