Ich habe versucht, eine Sammlung von Svg Pfade/Text vertikal in CSS/JS für eine ganze Weile ohne Glück zu zentrieren. Keine der Lösungen, die ich hier gefunden habe, hat bisher funktioniert. Ich habe mehrere < Text>, < Kreis>, < Rect>, obwohl ich glaube nicht, die Art des Inhalts wirklich wichtig ist. Die Höhe ist variabel, also habe ich an 2 Methoden gedacht: 1) Umwandlung über 50%. Dies funktioniert nicht, weil Sie eine Untermenge von Inhalt nicht von einer SVG, von dem, was ich bisher gefunden habe, transformieren können. Das < g> -Tag war vielversprechend, aber ich hatte kein Glück.Vertikal zentriert Teil von Svg Inhalt
2) Meine zweite Idee war, die Maske per Javascript nach dem Laden des Inhalts anzupassen. Dies funktioniert ebenfalls nicht.
Der gesamte Code ist auf Code-Stift und auch hier kopiert: https://codepen.io/anon/pen/MmdWXB
HTML:
<div class="mask_group" id="mask_container">
<div class="text">
<svg>
<defs>
<mask id="mask" x="0" y="0" width="100%" height="100%" >
<rect class="cutout label" x="0" y="0" width="100%" height="100%"/>
<text class="label normal_font_weight" y="1em" dy="0em" x="50%">A</text>
<text class="label normal_font_weight" y="3em" dy="0em" x="50%">B</text>
<line class="label" x1="47%" y1="4.6em" x2="53%" y2="4.6em" style="stroke:rgb(255,0,0);stroke-width:1.5" />
<text class="label" y="7em" dy="0em" x="50%">C</text>
<text class="label" y="9em" id="final_text" dy="0em" x="50%">D</text>
</mask>
</defs>
<rect id="base" x="0" y="0" width="100%" height="100%"/>
</svg>
</div>
</div>
<div style="background-color:blue; width: 100%; height: 100%">
</div>
CSS:
html,body {
height: 100%;
}
.text {
position: relative;
/*top: 0;
left: 0;*/
width: 100%;
height: 100%;
margin-left: 0%;
/*z-index: 11; */
}
.mask_group {
position: fixed;
width: 100%;
height: 100%;
z-index: 10;
}
svg {
position: absolute;
width: 100%;
height: inherit;
}
svg text {
text-anchor: middle;
}
.cutout {
fill: white;
}
svg #base {
fill: #051E2A; /*#1F5773;*/
-webkit-mask: url(#mask);
mask: url(#mask);
}
/*Unused*/
.vert_center {
position: relative;
margin-top: 50%;
transform: translateY(-50%);
}
JS:
function body_loaded() {
var final_text = document.getElementById("final_text");
var position = final_text.getBoundingClientRect();
var bottom = position.bottom;
var mask_container = document.getElementById("mask_container");
var mask_position = mask_container.getBoundingClientRect();
var mask_height = mask_position.height;
var origin_y = (mask_height - bottom)/2;
alert(origin_y);
var mask = document.getElementById("mask");
mask.style.y = origin_y;
}
Vielen Dank im Voraus!
SVG-Transformation wird nicht CSS-Transformation. Es verwendet nur dimensionslose Zahlen, die auf das vom "Viewport" festgelegte "Userspace" -Koordinatensystem verweisen. Lesen Sie [hier] (https://www.w3.org/TR/SVG11/coords.html). – ccprog
Ihr SVG ist ungültig.XML '' hat kein schließendes Tag. –
SVG hat kein automatisches Layout wie HTML. Wenn Sie Ihre Textobjekte in der Mitte des Rechtecks haben wollen, müssen Sie ihnen entsprechende Koordinaten geben ('x' und' y'). –