Unexpected resizing occurs when SVGs are nested within each other

I am experimenting with using nested inline SVGs to creatively position smaller svg images within an svg container. However, I am finding it challenging to grasp the guidelines for sizing nested SVG elements.

svg {
  display: block;
}

Here is my query - why does this code snippet correctly render a 200px by 200px red square:

<svg width="200px" viewBox="0 0 100 100">
  <rect width="100" height="100" fill="red" />
</svg>

But when I attempt to nest another svg element, the square resizes to 150px by 150px?
For example:

<svg>
  <svg width="200px" viewBox="0 0 100 100">
    <rect width="100" height="100" fill="red" />
  </svg>
</svg>

or:

<svg width="200px">
  <svg width="200px" viewBox="0 0 100 100">
    <rect width="100" height="100" fill="red" />
  </svg>
</svg>

or:

<svg width="200px">
  <svg viewBox="0 0 100 100">
    <rect width="100" height="100" fill="red" />
  </svg>
</svg>

Answer №1

To ensure consistency, adjust the width and height of each subsequent <rect>, while keeping the same dimensions for the parent <svg> element.

Example:

<svg width="200px" viewBox="0 0 200 200">

  <rect width="200" height="200" fill="red" />
  
  <svg width="200px" viewBox="0 0 200 200">
  
    <rect width="100" height="100" fill="yellow" />
  
    <svg width="200px" viewBox="0 0 200 200">
    
      <rect width="50" height="50" fill="red" />
    
      <svg width="200px" viewBox="0 0 200 200">
      
        <rect width="25" height="25" fill="yellow" />
      
        <svg width="200px" viewBox="0 0 200 200">

          <rect width="12.5" height="12.5" fill="red" />

        </svg>
      </svg>
    </svg>
  </svg>
</svg>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a form that is unable to be submitted

Can a form be created in HTML without the ability to submit, all while avoiding the use of JavaScript? I would like to utilize <input type="reset"> and have intentionally omitted an <input type="submit">, but there is still the possibility for ...

Centering all td elements except for those with a specific class

Consider the following code snippets: <tr> <td class="foo">chuchu</td> <td>chuchu</td> <td>chuchu</td> <td>chuchu</td> <td>chuchu</td> </tr> Is there a way to center align thes ...

show items in UL LI elements as "inline" within a bootstrap column

Struggling with organizing UL LI items in a bootstrap setup? Trying to make them align horizontally instead of vertically but can't figure it out. Here's my code: <div class="container-fluid" style="margin-bottom:45px;"> <div cl ...

Transfer groups between divisions

In my HTML structure, I have two main divs named Group1 and Group2. Each of these group divs contains at least two inner divs, all with the class .grp_item. Currently, the grp_item class is set to display:none, except for one div in each group that has a c ...

How can I ensure text remains within a parent div and is positioned above an image?

After writing the following HTML and CSS, an issue arose when resizing the page: <div style="position: relative;"> <img width="100%" src="images/some.jpg"> <div class="header"> <h1>my header</h1> ...

An error occurred while trying to set the property 'IS_CHECK' of an object that is undefined

I'm attempting to create a checkbox that, when selected, should also select everything else. I followed the code example provided in this tutorial for angular 2. However, I encountered an error: "ERROR TypeError: Cannot set property 'IS_CHECK&ap ...

"Android Webview's evaluateJavascript function is not returning the expected value

I am attempting to retrieve the username from a webview, but it is returning a null object. webView.settings.javaScriptEnabled = true webView.evaluateJavascript( "(function() { return document.getElementsByClassName('lgn-loginname') })() ...

Creating a tiled grid layout using Bootstrap 3

I’m grappling with a masonry layout issue while using Bootstrap. In my code, I have 5 divs but I’m struggling to get Div 4 and 5 to move up as shown in the attached image. While using margin-top is a quick fix, it disrupts the responsive design. Any su ...

Why does a relatively positioned element always begin at the top corner of the window?

I am really struggling to understand the concept of relative versus absolute positioning, especially when dealing with parent or grandparent elements that have position values. Can someone please help me clarify these two scenarios: Case 1: grandparent ...

Is it wise to use position absolute when floating a React JS tag around a component?

I am eager to dive into a challenging project. My goal is to replicate the design shown in the image below using React JS. Initially, I considered using position: absolute and manipulating the positioning of my divs accordingly. However, upon closer inspec ...

Struggling to add a border to an HTML div element

I'm completely baffled as to why I can't seem to add a border around my div. Here is the link to my jsfiddle: http://jsfiddle.net/4HnKs/1/ It's possible that I've been staring at my computer screen for too long, but no matter how hard ...

"Creating a responsive design: Setting the width of a :before pseudo element based on the size of

I am looking to encircle specific words with an image circle. <p> <span class="Subjekt">Father</span> <span class="Predicate">brings</span> </p> using .Subject:before { position: absolute; background-ima ...

Is there a way for me to perfectly align the image and the h1 header on the webpage?

I'm facing some challenges when it comes to aligning an image and an h1 tag on the same line. I've already tried using display: inline and inline-block, but they only affect the container of the two elements. I also set the width to 100% on the s ...

A guide on using Beautiful Soup to retrieve all text elements from a specific class

This is my custom code snippet: # -*- coding: utf-8 -*- from time import sleep import requests from bs4 import BeautifulSoup def get_all_coins(): response = requests.get("https://coinmarketcap.com/") soup = BeautifulSoup(response.content, 'ht ...

Validation of time picker consistently returns false

While using the daterangepicker library for my form validation with JavaScript, I encountered an issue with the time field. It returns false and displays an error message saying: "Please enter a valid time, between 00:00 and 23:59," preventing me from addi ...

Steps to troubleshoot the TypeError: cv.Mat is not a constructor in opencv.js issue

Encountering a problem while trying to use opencv.js for detecting aruco markers from my camera. Each time I attempt to utilize the method let image = new cv.imread('img'); An error keeps popping up: TypeError: cv.Mat is not a constructor ...

Using jQuery functions like closest(), find(), and children(), it is possible to target and retrieve the sibling of a specific parent

Having trouble determining the URL of a using .closest, .find and potentially other jQuery methods. The webpage structure is as follows: ul ul ul.good li ... li li <a href="/findme">findme</a> ul . ...

Please click the provided link to display the data in the div. Unfortunately, the link disappearance feature is currently not

What I'm Looking For I want the data to be displayed when a user clicks on a link. The link should disappear after it's clicked. Code Attempt <a href="javascript:void(0);" class="viewdetail more" style="color:#8989D3!important;">vi ...

Automatically updating the results section while executing SQL queries in PHP

Here is a JavaScript/Ajax code snippet: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready (function () { var updater = se ...

Trigger Click event when clicking on navigation link using the ID

Currently, I am working with a WordPress theme that includes a page with 10 tabs that open individually. My approach so far has been to provide links to all tabs in the navigation bar. For instance, href="mylink.com/#tabid" It works perfectly when the p ...