Get computed font size for DOM element in JS
You could try to use the non-standard IE element.currentStyle
property, otherwise you can look for the DOM Level 2 standard getComputedStyle
method if available :
function getStyle(el,styleProp) {
var camelize = function (str) {
return str.replace(/\-(\w)/g, function(str, letter){
return letter.toUpperCase();
});
};
if (el.currentStyle) {
return el.currentStyle[camelize(styleProp)];
} else if (document.defaultView && document.defaultView.getComputedStyle) {
return document.defaultView.getComputedStyle(el,null)
.getPropertyValue(styleProp);
} else {
return el.style[camelize(styleProp)];
}
}
Usage:
var element = document.getElementById('elementId');
getStyle(element, 'font-size');
More info:
- Get Styles (QuirksMode)
Edit: Thanks to @Crescent Fresh, @kangax and @Pekka for the comments.
Changes:
- Added
camelize
function, since properties containing hypens, likefont-size
, must be accessed as camelCase (eg.:fontSize
) on thecurrentStyle
IE object. - Checking the existence of
document.defaultView
before accessinggetComputedStyle
. - Added last case, when
el.currentStyle
andgetComputedStyle
are not available, get the inline CSS property viaelement.style
.
How to modify computed font-size in Javascript
You need to set the new size to the element's style.fontSize
property, and not the computed value that you get from window.getComputedStyle(), e.g.
el.style.fontSize = elNewFontSize;
--
As an aside, and not directly related, there is no reason to have the variable name twice, you can initialize it with the declaration, i.e. instead of:
var elNewFontSize
elNewFontSize = ...
Simply write
var elNewFontSize = ...
How To Get Font Size in HTML
Just grabbing the style.fontSize
of an element may not work. If the font-size
is defined by a stylesheet, this will report ""
(empty string).
You should use window.getComputedStyle.
var el = document.getElementById('foo');
var style = window.getComputedStyle(el, null).getPropertyValue('font-size');
var fontSize = parseFloat(style);
// now you have a proper float for the font size (yes, it can be a float, not just an integer)
el.style.fontSize = (fontSize + 1) + 'px';
I want to get the font-size using window.getComputedStyle() in javascript but it can't
You should pass an element to .getComputedStyle
method. .getElementsByClassName
returns a collection. You either need to select the first element from the set or use .querySelector
for selecting the target element. Also note that returned object by .getComputedStyle
doesn't have fontSize
method, it's a property. You can also use the .getPropertyValue
for getting a specific value:
// select the first .menu element
var menu = document.querySelector(".menu");
var a = window.getComputedStyle(menu, null).getPropertyValue('font-size');
Getting the calculated font-size of a div
function elementCurrentStyle(element, styleName){
if (element.currentStyle){
var i = 0, temp = "", changeCase = false;
for (i = 0; i < styleName.length; i++)
if (styleName[i] != '-'){
temp += (changeCase ? styleName[i].toUpperCase() : styleName[i]);
changeCase = false;
} else {
changeCase = true;
}
styleName = temp;
return element.currentStyle[styleName];
} else {
return getComputedStyle(element, null).getPropertyValue(styleName);
}
}
alert(elementCurrentStyle(myDiv,"font-size"));
I've describe this "getting computed style" issue few weeks ago here.
cheers,
Related Topics
Set Active Tab Style With Angularjs
Get a CSS Value With JavaScript
How to Change the Style of Alert Box
Angular: Conditional Class With *Ngclass
Jquery Svg, Why Can't I Addclass
How to Hide a ≪Option≫ in a ≪Select≫ Menu With Css
How to Use Jquery to Wait For the End of Css3 Transitions
Only Detect Click Event on Pseudo-Element
Setting Background-Image Using Jquery CSS Property
Bootstrap Modal Appearing Under Background
How to Generate a Simple Popup Using Jquery
Convert Binary Data to Base64 With JavaScript
Canvas2D Todataurl() Different Output on Different Browser
How to Select Multiple Files With ≪Input Type="File"≫