SVG in HTML




What is SVG?

  • SVG stands for Scalable Vector Graphics. Unlike raster images like PNGs or JPGs, SVGs are not pixel-based. 
  • They're composed of vectors—mathematical formulas that describe shapes, paths, and fills. This means SVGs can be resized without losing quality.

Why Use SVG?

Scalability

  • SVG images can be scaled indefinitely without losing quality, which is ideal for responsive web design.

File Size

  • SVG files are often smaller than their raster counterparts, especially for simple shapes and icons.

Flexibility

  • SVGs can be styled, animated, and manipulated using CSS and JavaScript.

How to Embed SVG in HTML

  1. Inline SVG: Directly writing the SVG XML code within HTML.
  2. Using a <img> tag: Point the src attribute to an SVG file.
  3. Using CSS: Setting SVG as a background image in a CSS file.

Inline SVG Example

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

<img> Tag Example

<img src="image.svg" alt="Sample SVG">

CSS Background Example

.background {
          background-image: url('image.svg');
        }

SVG Attributes

  • width and height: To set the dimensions.
  • viewBox: To set the coordinate system.
  • fill and stroke: To set the colors.

Examples

<svg height="30" width="30">
    <rect width="30" height="30" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" />
</svg>

Related Posts:

  • Semantic Element & Non-Semantic Elements Semantic HTML5 ElementsThe elements in HTML which define their meaning are semantic elements.And also these elements describe their co… Read More
  • HTML Elements What is an HTML Element?An HTML element is a complete set that consists of a start tag (or opening tag), content, and an end tag (or closing tag… Read More
  • HTML Id & Classes IDs and Classes in HTMLWhat is an ID?An ID is an attribute, a unique identifier assigned to only one HTML element within a page.It is often used… Read More
  • HTML Tags What is an HTML Tag?HTML Tags are the basic building block of elements in HTML. tags start with open (< ) and closing  ( … Read More
  • HTML Text Formatting HTML formatting involves enhancing the visual presentation of text. It enables text manipulation for improved aesthetics, eliminating the n… Read More

0 Comments:

Post a Comment

Do leave your comments