简体   繁体   English

在 DIV 中将 SVG 相互堆叠

[英]Stacking SVG's ontop of each other in a DIV

I"m looking to stack these three images ontop of each other. I don't care if they're showing as they're animated and two will pop out horizontally from the side of each.我希望将这三个图像堆叠在一起。我不在乎它们是否显示为动画,两个将从每个侧面水平弹出。

I however am getting an issue.然而,我遇到了一个问题。

Please see attached photo:请看附图:

容器问题

All three SVG's are contained within the below structure:所有三个 SVG 都包含在以下结构中:

<ImageContainer>
  <MainIcon />
  <JobListingIcon />
  <SingularListing />
</ImageContainer>;

This is within a flex box:这是在一个弹性盒子里:

const ImageContainer = styled.div`
    width: 90%;
    height: 400px;
    margin-top: 20px;
    background-color: #636388;

    display: flex;

    align-items: center;
    justify-content: center;
    flex-direction: row;
`;

I don't understand why they're being shown this way, and I have tried to have all 3 SVG's positioned absolute, but nothing.我不明白为什么它们会以这种方式显示,并且我试图将所有 3 个 SVG 定位为绝对位置,但没有。

What's the way to stack these?堆叠这些的方法是什么? It it not a flex box?它不是一个弹性盒子吗? Potentially something like a MUI grid?可能类似于 MUI 网格?

Sorry!对不起!

The problem with a phrase like "on top of each other" is that it is ambiguous.像“在彼此之上”这样的短语的问题在于它是模棱两可的。 Do you mean:你的意思是:

  1. vertically arranged on the page, or在页面上垂直排列,或
  2. one covers the other一个覆盖另一个

It sounds like you might mean the second one.听起来你可能是指第二个。 You can achieve that with absolute positioning.您可以通过绝对定位来实现。

parent    <-- make this "position: relative;"
   child  )     
   child  )  make either (or both) of these "position: absolute; top: 0;"

If those child elements are <svg> elements, then you'll also need to make them display: block , since SVGs are display: inline-block by default.如果这些child元素是<svg>元素,那么您还需要让它们display: block ,因为 SVG 默认是display: inline-block

Demo演示

 .parent { position: relative; width: 100px; height: 150px; } svg { display: block; }.svg-two { position: absolute; top: 0; }
 <div class="parent"> <svg class="svg-one" width="100" height="100"> <rect x="0" y="0" width="100" height="100" fill="linen"/> <circle cx="50" cy="50" r="40" fill="red"/> </svg> <svg class="svg-two" width="100" height="150"> <rect x="20" y="0" width="60" height="150" fill="limegreen"/> </svg> </div>

Try to use Flexbox property flex-direction:column like that:尝试像这样使用Flexbox属性flex-direction:column

const ImageContainer = styled.div`
    width: 90%;
    height: 400px;
    margin-top: 20px;
    background-color: #636388;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM