skip to Main Content

I have three separate SVG files and when they are layered on top of each other they will create one single graphic. The first SVG file is a red triangle, the second is a blue circle that sits inside the triangle and the third is a purple rectangle that sits on the bottom of the triangle(a little space between triangle and rectangle). My goal is to layer all three SVG files on top of each other in the center of the page. Below is my HTML and CSS code.

HTML

type<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Graphic-center</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
  
<div class="graphic">
   <div>
        <img src="stackoverflow/SVG/triangle.svg" class="triangle" width="150px"/>
   </div>
<div>
   <img src="stackoverflow/SVG/circle.svg" class="circle" width="150px"/>
</div>
<div>
        <img src="stackoverflow/SVG/rectangle.svg" class="rectangle" width="150px"/>
</div>
    </div>
    
</body>
</html>


CSS

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.graphic{
  height: 100vh;
  width: 100vw;
  background-color: palegreen;
  display: grid;
  place-items: center;
  position: relative;
}
.triangle{
  position: absolute;
}
.circle{
  position: absolute;
  top:0;  
}
.rectangle{
  position:relative;
}

As you can see in my CSS, I tried using—position: absolute; and position: relative;—but I still can’t get them to overlay each other properly in the center of the page. Please keep in mind that once the SVG files are properly centered, I am going to animate them using @keyframes, and I need to animate them individually (unless there’s another way) therefore the position of each SVG file cannot be fixed on the page (i.e. I need to be able to move them). Can anyone help? Thanks in advance.

2

Answers


  1. To center align and overlay the three SVG files, you can use flexbox and absolute positioning. Here’s an updated version of your HTML and CSS code:

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    html, body {
      height: 100%;
    }
    
    .graphic {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      width: 100%;
      background-color: palegreen;
      position: relative;
    }
    
    .triangle, .circle, .rectangle {
      position: absolute;
    }
    
    .circle {
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }
    
    .rectangle {
      bottom: 20px; /* Adjust this value to add space between the triangle and rectangle */
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Graphic-center</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
      
    <div class="graphic">
        <img src="stackoverflow/SVG/triangle.svg" class="triangle" width="150px"/>
        <img src="stackoverflow/SVG/circle.svg" class="circle" width="150px"/>
        <img src="stackoverflow/SVG/rectangle.svg" class="rectangle" width="150px"/>
    </div>
        
    </body>
    </html>

    The outer div with the class "graphic" uses flexbox to center align the SVG files both horizontally and vertically. This ensures they are placed in the center of the page.

    The position of each SVG file is set to absolute to allow them to overlay each other.

    The circle SVG file is centered within its parent div using margin: auto and setting all sides (top, right, bottom, left) to 0. This centers the circle both horizontally and vertically within the triangle.

    The rectangle SVG file is positioned at the bottom using the bottom property. You can adjust the value of "bottom" to add or reduce the space between the triangle and the rectangle.

    Login or Signup to reply.
  2. Just put them all in the same grid row and column.

    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    .graphic {
      background-color: palegreen;
      display: grid;
      place-items: center;
      grid-template-columns: 100vw;
      grid-template-rows: 100vh;
    }
    
    .triangle {
      z-index: 2;
    }
    
    .circle,
    .rectangle,
    .triangle {
      display: block;
      grid-column: 1;
      grid-row: 1;
    }
    <div class="graphic">
      <div class="rectangle">
        <svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
        <rect width="100%" height="100%" fill="red" />
    </svg>
      </div>
      <div class="triangle">
        <svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
        <text x="150" y="125" font-size="60" text-anchor="middle" fill="yellow">SVG</text>
    </svg>
      </div>
      <div class="circle">
        <svg version="1.1" width="300" height="200" xmlns="http://www.w3.org/2000/svg">
           <circle cx="150" cy="100" r="80" fill="orange" />
     </svg>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search