簡體   English   中英

SVG 文本未水平居中對齊

[英]SVG text not horizontally center aligning

我使用 Adobe XD 生成了一個 SVG。 他們使用變換來定位東西,但我的迷你電腦屏幕上的文本並不總是相同的寬度(它是動態生成的)。 我已經嘗試錨定,任何我能找到的東西,但它仍然沒有用。 這是當前代碼的樣子: 外觀如何

這是代碼:

 <svg xmlns="http://www.w3.org/2000/svg" width="903.5" height="860.5" viewBox="0 0 1200 1041"> <g transform="translate(-397)"> <g transform="translate(507 975)" fill="#fff" stroke="#707070" strokeWidth="1" > <rect width="907" height="66" rx="33" stroke="none" /> <rect x="0.5" y="0.5" width="906" height="65" rx="32.5" fill="none" /> </g> <rect width="119" height="395" transform="translate(901 613)" fill="#fff" /> <g transform="translate(397)" fill="#232323" stroke="#fff" stroke-width="30" > <rect width="1127" height="627" rx="103" stroke="none" /> <rect x="15" y="15" width="1097" height="597" rx="88" fill="none" /> </g> <text fill="white" fontSize="96" fontFamily="Fredoka" > {screenText} </text> </g> </svg>

你可以使用 transform/translate、 text-anchordominant-baseline將文本放在某物的中間。

 body { background: gray; }
 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 1041"> <g transform="translate(600 975)" fill="#fff" stroke="#707070" stroke-width="1"> <rect x="-453.5" width="907" height="66" rx="33" stroke="none" /> <rect x="-454" y="0.5" width="906" height="65" rx="32.5" fill="none" /> </g> <rect width="119" height="395" transform="translate(545.5 613)" fill="#fff" /> <g transform="translate(50)" fill="#232323" stroke="#fff" stroke-width="30"> <rect width="1127" height="627" rx="103" stroke="none" /> <rect x="15" y="15" width="1097" height="597" rx="88" fill="none" /> </g> <text fill="#fff" font-size="96" font-family="Fredoka" transform="translate(600 300)" text-anchor="middle" dominant-baseline="middle"> {screenText} </text> </svg>

感謝 Buhan Yu 的評論,我了解到您需要指定 x 和 y 以將其居中對齊。 我設置了x="50%"並且成功了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM