Invalid CSS Property
The code sets a CSS property that doesn't exist or uses incorrect syntax.
Explanation
Invalid CSS Property occurs when JavaScript tries to set a CSS property on an element that doesn't exist or uses incorrect CSS syntax. In React, this happens when using inline styles with invalid property names. In vanilla JavaScript, it occurs when setting style properties with incorrect names. Common mistakes include using hyphenated names (font-size) instead of camelCase (fontSize), using invalid CSS values, or referencing properties that don't exist in CSS.
Common Causes
- Using CSS syntax instead of camelCase
- Missing units for non-zero values
- Invalid property name
- Using shorthand properties incorrectly
- Browser compatibility issues
Solution
Use camelCase for CSS property names in JavaScript: fontSize instead of font-size, backgroundColor instead of background-color. Check CSS property names against the specification. Ensure values include proper units (px, rem, em, %) where required. For computed styles, use getComputedStyle() to check current values. In React, verify the style object properties are valid CSS. Use TypeScript with CSS type definitions to catch invalid properties. Check browser compatibility for newer CSS features.
Code Example
// Invalid: uses CSS syntax instead of JavaScript
const style = {
'font-size': '16px', // Warning: Invalid DOM property
'background-color': 'red',
'text-align': 'center',
};
// Fix: use camelCase
const style = {
fontSize: '16px',
backgroundColor: 'red',
textAlign: 'center',
};
// Missing units for non-zero values
const style = {
width: 100, // Warning: missing unit!
opacity: 0.5, // this is fine (unitless)
};
// Fix: add units
const style = {
width: '100px',
opacity: 0.5,
};
// Invalid property name
const style = {
font: '16px Arial', // shorthand in inline styles is tricky
bg: 'red', // not a valid CSS property!
};
// Fix: use individual properties
const style = {
fontFamily: 'Arial',
fontSize: '16px',
backgroundColor: 'red',
};
Error Information
Language
javascript
Difficulty
Beginner
Views
17
Related Errors
JavaScript
TypeError: X is not a function
The code tries to call a value as a function, but it's not a function.
Frontend
WebSocket Connection Failed
The WebSocket connection could not be established or was closed unexpectedly.
JavaScript
TypeError: Cannot Read Property of Undefined
The code tries to access a property or method on an undefined or null value.
Frontend
Webpack Module Not Found
Webpack cannot resolve a module or file imported in the code.