Arrangement of SVGs within one another

I am working on achieving a layout with multiple SVG elements arranged in a specific way. This layout involves a top-level gray box containing several orange boxes, each of which holds red boxes. The width and height of the top-level SVG are known.

https://i.stack.imgur.com/8dSlmm.png

The orange boxes must maintain the same distance between each other, mimicking the behavior of justify-content: space-around for alignment. Similarly, the red boxes should also maintain their vertical distance from one another.


I have attempted to adjust the positioning using transform-origin, but it does not seem to produce the desired results. Instead, the offset is always calculated from the top-left corner of each element, causing them to be pushed out of view. Below is a basic code snippet that showcases my attempts:

<svg width="400" height="400" style="background:#ccc;">
  <g transform="translate(10, 10)">
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
  </g>
  <g transform="translate(200, 10)">
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
  </g>
  <g transform="translate(360, 10)">
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
    <svg width="50" height="50">
      <rect width="50" height="50" fill="red" />
    </svg>
  </g>
</svg>


I am looking for a way to align these containers in the specified manner without requiring prior knowledge of the dimensions of the red boxes. Each solution I attempt seems to involve complex calculations, such as subtracting half of the width and height of the red boxes. Any help or insights would be greatly appreciated!

Answer №1

It was noted in a feedback from @enxaneta :

Rather than using numerous nested SVG elements, consider placing a rectangle in the defs. The rectangle should be centered at point 0,0

In SVG, object positions are based on the top left corner of the canvas.
When you set the same x=40 values for multiple rectangles, they will align vertically.
To avoid repetition of coordinates and dimensions like width and height for each rectangle, you can define one instance in the <defs> section and duplicate it using the <use> tag.

For each copy, specify the x,y coordinates:

<use xlink:href="#rect" x="40" y="62.5" /> 

To create three columns, enclose a column within a <g> group tag and replicate it with <use>

<use xlink:href="#column" x="150" y="0" />
<svg  xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink"
         width="450" height="400" viewBox="0 0 450 400"    style="background:#ccc;">
 <defs>
  <rect id="rect" width="50" height="50" fill="red" x="0" y="0" /> 
 </defs> 
 
<g id="column" > 
 <rect x="30" y="0" width="70" height="400" fill="#C89F33" />
   <use xlink:href="#rect" x="40" y="62.5" /> 
    <use xlink:href="#rect" x="40" y="175" />
      <use xlink:href="#rect" x="40" y="287.5" /> 
</g> 
  <use xlink:href="#column" x="150" y="0" />
    <use xlink:href="#column" x="300" y="0" />  
       
 </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

The left margin is malfunctioning

As a newcomer to css, I am struggling with adding margin-left to my paragraph and <h2>, and it's not taking effect. I have a 700px div and I want to add a 10px margin-left to both <p> and <h2> in relation to the image. Despite my e ...

Is there a way to iterate through indexed variables in javascript?

After receiving an array of data from a JQuery .ajax function, I noticed that the fields in the array are named and numbered like part1, part2, part3, etc. I attempted to loop through this data using the code below, but unfortunately, it resulted in NaN: ...

Tips for avoiding word fragmentation in <pre> code blocks

