Resizing SVG in html?

后端 未结 8 1410
慢半拍i
慢半拍i 2020-12-04 05:25

So, I have an SVG file in HTML, and one of the things I\'ve heard about the format is that it doesn\'t get all pixelated when you zoom in on it.

I know with a jpeg o

相关标签:
8条回答
  • 2020-12-04 06:07

    Open your .svg file with a text editor (it's just XML), and look for something like this at the top:

    <svg ... width="50px" height="50px"...
    

    Erase width and height attributes; the defaults are 100%, so it should stretch to whatever the container allows it.

    0 讨论(0)
  • 2020-12-04 06:09

    I have an SVG file in HTML [....] IS there any way to specify that you want an SVG image displayed smaller or larger than it actually is stored in the file system?

    SVG graphics, like other creative works, are protected under copyright law in most countries. Depending on jurisdiction, license of the work or whether or not you are the copyright holder you may not be able to modify the SVG without violating copyright law, believe it or not.

    But laws are tricky topics and sometimes you just want to get shit done. Therefore you may adjust the scale of the graphic without modifying the work itself using the img tag with width attribute within your HTML.

    Using an external HTTP request to specify the size:

    <img width="96" src="/path/to/image.svg">
    

    Specifying size in markup using a Data URI:

    <img width="96" src="data:image/svg+xml,...">
    

    SVGs can be Optimized for Data URIs to create SVG Favicon images suitable for any size:

    <link rel="icon" sizes="any" href="data:image/svg+xml,%3Csvg%20viewBox='0%200%2046%2045'%20xmlns='http://www.w3.org/2000/svg'%3E%3Ctitle%3EAfter%20Dark%3C/title%3E%3Cpath%20d='M.708%2045L23%20.416%2045.292%2045H.708zM35%2038L23%2019%2011%2038h24z'%20fill='%23000'/%3E%3C/svg%3E">
    
    0 讨论(0)
  • 2020-12-04 06:14

    Try these:

    1. Set the missing viewbox and fill in the height and width values of the set height and height attributes in the svg tag

    2. Then scale the picture simply by setting the height and width to the desired percent values. Good luck.

    3. Set a fixed aspect ratio with preserveAspectRatio="X200Y200 meet (e.g. 200px), but it's not necessary

    e.g.

     <svg
       xmlns:dc="http://purl.org/dc/elements/1.1/"
       xmlns:cc="http://creativecommons.org/ns#"
       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
       xmlns:svg="http://www.w3.org/2000/svg"
       xmlns="http://www.w3.org/2000/svg"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
       xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
       width="10%" 
       height="10%"
       preserveAspectRatio="x200Y200 meet"
       viewBox="0 0 350 350"
       id="svg2"
       version="1.1"
       inkscape:version="0.48.0 r9654"
       sodipodi:docname="namesvg.svg">
    
    0 讨论(0)
  • 2020-12-04 06:25

    I have found it best to add viewBox and preserveAspectRatio attributes to my SVGs. The viewbox should describe the full width and height of the SVG in the form 0 0 w h:

    <svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 700 550"></svg>
    
    0 讨论(0)
  • 2020-12-04 06:26

    Changing the width of the container also fixes it rather than changing the width and height of source file.

    .SvgImage img{ width:80%; }
    

    This fixes my issue of re sizing svg . you can give any % based on your requirement.

    0 讨论(0)
  • 2020-12-04 06:26

    Use the following code:

    <g transform="scale(0.1)">
    ...
    </g>
    
    0 讨论(0)
提交回复
热议问题