简体   繁体   English

如何在其他 div 之上排列 svg 元素?

[英]How to arrange svg elements on top of other div?

I am trying to arrange this small SVG rectangle on top of the base rectangle present in the same SVG viewBox and on top the other div which is just below it.我正在尝试将这个小的 SVG 矩形排列在同一个 SVG viewBox 中存在的基本矩形的顶部,并在它正下方的另一个 div 的顶部。

Any help would be appreciated.任何帮助,将不胜感激。

Edit 1 I want rect with id="over" top of div with class="firstrect" and div with class="secondrect".编辑 1我想要 id="over" 顶部的 div 和 class="firstrect" 和 div 的 class="secondrect"。

截屏

 .main { position: relative; }.secondrect { width: 100%; background: purple; height: 30px; } #over { position: absolute; }
 <div class="main"> <div class="firstrect"> <svg viewBox="0 0 50 10"> <rect width="100%" height="100%" style="fill: rgb(102, 102, 201);" /> <rect id="over" x="10" y="9" width="20%" height="20%" style="fill: rgb(102, 201, 171);" /> </svg> </div> <div class="secondrect"></div> </div>

  1. Add class="viewbox" to the viewbox.class="viewbox"添加到视图框。
  2. Then, add overflow: visible on said class然后,添加溢出:在 class 上可见
  3. Then adjust x and y coordinates as you see fit然后根据需要调整 x 和 y 坐标

 .main { position: relative; } /*Add class="viewbox" to viewbox */.viewbox { overflow: visible; }.secondrect { width: 100%; background: purple; height: 30px; } #over { position: absolute; }
 <div class="main"> <div class="firstrect"> <svg viewBox="0 0 50 10" class="viewbox"> <rect width="100%" height="100%" style="fill: rgb(102, 102, 201);" /> <rect id="over" x="10" y="9" width="20%" height="30%" style="fill: rgb(152, 251, 071);" /> </svg> </div> <div class="secondrect"></div> </div>

set svg overflow: visible;设置svg overflow: visible;

 svg { overflow: visible; }.secondrect { width: 100%; background: purple; height: 30px; } #over { position: absolute; }
 <div class="main"> <div class="firstrect"> <svg viewBox="0 0 50 10"> <rect width="100%" height="100%" style="fill: rgb(102, 102, 201);" /> <rect id="over" x="10" y="9" width="20%" height="20%" style="fill: rgb(102, 201, 171);" /> </svg> </div> <div class="secondrect"></div> </div>

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

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