Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize svg used in img tag in html?

Tags:

html

svg

Today, I have started to learn SVG and using it in HTML.

So, I created a file in SVG as follows:

 <svg xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         viewbox="0 0 400 200">
        <path d = "M0,100 L100,100 L125,50 L175,150 L225,50 L275,150 L300,100 L400,100"
                style="
                    stroke: #000000;
                    stroke-width: 5;
                    fill: none;
                    " 
        />
    </svg>

Then I tried to use it in html as image:

<!DOCTYPE html>
<html>
    <head>
        .....
    </head>
    <body>

        <div class="menu">
            <ul>
                <li class="menu-item"><a href="#"><img src="svg/mysvg.svg" height="48" width="48"></img></a></li>
                <li ...... />
                <li ...... />
                <li ...... />
            </ul>
        </div> 

    </body>
</html>

But I cannot see the svg as its not resized. What is my mistake? Can anybody point me to the right direction?

like image 644
Vishal Avatar asked Dec 01 '16 19:12

Vishal


1 Answers

The problem is that viewBox has been written with a lower-case b. As a result, this parameter is not recognized by the browser. In an SVG without a viewBox attribute, all lengths are assumed to be in pixels, so resizing the SVG will have no effect.

If you change viewbox to viewBox, the SVG will scale so that the view box area matches the dimensions of the SVG (in much the same way as an ordinary embedded image).

like image 151
r3mainer Avatar answered Oct 19 '22 00:10

r3mainer