I am attempting to create an SVG rectangle around SVG text. I have noticed that when using .width()
or .height()
on SVG text, Chrome provides the expected results while Firefox does not. Here is a link to a demo I created on jsfiddle.
$(document).ready(function(){
var $rect = $(document.createElementNS('http://www.w3.org/2000/svg', 'rect'));
var x = 50;
var y = 50;
var fontSize = 10;
$rect.attr({
'x' : x,
'y' : y,
'stroke' : 'black',
'stroke-width' : 1,
'fill' : 'white'
});
$rect.appendTo($("#svgArea"));
var $svgText = $(document.createElementNS('http://www.w3.org/2000/svg', 'text'));
$svgText.attr({
'x': x,
'y': y,
'font-family' : 'verdana',
'font-weight': 'bold',
'font-size' : fontSize
});
var node = document.createTextNode("Lorem Ipsum");
$svgText.append(node);
$svgText.appendTo($("#svgArea"));
var textWidth = $svgText.width();
var textHeight = $svgText.height();
$rect.attr({
'x': x,
'y': y - textHeight + 3,
'width' : textWidth + 2,
'height': textHeight
});
});
html
<svg height="200px" width="200px" id="svgArea">
</svg>
css
#svgArea{
border: 1px solid black;
}