This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
What is the SVG
Created in 1999, SVG is an XML language that resembles XHTML, or SVG, and can be used to draw vector graphics, such as the one shown on the right. SVG can create a graph by defining the necessary lines and shapes, modifying an existing bitmap, or combining the two. Graphics and their components can be deformed, synthesized, or completely altered through filters.
Why learn SVG? What are its advantages?
The advantages of SVG over other image formats such as JPG and GIF are:
- You can use any text editor to create a painting
SVG
. - You can search, index, script, compress
SVG
The image. SVG
Images can be expanded and can be displayed in high quality at any resolution.SVG
Images can be scaled without losing any quality.SVG
It’s open standard, it’s pureXML
File.
Draw a simple SVG
<svg width="300" height="200">
<rect width="100%" height="100%" fill="blue" />
<circle cx="50" cy="50" r="50" fill="red"></circle>
</svg>
Copy the code
- Draw one above
SVG
The basic flow- Draw a
svg
Root tag - Draw a
rect
Label, which is a label that draws a rectangle, width and height set100%
So if you fill up the parent element,fill
Property to set the background color - Draw a
circle
Tag, which is a label that draws a circle,cx
andcy
Is an offset property (default is0
That is, the center of the circle is in coordinates0
.0
Chinese painting),r
It’s the radius
- Draw a
Rendering rules
svg
Elements in the render order, the rule is that the later to render the elements in the ahead.
Several ways to nest an HTML file
-
XHTML needs to declare the type Application/XHTML + XML in order to embed SVG in XML.
-
HTML5 can embed SVG directly. But some grammatical tweaks are needed.
-
You can reference an SVG file with the object element:
-
<object data="image.svg" type="image/svg+xml" /> Copy the code
-
Similarly, you can reference an SVG file using an iframe element:
-
<iframe src="image.svg"></iframe> Copy the code
-
Using the IMG tag to reference an SVG file:
-
<img src="image.svg" style="display:block; width:200px; height:200px" /> Copy the code
-
Use the embed tag:
-
<embed src="image.svg" style="display:block; width:200px; height:200px" /> Copy the code
-
Div tag:
-
<div style="display:block; width:200px; height:200px; background: url(./image.svg) no-repeat; background-size: 100%;" ></div> Copy the code
-
Use the picture label:
-
<picture> <source srcset="image.svg" type="image/svg+xml"> <img src="image.svg" style="display:block; width:200px; height:200px"> </picture> Copy the code
-
SVG File types
svg
Files come in two forms- ordinary
SVG
Files: IncludedSVG
A standard text file, usually with a suffix of.svg
. - Compressed version
SVG
File: in a certain scenarioSVG
The file may be large and is allowed by the SVG standardgzip
Compressed, usually with a suffix of.svgz
(Note that inFireFox
Cannot be loaded locallysvgz
Files, unless you know how to handle the releaseweb
The server can handle it correctlygzip
Otherwise avoid usinggzip
Compression of theSVG
).
- ordinary
The last
This article covers the use of SVG in its simplest form and the various ways to embed SVG in HTML. The next article will cover SVG mesh positioning, image drawing, and more.
😀😀 Pay attention to me, don’t get lost! 😀 😀