<svg viewBox="0 0 200 60" xmlns="http://www.w3.org/2000/svg">
<g id="greeting">
<text x="5" y="16" transform="scale(2, 2)">Hello World!</text>
<text x="8" y="32" transform="translate(0 20) scale(1.25 1)">
Hello World Again!
</text>
</g>
<rect id="bbox" stroke="green" stroke-width="2" fill="none" />
</svg>
<div id="client-rect"></div>
<ul id="legend"></ul>SVGGraphicsElement: getBBox() method / notebook
SVGGraphicsElement: getBBox() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
svg {
max-width: 400px;
}
#client-rect {
position: absolute;
box-sizing: border-box;
border: 2px dashed red;
}
ul {
padding-inline-start: 1em;
font-family: monospace;
line-height: 1.5;
}const greeting = document.querySelector("#greeting");
const legend = document.querySelector("#legend");
const bbox = greeting.getBBox();
const outline = document.querySelector("#bbox");
outline.setAttribute("x", bbox.x);
outline.setAttribute("y", bbox.y);
outline.setAttribute("width", bbox.width);
outline.setAttribute("height", bbox.height);
const clientRect = greeting.getBoundingClientRect();
const overlay = document.querySelector("#client-rect");
overlay.style.left = `${clientRect.x + window.scrollX}px`;
overlay.style.top = `${clientRect.y + window.scrollY}px`;
overlay.style.width = `${clientRect.width}px`;
overlay.style.height = `${clientRect.height}px`;
for (const [label, rect] of [
["getBBox()", bbox],
["getBoundingClientRect()", clientRect],
]) {
const item = document.createElement("li");
item.textContent = `${label} → ${rect.x.toFixed(1)}, ${rect.y.toFixed(1)}, ${rect.width.toFixed(1)}, ${rect.height.toFixed(1)}`;
legend.append(item);
}<svg viewBox="0 0 200 150" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker
id="dot"
viewBox="0 0 10 10"
refX="5"
refY="5"
markerUnits="userSpaceOnUse"
markerWidth="30"
markerHeight="30">
<circle cx="5" cy="5" r="5" fill="rebeccapurple" />
</marker>
<clipPath id="clip">
<rect x="40" y="40" width="120" height="80" />
</clipPath>
</defs>
<path
id="chevron"
d="M 50 100 L 100 50 L 150 100"
fill="none"
stroke="cornflowerblue"
stroke-width="20"
marker-start="url(#dot)"
marker-end="url(#dot)"
clip-path="url(#clip)" />
<g fill="none" stroke-width="1">
<rect id="fill-box" stroke="red" />
<rect id="stroke-box" stroke="green" />
<rect id="markers-box" stroke="orange" />
<rect id="clipped-box" stroke="black" />
</g>
</svg>
<ul id="legend"></ul>svg {
max-width: 400px;
}
ul {
padding-inline-start: 1em;
font-family: monospace;
line-height: 1.5;
}const chevron = document.querySelector("#chevron");
const legend = document.querySelector("#legend");
const variants = [
{ id: "fill-box", label: "getBBox()", options: {} },
{
id: "stroke-box",
label: "getBBox({ stroke: true })",
options: { stroke: true },
},
{
id: "markers-box",
label: "getBBox({ stroke: true, markers: true })",
options: { stroke: true, markers: true },
},
{
id: "clipped-box",
label: "getBBox({ stroke: true, markers: true, clipped: true })",
options: { stroke: true, markers: true, clipped: true },
},
];
for (const { id, label, options } of variants) {
const box = chevron.getBBox(options);
const outline = document.querySelector(`#${id}`);
outline.setAttribute("x", box.x);
outline.setAttribute("y", box.y);
outline.setAttribute("width", box.width);
outline.setAttribute("height", box.height);
const item = document.createElement("li");
item.style.color = outline.getAttribute("stroke");
item.textContent = `${label} → ${box.x.toFixed(1)}, ${box.y.toFixed(1)}, ${box.width.toFixed(1)}, ${box.height.toFixed(1)}`;
legend.append(item);
}