CSS Attribute Name in JavaScript

By Xah Lee. Date: . Last updated: .

CSS attribute names are hyphen separated, example: font-size.

The corresponding property in JavaScript is camelCase: fontSize.

Here is a table of setting styles in CSS and in JavaScript:

CSS Syntax vs JavaScript DOM Syntax
CSSJavaScript
padding:1emelm.style.padding="1em"
border:solid thin redelm.style.border="solid thin red"
background-color:redelm.style.backgroundColor="red"
font-size:largeelm.style.fontSize="large"
font-family:"DejaVu Sans",sans-serifelm.style.fontFamily='"DejaVu Sans",sans-serif'

How to Find the Exact CSS Attribute Name in JavaScript

A easy way to check style attribute is to use browser's JavaScript console name completion feature. Type document.body.style. then press Tab, it'll show a list. [see How to Use Browser Console]

Google Chrome browser console completion
Google Chrome browser console JavaScript keyword completion
BUY Ξ£JS JavaScript in Depth

JavaScript/DOM, Change CSS