Andrey N. Andrey N. - 1 year ago 48
CSS Question

Alignment of SVG of animation

I have a problem with alignment of SVG of the text.


I want that "AN" was on the center from the very beginning.

But if I do:

.logo-svg {
margin-left: 16em;
transition-delay: 222ms;

On one computer all exactly on the center, but on another computer - no.

Answer Source

If you don't want to rely on carefully tweaked margins to center your SVG, then by making a few changes, you can leave the centering up to the browser. That will mean that the centering will continue to work even if other elements like the <p> change in size.

Here's how I would do it:

  1. Wrap the whole thing in a <div> so we have something to center in. If you are centering on the page, then you don't need this. You could just use the <body>.
  2. Give the <div> a width that's wide enough for the whole SVG (340px). If your original container (or <body>) is wider than the SVG, then you don't need this either.

  3. Set preserveAspectRatio="xMinYMid slice on the SVG. This makes the SVG content left aligned with the SVG viewport instead of being centered. The "slice" part means that it overflows off the RHS of the SVG instead of being scaled down to fit.

  4. Set the height of the SVG to 72 so that its width/height ratio matches the viewBox. This is needed so the next step works.

  5. Set the width of the SVG so that it covers only the first two letters. Since the width matches the initially-visible part of the SVG (the "AN"), it gets automatically centred in the container.

  6. On hover,. set the width of the SVG to the full width (340px). The browser will recentre the new, wider SVG.

See working demo below. I've simplified your example a bit here to make things clearer.

body {
  text-align: center;

svg {
  margin: 80px auto 0;
  width: 106px;

svg:hover {
  width: 340px;

.logo-type {
  opacity: 0;

svg:hover .logo-type {
  opacity: 1;

p {
  font-size: 13px;
  color: #777;
  background: #333;
  width: 15em;
  margin: 3em auto;
  padding: 0.75em 0;
  <div class="container">
    <svg class="logo-svg" viewBox="0 0 224.8 47.6" width="340px" height="72px"
         preserveAspectRatio="xMinYMid slice">
        <g class="logo-type-group">
            <path class="logo-type logo-type-code-ninero" d="M178,0v38.2h-54.4L109.4,24c0,4.7,0,9.5,0,14.2c-12.8,0-25.6,0-38.3,0V29h28.3V24H70.9c0-8,0-16,0-24c21.7,0,43.3,0,65,0
		l0-27.2L178,0z M109.4,23.3c4.4-4.5,8.9-8.9,13.3-13.4h-13.3C109.3,14.4,109.4,18.8,109.4,23.3z M80.4,14.4c6.3,0,12.6,0,19,0
            <path class="logo-type logo-type-code-s" d="M224.8,0c-3.2,3.2-6.5,6.5-9.7,9.7c-7.2,0-14.4,0-21.6,0c-1.6,1.6-3.1,3.1-4.7,4.7c9.7,0,19.3,0,29,0c0,4.5,0,9,0,13.5

      <path class="logo-an" d="M69.7,0c0,15.1,0,30.2,0,45.3c-10.3-10.3-20.7-20.7-31-31c0,7.9,0,15.8,0,23.8c-3.2,0-6.5,0-9.7,0c0-9.4,0-18.8,0-28.2

    <p class="hover">***</p>