CSS background animation works in pixels, not in percentages

Viewed 23

<style type="text/css">
  * {
    box-sizing: border-box;
  }

  .fancy {
    position: relative;
    max-width: 400px;
    padding: 15px;
    text-align: center;
  }

  .fancy::before, .fancy::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    height: 1px;
  }

  .fancy::before {
    top: 0;
    background: linear-gradient(to right, #FFFFFF, #168DBF 70%);
    animation: fancy-top 2s linear infinite;
  }

  .fancy::after {
    bottom: 0;
    background: linear-gradient(to left, #FFFFFF, #168DBF 70%);
    animation: fancy-bottom 2s linear infinite;
  }

  @keyframes fancy-top {
    0%    { background-position: 0 0; }
    100%  { background-position: 400px 0; }
  }

  @keyframes fancy-bottom {
    0%    { background-position: 0 0; }
    100%  { background-position: -400px 0; }
  }
</style>
<h1 class="fancy">Fancy Heading</h1>

I'm looking for a way to allow this to work independent of the width - I've tried a few different solutions I found from searches on similar things (including background-size, transform, widths, left/right positions), nothing seems to give me the same smooth repeating animation that this does.

Can anyone lend a hand?

0 Answers
Related