Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive SVG rectangle

Tags:

html

css

svg

I found a very good example of svg button animation but the size of the button is static - 320px x 60px. I would like to have the button responsive - it is svg rectangle, but I struggle to achieve that.

HTML:

<div class="svg-wrapper">
  <svg class="svg-responsive" viewBox="0 0 320 60" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
    <rect class="shape" width="100%" height="100%"/>
  </svg>
  <div class="text">HOVER</div>
</div>

CSS:

.svg-wrapper {
  border: 1px solid red;
  position: relative;

  height: 0;
  padding-top: 16.6%;
}
svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.shape {
  width: 100%;
  height: 100%;
  fill: salmon;
  stroke-dasharray: 140 540;
  stroke-dashoffset: -474;
  stroke-width: 8px;
  stroke: #19f6e8;
}

.text {
  color: #fff;
  font-family: "Roboto Condensed";
  font-size: 22px;
  letter-spacing: 8px;
  line-height: 32px;
  position: relative;
  top: -48px;
}

@keyframes draw {
  0% {
    stroke-dasharray: 140 540;
    stroke-dashoffset: -474;
    stroke-width: 8px;
  }
  100% {
    stroke-dasharray: 760;
    stroke-dashoffset: 0;
    stroke-width: 2px;
  }
}

.svg-wrapper:hover .shape {
  -webkit-animation: 0.5s draw linear forwards;
  animation: 0.5s draw linear forwards;
}

https://codepen.io/anon/pen/dePNLG

Already read a tutorial on the matter and I followed all the steps for inline svg but I guess I'm missing something. Can you help me get that SVG rectangle responsive?

like image 279
pollx Avatar asked May 03 '26 18:05

pollx


1 Answers

Add the preserveAspectRatio="none" attribute to your svg

like image 75
ibrahim tanyalcin Avatar answered May 06 '26 08:05

ibrahim tanyalcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!