5

Is is possible to change width of SVG rectangle with CSS?

This doesn't work:

#red {
    width: 800px;
}
#red:hover {
    width: 1600px;
}

See jsfiddle.

Erik Dahlström
  • 59,452
  • 12
  • 120
  • 139
l00per
  • 481
  • 4
  • 7
  • 19

3 Answers3

12

Since the width css property doesn't (yet) apply to <rect> elements you can't do it like in your question.

However, you can make it work by using units that depend on some other css property.

Like this, svg:

<rect id="red" width="1em" fill="red" height="270" />

and CSS:

#red {
    font-size: 800px;
}
#red:hover {
    font-size: 1600px;
}

See jsfiddle.

To answer the followup question, "how do you do this if you want to grow the height (or width) in the opposite direction?", here's one way:

Flip the coordinate system using transform="scale(1,-1)" (adjusting the other values to go along with that, e.g negative y coordinate position).

<rect id="red" y="-515.5" width="270" height="2em" transform="scale(1,-1)"/>

and CSS:

#red:hover {    
    animation: scaleheight 1s ease-in-out infinite alternate;
}

@keyframes scaleheight {
    0% {
         fill:red;
    }
    100% {
         fill:blue;
         font-size: 300px;
    }
}

See jsfiddle.

Erik Dahlström
  • 59,452
  • 12
  • 120
  • 139
0

I've made some SVG animation for you. Check the below link shows examples like SVG Scalingand SVG element Movement. As example shows you can create many rec and animate them as way you want.

Check the Update Demo. http://jsfiddle.net/kheema/CasPj/11/

Kheema Pandey
  • 9,977
  • 4
  • 25
  • 26
-1

Yes, its possible. Make an svg file with a rectangle, and then import it to your html with img tag.

See: link

Community
  • 1
  • 1
garyry
  • 61
  • 8