SVG Basics and Document Structure:


SVG (Scalable Vector Graphics) is a standard format for vector graphics that allows for the creation of scalable and interactive graphics on the web. SVG images are defined using XML syntax, making them easily readable and editable. Here's how to set up a basic SVG document structure:


SVG Element: The <svg> element is used to define an SVG graphic. It can be embedded directly within an HTML document or stored as a separate SVG file and linked to the HTML file.



 

<svg width="200" height="200">
  <!-- SVG content goes here -->
</svg>
 


 

Width and Height: You can specify the width and height of the SVG canvas using the width and height attributes of the <svg> element. These dimensions define the viewport for the SVG content.


SVG Content: Inside the <svg> element, you can add various SVG shapes, paths, text, and other elements to create your graphic.




Example: Creating a Simple SVG Graphic:


Let's create a basic SVG graphic with a rectangle and a circle:


 

<!DOCTYPE html>
<html>
<head>
  <title>SVG Example</title>
</head>
<body>
  <svg width="300" height="200">
    <rect x="50" y="20" width="200" height="100" fill="blue" />
    <circle cx="150" cy="150" r="50" fill="red" />
  </svg>
</body>
</html>
 







In this example:

 
  • <rect> defines a blue rectangle with a starting point at coordinates (50, 20) and dimensions of 200x100 pixels.
  • <circle> defines a red circle with a center at coordinates (150, 150) and a radius of 50 pixels.
  • When you open this HTML file in a web browser, you'll see the SVG graphic with the rectangle and circle rendered.


Embedding SVG in HTML:

 

SVG can be embedded directly into an HTML document using the <svg> element, or it can be linked to as an external SVG file. Here's how to embed SVG in HTML:
 

Inline SVG: You can include SVG directly within an HTML file using the <svg> element.

 

<!DOCTYPE html>
<html>
<head>
  <title>Inline SVG Example</title>
</head>
<body>
  <h1>Inline SVG Example</h1>
  <svg width="300" height="200">
    <rect x="50" y="20" width="200" height="100" fill="blue" />
    <circle cx="150" cy="150" r="50" fill="red" />
  </svg>
</body>
</html>
 







 

External SVG File: Alternatively, you can create a separate SVG file (e.g., graphic.svg) and link to it from your HTML file using the <object> or <img> element.



Using <object>:



 

<!DOCTYPE html>
<html>
<head>
  <title>External SVG Example</title>
</head>
<body>
  <h1>External SVG Example</h1>
  <object type="image/svg+xml" data="graphic.svg" width="300" height="200"></object>
</body>
</html>

 







 

In both cases, the SVG file (graphic.svg in this example) contains the SVG markup defining the graphic. This approach is useful for reusing SVG graphics across multiple HTML pages.




Advantages of Using SVG:
 
  • Scalability: SVG graphics can be scaled to any size without losing quality, making them ideal for responsive web design.
  • Interactivity: SVG supports interactive elements such as animations, hover effects, and clickable regions.
  • Accessibility: SVG allows for accessible designs, including text alternatives and semantic structure.
  • Small File Size: Compared to raster images, SVG files are often smaller in size, leading to faster loading times.
  • Editability: SVG graphics are easily editable using vector graphic editors like Adobe Illustrator or Inkscape.


By leveraging SVG, web developers can create visually appealing and interactive graphics that enhance user experience across various devices and screen sizes.

 



Practice Excercise Practice now