Course contentsShow
← React
Lesson 102 of 2,3963. JSX FundamentalsFree lesson

SVG Attributes in JSX: Subtle Naming Differences

How SVG attributes like viewBox and strokeWidth work in JSX, with their capitalization quirks.

SVG Attributes in JSX: Subtle Naming Differences

What you'll learn: How SVG-specific attributes like viewBox and strokeWidth follow unique capitalization rules in JSX.

The SVG Quirk

When you embed SVG elements directly in JSX (like <svg>, <path>, <circle>), most attributes follow the same camelCase pattern you've learned for HTML attributes. However, SVG has some inherited quirks from XML that React preserves for compatibility.

Key Naming Patterns

Compound words stay as one camelCase word:

<svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
  <path strokeWidth="2" fillOpacity="0.5" />
</svg>

Notice: viewBox (not viewbox), strokeWidth (not stroke-width), fillOpacity (not fill-opacity).

Hyphenated attributes with XML namespaces keep their hyphens:

<svg xmlnsXlink="http://www.w3.org/1999/xlink">
  <image xlinkHref="image.png" />
</svg>

The xlink: prefix becomes xlink in camelCase, but namespace declarations like xmlns:xlink become xmlnsXlink.

Why This Matters

Unlike HTML, where React invented conventions like className, SVG is a mature XML-based standard. React chose to respect SVG's existing attribute names while adapting them to JavaScript's naming conventions. Think of it as React being a polite guest—it adjusts slightly but doesn't redecorate the whole house.

Analogy: HTML attributes in JSX are like translating English to Spanish (complete transformation: class → className). SVG attributes are like adapting English names to French pronunciation—recognizable but with subtle adjustments (stroke-width → strokeWidth).

Key Takeaway: SVG attributes in JSX use camelCase for compound words (viewBox, strokeWidth) while preserving XML namespace patterns, blending JavaScript conventions with SVG's XML heritage.