Integrating SVG via CSS Background with viewBox and viewport Attributes

In this method we use SVG as a background image in CSS. The viewbox helps us to define the size and shape of the picture, and the viewport attributes width and height help us to control the size of the SVG within its containing element. preserveAspectRatio ensures that the SVG maintains its aspect ratio. The background-size property controls how the SVG scales within its container.

Syntax for HTML:

<div class="svg-background"></div>

Syntax for CSS:

.svg-background {
background-image: url('path/to/svg');
background-size: value;
}

Example: Illustration of Integrating SVG via CSS Background with viewBox and viewport Attributes

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>
          Integrating SVG 
          via CSS Background
      </title>
    <link rel="stylesheet" 
          href="style.css">
</head>

<body>
    <div class="container">
        <div class="content">
            <h1>
                  Integrating SVG 
                  via CSS Background
              </h1>
        </div>
    </div>
</body>

</html>
CSS
/* style.css */

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.container {
    width: 300px;
    height: 300px;
    background-image: url(
'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>');
    background-size: cover;
    background-repeat: no-repeat;
    border: 1px solid black;
    border-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

.content {
    text-align: center;
    color: black;
}

h1 {
    font-size: 20px;
    margin-bottom: 10px;
}

p {
    font-size: 8px;
    line-height: 1;
}

Output:

How does the SVG Viewbox & Viewport work in CSS ?

SVG (Scalable Vector Graphics) is a format used to create graphics on the web that can scale without losing quality. Within SVG, two important concepts are the viewBox and viewPort, which play crucial roles in how SVG elements are displayed and scaled.

Table of Content

  • What is SVG ViewBox?
  • What is SVG ViewPort?
  • How Do They Work Together?
  • Employing External SVG with viewbox and viewport Attributes:
  • Integrating SVG via CSS Background with viewBox and viewport Attributes:
  • Difference

Similar Reads

What is SVG ViewBox?

The viewBox attribute in SVG defines the coordinate system and the dimensions of the “viewable” area within an SVG element. It specifies the position and dimensions of the SVG content relative to the SVG canvas. For example, a viewBox of “0 0 100 100” means that the SVG content covers an area from (0, 0) to (100, 100) units....

What is SVG ViewPort?

The viewPort (or viewport) refers to the area where an SVG image is displayed on the screen or within an HTML document. It is the visible area that contains the SVG content. The width and height attributes of the SVG element determine the size of the viewport....

How Do They Work Together?

The viewBox defines the coordinate system and content area.The width and height attributes of the SVG element define the viewport size.When the viewBox and viewport have different aspect ratios, the SVG content is scaled to fit the viewport while maintaining its proportions.If the aspect ratios match, the SVG content is displayed without distortion....

Employing External SVG with viewbox and viewport Attributes

In this method, we put the SVG picture in a separate file and then add it to our webpage using a special tag called . The viewbox helps us to define the size and shape of the picture, and the viewport attributes width and height help us to control the size of the SVG within its containing element. preserveAspectRatio ensures that the SVG maintains its aspect ratio....

Integrating SVG via CSS Background with viewBox and viewport Attributes:

In this method we use SVG as a background image in CSS. The viewbox helps us to define the size and shape of the picture, and the viewport attributes width and height help us to control the size of the SVG within its containing element. preserveAspectRatio ensures that the SVG maintains its aspect ratio. The background-size property controls how the SVG scales within its container....

Difference between SVG Viewbox & Viewport

AspectSVG ViewBoxSVG ViewPortDefinitionDefines the coordinate system and content area within the SVG.Defines the visible area where the SVG content is displayed.AttributeDefined within the SVG content using the viewBox attribute.Defined by the width and height attributes of the SVG element.PurposeSpecifies the position and dimensions of the SVG content relative to the SVG canvas.Determines the size of the visible area that contains the SVG content.Aspect RatioCan have different aspect ratios from the viewport.Usually matches the aspect ratio of the viewport.Scaling BehaviorDetermines how SVG content scales and fits within the viewport.Affects how the SVG image is displayed in terms of size and proportions....

Contact Us