HTML Guides for NAN
Learn how to identify and fix common HTML validation errors flagged by the W3C Validator — so your pages are standards-compliant and render correctly across every browser. Also check our Accessibility Guides.
NaN in JavaScript stands for "Not a Number" and appears when a numeric operation fails — for example, parsing a non-numeric string with parseFloat(), dividing 0 by 0, or referencing an undefined variable in arithmetic. When this NaN value gets concatenated with a unit string like "rem", the result is "NaNrem", which is meaningless to CSS. The browser cannot interpret it, and the W3C validator flags it as an invalid letter-spacing value.
This issue almost always originates from dynamically generated styles — either through JavaScript that sets inline styles, a server-side template that computes CSS values, or a CSS-in-JS library. The rendered HTML ends up containing something like style="letter-spacing: NaNrem", which the validator rightly rejects.
Beyond validation, this is a practical problem: the browser will ignore the invalid declaration entirely, so whatever letter-spacing you intended won't be applied. Your layout may look different than expected, and the invalid value in the markup signals a bug in your code that could affect other computed styles too.
How to Fix It
Trace the source of the value. Search your codebase for where
letter-spacingis set. If it's an inline style, look at the JavaScript or server-side code that generates it.Validate the number before using it. In JavaScript, use
Number.isNaN()orisFinite()to check that a computed value is valid before applying it.Provide a sensible fallback. If the calculation might fail, default to a known-good value like
0ornormal.Fix the root cause. Determine why the calculation produces
NaN— common causes include missing data attributes,undefinedvariables, or parsing non-numeric strings.
Examples
❌ Invalid: NaN concatenated with a unit
This is what the rendered HTML looks like when the bug occurs:
<pstyle="letter-spacing:NaNrem">Spaced text</p>
The JavaScript that likely produced it:
// Bug: getAttribute returns null if data-spacing is missing
letspacing=parseFloat(element.getAttribute('data-spacing'));
element.style.letterSpacing=spacing+'rem';// "NaNrem" if attribute is missing
✅ Fixed: Validate the value before applying it
letraw=parseFloat(element.getAttribute('data-spacing'));
letspacing=Number.isFinite(raw)?raw:0.1;// fallback to 0.1
element.style.letterSpacing=spacing+'rem';
This produces valid inline CSS:
<pstyle="letter-spacing: 0.1rem">Spaced text</p>
✅ Fixed: Using a static CSS value
If the letter-spacing doesn't need to be dynamic, the simplest fix is to use a plain CSS rule instead of computing it in JavaScript:
<style>
.spaced-text{
letter-spacing:0.1rem;
}
</style>
<pclass="spaced-text">Spaced text</p>
✅ Fixed: Server-side template with a guard
If a server-side template generates the style, add a check before rendering:
<!-- Pseudocode for a template engine -->
<!-- Only output the style attribute if the value is a valid number -->
<pstyle="letter-spacing: 0.05rem">Spaced text</p>
Ensure your template logic verifies the value is numeric before injecting it into the markup. If the value is missing or invalid, either omit the style attribute entirely or use a safe default.
Valid letter-spacing values for reference
The letter-spacing property accepts:
- The keyword
normal(default, lets the browser decide) - Any valid CSS length:
0.1rem,1px,0.05em,2px, etc. 0(no extra spacing)
The numeric part must always be a real number — NaN, Infinity, and empty strings are never valid.
Validate at scale.
Ship accessible websites, faster.
Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.
Pro Trial
Full Pro access. Cancel anytime.
Start Pro Trial →Join teams across 40+ countries