pre { white-space: pre-wrap; /* CSS 3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ overflo ...

A guide on integrating mat-select into Angular input fields

I've been trying to implement a mat-select dropdown on my input field, but for some reason, when I click on the arrow, nothing happens and the list doesn't show up. Usually, with autocomplete, when a user starts typing, all the options are displ ...

An image inside this stylishly framed photo with rounded corners

.a { clip-path: polygon(10% 0, 100% 0%, 100% 100%, 10% 100%,10% 60%, 0% 50%, 10% 40%); } .a { position: relative; width: 500px; height: 500px; background:url("https://cdn.pixabay.com/photo/2020/02/16/07/55/thailand-4852830_960_720.jpg"); border-radi ...

How can we create a touch and mouse-friendly horizontal scrolling navigation pattern?

I am trying to implement horizontal scrolling for video testimonials in a single row on a responsive website, but I am encountering an issue. The width of the container class is larger than col-12, which leaves space and causes horizontal scroll on the pag ...

Having trouble getting CSS styles to work properly in conjunction with Javascript code

Currently, I am developing a feature on a canvas that covers the entire webpage's width and length. In this canvas, I have the ability to create boxes by clicking down anywhere on the canvas, dragging my mouse in any direction, and upon releasing the ...

Exporting an HTML table to Excel while excluding any hidden <td> elements

I have a web application with an HTML table that displays data to users. I wanted to export the table to Excel and found a jQuery code for it. However, the exported data includes information that is hidden in the table. Is there a way to export the tabl ...

Issues with implementing CSS Icon Generated Content on a live Azure Web Role

My dilemma lies with my MVC4 web application that I'm attempting to deploy as an Azure web role. The site incorporates Metro UI CSS. While the deployed web role is functioning properly overall, I am encountering an issue with the CSS-generated conten ...

What steps should I follow to make a sticker adhere to a div?

I'm currently in the process of designing a sticker that attaches to the top of a div, much like this: <div className="upgrade-premium"> <div className="upgrade-team"> <h1>Premium</h1> <p>100 ...

Text will not be moved to a new line

Hello, I am encountering an issue with my messaging system where the messages retrieved from the database are not properly going to a new line in the window. Despite adjusting the width, the messages continue to overlap. Below is the code snippet: This co ...

How can I implement horizontal scrolling on a material-ui table containing numerous columns?

As I work with the React Material-UI framework, I am using this table example as a guide. However, I am facing an issue where my table becomes overcrowded when I have numerous columns, causing the content to be cut off. I recently came across the material ...

Creating anchor links with #id that function correctly in an Angular project can sometimes be challenging

My backend markdown compiler generates the HTML content, and I need Angular to retrieve data from the server, dynamically render the content, and display it. An example of the mock markdown content is: <h1 id="test1">Test 1<a href="#test1" title ...

Preserving the position of inline text

I've been working on a button design that I want to make look more realistic by moving the text down 1px inside the button when it's pressed. To achieve this effect, I added an extra pixel to the top and removed one from the bottom. However, the ...

What causes jquery to not trigger PHP when the HTML file resides in a different location?

In my project structure, I have a 'js' folder containing the jQuery function and a PHP script. The HTML file is located in a separate folder. Currently, the setup looks like this: /server/js/global.js /server/js/script.php /server/html/stude ...

The functionality of aos.css seems to be malfunctioning on a Django-powered website

I recently set up a django backend website and attempted to incorporate CSS in my HTML using the following code snippet. {% load static %} <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-wid ...

Ways to incorporate a smooth transition between the opened and closed fab functionality

I am currently utilizing VueJS and Vuetify in my project. I have successfully implemented Vuetify's FAB component, which displays a list of fab buttons as demonstrated in the documentation: <div id="fab-button"> <v-speed-dial v-mo ...

Generate a list of users using Angular in an efficient manner

I am working on creating a user list similar to the image provided below. I am using Angular and Bootstrap for the basics of this project. However, my concern is how to efficiently handle a large number of users. If the user count exceeds 10k, what would b ...

Tips for creating dynamic rope animations with canvas and CSS3 styling

Interested in creating a canvas animation, or possibly using CSS3 for the same purpose. Looking to add an automatic rope animation, similar to it swaying gently. I've searched for scripts and other solutions, but haven't found anything suitable. ...

Extracting Tik Tok videos from their URLs using PHP web scraping

I'm attempting to save videos from the following URL: Original: https://api.tiktokv.com/aweme/v1/playwm/?video_id=v09044340000bq0vmd39if221ld9o5n0 The link then changes to this: http://v16m.tiktokcdn.com/e50056249a925719f71ce8618053e173/5eee4e39/vid ...