How to use .svg files in a webpage?

前端 未结 7 1344
长情又很酷
长情又很酷 2021-01-30 19:37

I want to know how can one actually use a .svg file In a web page?

相关标签:
7条回答
  • 2021-01-30 20:33

    I recommend putting the svg inline into your document (html5 technique). Just open your SVG file, copy the SVG tag and everything insideof it and then paste it into your html document.

    <html>
        <body>
            <svg></svg>
        </body>
    </html>
    

    It has the advantage that this allows you to use css to style it, like changing the fill color or applying filters to it like blur. Another advantage is that you save one http request for fetching the svg file if it is inside of your document.

    If you want for example to change its position using css, then you have to put the css inside of a style attribute. Styles that are in an external css file will not get applied in most browser as this is a security restriction. For example:

    <svg id="mySVG" style="position: absolute; top: 200px; left: 200px;"></svg>
    

    This technique is supported by all browsers except IE8 and below as well as the android 2.3 browser and below.

    Read the chapter inline SVG for further details:

    • css-tricks.com Using SVG
    • developer.mozilla.org SVG In HTML Introduction

    If you dont want to put it inline in your page then the best alternative seems to be the object tag and avoid using the embed tag.

    Read this for further details about object vs embed vs img tag:

    • How to Add Scalable Vector Graphics to Your Web Page
    0 讨论(0)
提交回复
热议问题