What is an SVG file? Bueno Web Agency

What is an SVG file and how do you use it?

Harness the full potential of SVGs

Images are essential – they tell stories, convey emotions and set the visual tone for your website. But not all images are created equal. Traditional formats like JPEG and PNG, while widely available, can have a significant drawback: they lose quality when scaled up. This is where Scalable Vector Graphics (SVG) comes into play with crystal clear advantages.

SVGs aren’t made up of pixels like traditional images. Instead, they use mathematical formulas to define lines, shapes, and colors. This magic formula means that no matter how big or small an SVG is displayed, it will always look perfectly sharp. They’re ideal for logos, icons, illustrations, and other design elements that need to look perfect on any screen size. What’s more, SVGs often have incredibly small file sizes, making your website fast and smooth to load.


SVG Basics

The building blocks of SVGs

Like a digital drawing, SVGs are constructed using special code that defines the basic building blocks of the image. Let's break down the main components:

Basic elements

  1. Rectangles ( ): Defines rectangular shapes with attributes such as width, height, x, and y for positioning. Add rounded corners with rx and ry.
  2. Circles ( ): Create circles using the attributes cx and cy for center coordinates and the attribute r for radius.
  3. Ellipses ( ): Similar to circles, but with the rx and ry attributes to adjust the radius along the x and y axes.
  4. Lines ( ): Draw straight lines with starting coordinates (x1, y1) and ending coordinates (x2, y2).
  5. Polygons ( ): Specify a series of points to create closed shapes with any number of sides.
  6. Rises ( ): The most powerful element – use the d attribute to draw complex curves and shapes using a series of commands.
  7. Text ( ): Embed text directly into your SVG for titles, labels, and more.

Attributes: Important Modifiers

  1. fill: Controls the color inside a shape. Accepts color names (red), hex codes (#FF0000), RGB values (RGB(255, 0, 0))
  2. stroke: Specifies the color of the outline around a shape.
  3. stroke-width: Determines the thickness of the outline.
  4. Opacity: Controls the transparency of an element (values from 0 to 1).

Groups and transformations

  1. -tag: Group elements together for common styling or transformations.
  2. Transform attributes: Apply transformations such as rotating, moving, scaling, or skewing elements or groups.

Example: Create a simple SVG

Here's a simple example that shows how these basic elements and attributes work together:

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

This code creates a blue circle with a black outline in the center of a 100×100 pixel SVG canvas.


Embedded SVG vs. External SVG

When working with SVGs on websites, you have two main methods for integrating them:

Embedded SVG:

The SVG code is embedded directly into your HTML document using -tag.

Advantages:

  • No extra HTTP requests, which can potentially improve page load times.
  • Full CSS control over individual SVG elements.
  • Perfect for simple SVGs or those that require extensive styling.

Disadvantages:

  • Can increase the size of your HTML files, especially with complex SVGs.
  • Less suitable for reusing the same SVG on multiple pages.

External SVG:

The SVG file exists as a separate file with the extension .svg.

You reference it in your HTML using methods like <img>, <object> or as a background image.

Advantages:

  • Cleaner separation of code for better maintainability.
  • Perfect for reusing the same SVG in multiple places.
  • Browser-level caching can potentially increase performance on all pages.

Disadvantages:

  • Requires an extra HTTP request (although caching can mitigate this).
  • Less direct CSS control over individual SVG elements in some cases.

More great guides about images

Don't miss our other guide about What is the difference between AVIF and WebP? And why you should use it on your website.

Contact Bueno