Skip to main content

SVG Tutorial: A Guide to HTML Graphics

Introduction to SVG

SVG (Scalable Vector Graphics) is a powerful XML-based vector image format for two-dimensional graphics. It offers resolution-independent scaling, making it ideal for responsive web design and high-quality graphics.

SVG in HTML

You can include SVG directly in your HTML using the <svg> element:

<svg width="100" height="100">
    <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

Basic SVG Shapes

SVG provides several basic shapes:

  • Rectangle (<rect>)
  • Circle (<circle>)
  • Ellipse (<ellipse>)
  • Line (<line>)
  • Polygon (<polygon>)
  • Polyline (<polyline>)

Try it:

SVG Path and Text

The <path> element is used to define a path in SVG. It allows you to create complex shapes by specifying a series of commands and parameters. For example, the command M moves the pen to a new location, while H and V draw horizontal and vertical lines, respectively.

<svg width="100" height="100">
    <path d="M10 10 H 90 V 90 L 10 90 Z" fill="transparent" stroke="black"/>
</svg>

Text can be added using the <text> and <tspan> elements. The <text> element defines the text content, while <tspan> allows for styling parts of the text, such as changing the color or font size.

<svg width="200" height="100">
    <text x="10" y="50">Hello, <tspan fill="red">SVG</tspan>!</text>
</svg>

Try it:

SVG Styling

SVG elements can be styled using CSS or inline attributes. This allows for a wide range of visual effects and customizations. You can change colors, borders, and other properties to enhance the appearance of your SVG graphics. For example, using the style attribute, you can apply multiple styles directly to an SVG element.

<svg width="100" height="100">
    <circle cx="50" cy="50" r="40" style="fill: yellow; stroke: black; stroke-width: 3;" />
</svg>

Try it:

SVG Filters and Effects

SVG supports various filters and effects, such as blur and drop shadows, which can add depth and dimension to your graphics. Filters are defined within a <defs> section and can be applied to any SVG element. This allows for creative visual effects that enhance the user experience.

<svg width="200" height="200">
    <defs>
        <filter id="blur">
            <feGaussianBlur stdDeviation="5" />
        </filter>
    </defs>
    <circle cx="100" cy="100" r="80" fill="green" filter="url(#blur)" />
</svg>

Try it:

SVG Gradients and Patterns

Create gradients and patterns to fill shapes. Gradients allow for smooth transitions between colors, while patterns can repeat images or designs. This enhances the visual appeal of your SVG graphics and provides more depth and texture.

<svg width="200" height="200">
    <defs>
        <linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
            <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
            <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
        </linearGradient>
    </defs>
    <rect width="200" height="200" fill="url(#grad)" />
</svg>

Try it:

SVG Transformations

Apply transformations to SVG elements to change their position, size, and orientation. Transformations can include translation, rotation, scaling, and skewing, allowing for dynamic and interactive graphics.

<svg width="200" height="200">
    <rect x="50" y="50" width="100" height="100" fill="blue" transform="rotate(45 100 100)" />
</svg>

Try it:

Advanced SVG Topics

In this section, we will explore some advanced concepts in SVG that can significantly enhance your graphics and user interactions. Understanding these topics will empower you to create more dynamic and engaging visual content.

  • Clipping and Masking: Clipping paths allow you to define a specific area of an SVG graphic that will be visible, while masking can be used to hide or reveal parts of an image based on a mask shape. This is useful for creating complex shapes and effects.

    Try it:

  • SVG Animation: SVG supports various animation techniques, including SMIL (Synchronized Multimedia Integration Language) and CSS animations. These allow you to animate properties such as position, color, and size, creating engaging visual effects.

    Try it:

  • SVG Scripting with JavaScript: You can manipulate SVG elements using JavaScript, allowing for interactive graphics that respond to user input. This can include changing attributes, adding event listeners, and creating dynamic visualizations.

    Try it:

These advanced topics allow for even more complex and interactive SVG graphics, enabling developers to create rich user experiences. Mastering these techniques will set you apart in the world of web graphics.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!