SoFunction
Updated on 2025-04-10

How to get the style information of an element in JavaScript

Cross-browser method

function getStyle(elem, cssprop, cssprop2){
 if(){
  return [cssprop]
  }else if( && ){
  return (elem, "").getPropertyValue(cssprop2)
 }else{
  return null
 }
}
<style> 
 #elem{font-size:23px;} 
</style> 
<input id ="elem" style="font-size:18px;"/> 
<script>
 = '12px'; 
(getStyle(elem, "fontSize", "font-size")) 
// Output 12px</script>

The priority of getStyle value is:

1. Dynamically set

2. Inline settings

3. Stylesheet settings

4. The default settings of the browser

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.