JS 操作 `element.style` 時,CSS 屬性名需轉為**駝峰命名法**(`background-color` → `backgroundColor`)。本文提供完整分類對照表,涵蓋盒模型、定位、視覺格式化、字體、文字、顏色背景、表格、列表、生成內容等 9 大類 100+ 屬性,並說明特殊屬性(`float`、`cssText`、CSS 變數)處理方式。
命名轉換規則
- 連字號分隔 → 駝峰:`font-size` → `fontSize`、`z-index` → `zIndex`。
- 前綴 `-webkit-` → `Webkit` (大寫 W):`-webkit-transform` → `webkitTransform`。
- 保留字 `float` → `cssFloat` (標準) / `styleFloat` (IE舊版)。
- 通用:`element.style['font-size']` 字串鍵值也可用,但駝峰為標準。
完整分類對照表 (節選重點)
盒模型
| CSS | JS |
| width / height | width / height |
| margin / margin-top / -right / -bottom / -left | margin / marginTop / Right / Bottom / Left |
| padding / padding-top / ... | padding / paddingTop / ... |
| border / border-width / -style / -color / -top / ... | border / borderWidth / Style / Color / Top / ... |
| box-sizing | boxSizing |
| overflow / overflow-x / -y | overflow / overflowX / Y |
定位與視覺格式化
| CSS | JS |
| position | position |
| top / right / bottom / left | top / right / bottom / left |
| z-index | zIndex |
| display | display |
| visibility | visibility |
| float | cssFloat / styleFloat |
| clear | clear |
| clip / clip-path | clip / clipPath |
| opacity | opacity |
Flexbox / Grid (現代佈局)
| CSS | JS |
| flex / flex-grow / -shrink / -basis / -direction / -wrap / -flow / justify-content / align-items / align-content / align-self / order / gap | flex / flexGrow / Shrink / Basis / Direction / Wrap / Flow / justifyContent / alignItems / alignContent / alignSelf / order / gap |
| grid / grid-template-columns / -rows / -areas / -column / -row / gap / column-gap / row-gap / auto-flow / auto-columns / auto-rows | grid / gridTemplateColumns / Rows / Areas / Column / Row / gap / columnGap / rowGap / autoFlow / autoColumns / autoRows |
字體與文字
| CSS | JS |
| font / font-family / -size / -weight / -style / -variant / -stretch / -size-adjust | font / fontFamily / Size / Weight / Style / Variant / Stretch / SizeAdjust |
| color | color |
| text-align / -decoration / -indent / -transform / -overflow / -shadow / line-height / letter-spacing / word-spacing / white-space / word-break / overflow-wrap / tab-size | textAlign / Decoration / Indent / Transform / Overflow / Shadow / lineHeight / letterSpacing / wordSpacing / whiteSpace / wordBreak / overflowWrap / tabSize |
背景與邊框進階
| CSS | JS |
| background / -color / -image / -position / -size / -repeat / -attachment / -origin / -clip / -blend-mode | background / Color / Image / Position / Size / Repeat / Attachment / Origin / Clip / BlendMode |
| border-radius / -top-left / -top-right / -bottom-right / -bottom-left | borderRadius / TopLeft / TopRight / BottomRight / BottomLeft |
| box-shadow | boxShadow |
| filter / backdrop-filter | filter / backdropFilter |
變形、動畫、轉場
| CSS | JS |
| transform / transform-origin / transform-style / perspective / perspective-origin / rotate / scale / translate | transform / transformOrigin / transformStyle / perspective / perspectiveOrigin / rotate / scale / translate (個別屬性現代瀏覽器支援) |
| transition / -property / -duration / -timing-function / -delay | transition / TransitionProperty / Duration / TimingFunction / Delay |
| animation / -name / -duration / -timing-function / -delay / -iteration-count / -direction / -fill-mode / -play-state | animation / AnimationName / Duration / TimingFunction / Delay / IterationCount / Direction / FillMode / PlayState |
特殊屬性處理
1. `cssText` 一次性讀寫完整內聯樣式
el.style.cssText = 'color: red; font-size: 14px;'; // 覆寫
el.style.cssText += '; background: blue;'; // 追加
console.log(el.style.cssText); // "color: red; font-size: 14px; background: blue;"
2. CSS 自訂屬性 (CSS Variables)
// 設定
el.style.setProperty('--main-color', '#007bff');
el.style.setProperty('--gap', '16px');
// 獲取
getComputedStyle(el).getPropertyValue('--main-color'); // "#007bff"
el.style.getPropertyValue('--main-color'); // 僅內聯設定值
3. `float` 關鍵字衝突
el.style.cssFloat = 'left'; // 標準
el.style.styleFloat = 'left'; // IE ≤ 8
// 推薦統一
el.style.float = 'left'; // 現代瀏覽器支援 style.float 別名
效能建議
- 批量修改用 `cssText` 或 `classList` 切換預定義類別。
- 讀取計算值用 `getComputedStyle(el).prop`,非 `el.style.prop` (後者只讀內聯)。
- 動畫屬性優先用 `transform`/`opacity` (合成器層),避免觸發佈局/繪製。
- 使用 `will-change` 提示瀏覽器優化。
收藏此表,查找 JS 樣式屬性名不再猜測,寫出零錯誤的動態樣式代碼。