Center Path inside SVG

Aaron Benjamin

Is it possible to center a path vertically within an SVG element?

This is one of the paths I need to center:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 144 144"><path d="M127.782 40.348H15.802c-3.813 0-6.912 3.002-6.912 6.766v14.113c0 3.235 1.52 6.11 3.953 7.92h-.012s52.9 35.873 53.09 36c1.56.978 3.82 1.886 5.82 1.886 2.21 0 4.25-.71 5.9-1.886l52.54-35.675c2.69-1.78 4.494-4.81 4.494-8.246V47.194c0-3.772-3.07-6.846-6.885-6.846zm-38.05 29.864c-.066 1.966-.685 3.392-2.68 3.392H64.744c0 4.482 4.56 6.735 8.77 6.735 7.825 0 9.407-4.4 12.117-3.34 1.49.58 2.14 1.62 2.11 3.62-.08 4.48-5.292 7.864-14.94 7.864-11.637 0-19-6.573-19-17.353 0-10.31 7.63-17.88 18.184-17.88 10.465 0 17.757 7.04 17.757 16.74v.215zm-17.91-8.494c-3.434 0-6.28 2.284-6.695 5.597h13.47c-.068-3.45-3.116-5.597-6.776-5.597zm-.087 53.697c-3.328-.038-6.947-1.51-8.567-2.805L8.86 75.7v52.492c0 3.764 3.065 6.808 6.88 6.808h111.983c3.824 0 6.944-3.044 6.944-6.808v-52.49L80.53 112.61c-2.586 1.832-5.636 2.84-8.795 2.805z"/></svg>
Paul LeBeau

If you mean automatically, without intervention, then no. However you can add a transform to the path to centre it in the SVG.

The bounding box for the path is:

{
   x: 8.859999656677246,
   y: 40.347999572753906,
   width: 125.81500244140625,
   height: 94.6520004272461
}

The height of the document is 144, so the y should actually be at:

(144 - 94.652) / 2 = 24.674

So the y position of the path has to be adjusted by:

(24.674 - 40.348) = -15.674

So add the following attribute to the path element:

transform="translate(0, -15.674)"

Demo before and after:

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 144 144" style="background-color: gray"><path d="M127.782 40.348H15.802c-3.813 0-6.912 3.002-6.912 6.766v14.113c0 3.235 1.52 6.11 3.953 7.92h-.012s52.9 35.873 53.09 36c1.56.978 3.82 1.886 5.82 1.886 2.21 0 4.25-.71 5.9-1.886l52.54-35.675c2.69-1.78 4.494-4.81 4.494-8.246V47.194c0-3.772-3.07-6.846-6.885-6.846zm-38.05 29.864c-.066 1.966-.685 3.392-2.68 3.392H64.744c0 4.482 4.56 6.735 8.77 6.735 7.825 0 9.407-4.4 12.117-3.34 1.49.58 2.14 1.62 2.11 3.62-.08 4.48-5.292 7.864-14.94 7.864-11.637 0-19-6.573-19-17.353 0-10.31 7.63-17.88 18.184-17.88 10.465 0 17.757 7.04 17.757 16.74v.215zm-17.91-8.494c-3.434 0-6.28 2.284-6.695 5.597h13.47c-.068-3.45-3.116-5.597-6.776-5.597zm-.087 53.697c-3.328-.038-6.947-1.51-8.567-2.805L8.86 75.7v52.492c0 3.764 3.065 6.808 6.88 6.808h111.983c3.824 0 6.944-3.044 6.944-6.808v-52.49L80.53 112.61c-2.586 1.832-5.636 2.84-8.795 2.805z"/></svg>

<svg xmlns="http://www.w3.org/2000/svg" width="300" height="300" viewBox="0 0 144 144" style="background-color: gray">
  
  <path transform="translate(0, -15.674)"
        d="M127.782 40.348H15.802c-3.813 0-6.912 3.002-6.912 6.766v14.113c0 3.235 1.52 6.11 3.953 7.92h-.012s52.9 35.873 53.09 36c1.56.978 3.82 1.886 5.82 1.886 2.21 0 4.25-.71 5.9-1.886l52.54-35.675c2.69-1.78 4.494-4.81 4.494-8.246V47.194c0-3.772-3.07-6.846-6.885-6.846zm-38.05 29.864c-.066 1.966-.685 3.392-2.68 3.392H64.744c0 4.482 4.56 6.735 8.77 6.735 7.825 0 9.407-4.4 12.117-3.34 1.49.58 2.14 1.62 2.11 3.62-.08 4.48-5.292 7.864-14.94 7.864-11.637 0-19-6.573-19-17.353 0-10.31 7.63-17.88 18.184-17.88 10.465 0 17.757 7.04 17.757 16.74v.215zm-17.91-8.494c-3.434 0-6.28 2.284-6.695 5.597h13.47c-.068-3.45-3.116-5.597-6.776-5.597zm-.087 53.697c-3.328-.038-6.947-1.51-8.567-2.805L8.86 75.7v52.492c0 3.764 3.065 6.808 6.88 6.808h111.983c3.824 0 6.944-3.044 6.944-6.808v-52.49L80.53 112.61c-2.586 1.832-5.636 2.84-8.795 2.805z"/></svg>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SVG path inside circle

From Dev

Center a div with an svg and text inside of it

From Dev

Center a div with an svg and text inside of it

From Dev

Animation: Spin svg path on its own center inside a larger rect box

From Dev

Nest text inside SVG path

From Dev

Path inside svg is not rotating correctly

From Dev

Manipulate class of a <path> inside a SVG

From Dev

Path inside svg is not rotating correctly

From Dev

SVG path animation from center point

From Dev

SVG - scale path from center repetitively (pulsating)

From Dev

Rotating a Svg path around center using matrix

From Dev

SVG - scale path from center repetitively (pulsating)

From Dev

Svg scale path at a center point (pulsating)

From Dev

SVG - Center text for each path/shape

From Dev

How to center text inside a SVG g element?

From Dev

Trying to center an svg circle inside a div

From Dev

Clip-path and SVG rect inside animation

From Dev

How do I perfectly center a single character inside an SVG circle?

From Dev

Vertically Align Rect Elements Inside SVG By Center of Rect

From Dev

Recognize point(x,y) is inside svg path or outside

From Dev

Adjacent lines inside svg path with stroke-width

From Dev

What does the letter 'e' means inside a SVG path?

From Dev

How do I rotate or scale (transform) an SVG path relative to its center point?

From Dev

How to make svg path element scale up and down from the elements unknown center point?

From Dev

How to make svg path element scale up and down from the elements unknown center point?

From Dev

In SVG, is <path /> identical to <path></path>?

From Dev

How to center text in SVG?

From Dev

SVG center text in circle

From Dev

How to center a circle in an svg

Related Related

HotTag

Archive